-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion in Linked List
176 lines (173 loc) · 3.85 KB
/
Insertion in Linked List
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
struct node *link;
};
struct node *start = NULL; //declaring a head pointer
struct node* createNode() // allocating memory to a node
{
struct node *n;
n = (struct node *)malloc(sizeof(struct node));
return n;
}
void insertNodeAtEnd()
{
struct node *temp;
temp = createNode();
cout << "Enter a number : ";
cin >> temp->data;
temp->link = NULL;
if(start==NULL){ // when the list is empty
start = temp;
}
else{
struct node *t = start;
while(t->link!=NULL){
t = t->link;
}
t->link = temp;
}
cout << "\nElement inserted.\n\n";
}
void insertNodeAtBeg()
{
struct node *temp;
temp = createNode();
cout << "Enter a number : ";
cin >> temp->data;
temp->link = NULL;
if(start==NULL){
start = temp;
}
else{
struct node *t;
t = temp;
temp->link = start;
start = temp;
}
cout << "\nElement inserted.\n\n";
}
void insertAfter()
{
if(start==NULL){
cout << "\nList is empty. Create List First.\n";
}
else {
int node;
struct node *temp;
temp = createNode();
cout << "Enter the number you want to insert : ";
cin >> temp->data;
temp->link = NULL;
cout << "Enter the node after you want to insert : ";
cin >> node;
struct node *t = start;
while(t->data!=node&&t->link!=NULL){ // for finding the node
t = t->link;
}
if(t->link==NULL){ //when the node is present at the beginning
t->link = temp;
}
else {
temp->link = t->link;
t->link = temp;
}
cout << "\nElement inserted.\n\n";
}
}
void insertBefore()
{
if(start==NULL){
cout << "\nList is empty. Create List First.\n";
}
else {
int node;
struct node *temp;
temp = createNode();
cout << "Enter the number you want to insert : ";
cin >> temp->data;
temp->link = NULL;
cout << "Enter the node before you want to insert : ";
cin >> node;
struct node *t = start,*p = start;
while(t->data!=node){
if(t==p){
t = t->link;
}
else {
t = t->link;
p = p->link;
}
}
if(t==p){ // when the node is present at beginning, both t and p pointers point to same node
temp->link = t;
start = temp;
}
else {
temp->link = t;
p->link = temp;
}
cout << "\nElement inserted.\n\n";
}
}
void displayList(struct node *n)
{
struct node *t;
if(start==NULL){
cout << "\nList is empty.\n\n";
}
else {
t = start;
while(t!=NULL){
cout << t->data << " ";
t = t->link;
}
}
cout << "\n\n";
}
int menu()
{
int ch;
cout << "1. Insert an element at the end.\n";
cout << "2. Insert an element at the beginning.\n";
cout << "3. Insert an element after a node.\n";
cout << "4. Insert an element before a node.\n";
cout << "5. Display List.\n";
cout << "6. Exit\n";
cout << "Enter your choice : ";
cin >> ch;
return ch;
}
int main()
{
while(1)
{
switch(menu())
{
case 1:
insertNodeAtEnd();
break;
case 2:
insertNodeAtBeg();
break;
case 3:
insertAfter();
break;
case 4:
insertBefore();
break;
case 5:
displayList(start);
break;
case 6:
exit(0);
break;
default:
cout << "\nInvalid choice\n\n";
}
}
}