-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList(append).cpp
84 lines (57 loc) · 1.08 KB
/
LinkedList(append).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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
} *head, *tail;
// node *head;
// node *tail
void appendLinkedList(int item)
{
node *tmp = new node;
tmp -> data = item;
tmp -> next = NULL;
if(head == NULL)
{
head = tmp;
tail = head;
}else
{
node *trvlr = head;
while(trvlr -> next != NULL)
{
trvlr = trvlr -> next;
}
trvlr -> next = tmp;
// tail->next = ptr; // these two lines of code also work instead of the above whole else block...
// tail = tail->next;
}
}
void printLinkedList()
{
node *trvlr = head;
cout<<"\n Node in structure: ";
while(trvlr != NULL)
{
cout<<trvlr -> data <<"->";
trvlr = trvlr -> next;
}
cout<<" NULL \n";
}
int main()
{
head = NULL;
tail = NULL;
int size, item;
cout<<"\n enter linked list size: ";
cin>>size;
for(int i=1; i<=size; i++)
{
cout<<"\n enter item "<<i<<" to linked list: ";
cin>>item;
appendLinkedList(item);
}
printLinkedList();
return 0;
}