-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_new.cpp
61 lines (53 loc) · 1.16 KB
/
linked_list_new.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void traverse_linked_list(struct Node *head)
{
while (head != NULL)
{
cout << head->data << "||";
head = head->next;
}
}
void add_node_to_end(struct Node *head, int data)
{
struct Node *node_to_be_inserted = new Node;
node_to_be_inserted->data = data;
node_to_be_inserted->next = NULL;
while (head->next != NULL)
{
head = head->next;
}
head->next = node_to_be_inserted;
}
struct Node *add_node_to_beginning(struct Node *head, int data)
{
struct Node *node_to_be_inserted = new Node;
node_to_be_inserted->data = data;
node_to_be_inserted->next = head;
return node_to_be_inserted;
}
int main()
{
struct Node *head;
struct Node *first;
struct Node *second;
head = new Node;
first = new Node;
second = new Node;
head->data = 67;
head->next = first;
first->data = 98;
first->next = second;
second->data = 78;
second->next = NULL;
traverse_linked_list(head);
cout << endl;
head = add_node_to_beginning()
traverse_linked_list(head);
return 0;
}