-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllist.cpp
More file actions
149 lines (134 loc) · 2.22 KB
/
llist.cpp
File metadata and controls
149 lines (134 loc) · 2.22 KB
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
#include<iostream>
using namespace std;
struct Node {
int info;
Node * link;
} *start, *newptr, *save, *ptr, *rear;
Node * Create_new_node(int n)
{
ptr = new Node;
ptr -> info = n;
ptr -> link = NULL;
return ptr;
}
void Insert_beg (Node * np)
{
if (start == NULL)
start = rear = np;
else
{
save = start;
start = np;
np -> link = save;
}
}
void Insert_end (Node *np)
{
if(start == NULL)
start = rear = np;
else
{
rear -> link = np;
rear = np;
}
}
void Display(Node * np)
{
cout<<"\nThe linked list is: ";
while(np != NULL)
{
cout<<np -> info<<"->";
np = np -> link;
}
cout<<"!!!\n";
}
void DelNode(int item)
{
int flag = 0;
if(start == NULL)
cout<<"!!!Underflow!!!";
else
{
save = start;
ptr = save -> link;
if(start -> info == item)
{
start = ptr;
delete save;
flag = 1;
}
else
{
while(ptr -> info != item)
{
ptr = ptr -> link;
save = save -> link;
if(ptr -> info == item)
{
if(ptr -> link== NULL)
{
save -> link = NULL;
delete ptr;
rear = save;
flag = 1;
}
else
{
save -> link = ptr -> link;
delete ptr;
flag = 1;
}
}
}
if (flag == 0)
cout<<"\nItem not found";
}
}
}
int main()
{
start = rear = NULL;
int info, item, ch;
char ch1;
do
{
cout<<"Enter your choice:"<<endl
<<"1.Insert a node in the beginning of the linked list"<<endl
<<"2.Insert a node at the end of the linked list"<<endl
<<"3.Delete a node containing a particular information"<<endl
<<"4.View the linked list"<<endl;
cin>>ch;
if(ch==1)
{
cout<<"Enter the information of the node to be added";
cin>>info;
newptr = Create_new_node(info);
Insert_beg(newptr);
Display(start);
}
else
if(ch==2){
cout<<"Enter the information of the node to be added";
cin>>info;
newptr = Create_new_node(info);
Insert_end(newptr);
Display(start);
}
else
if(ch==3){
cout<<"Enter the item to be deleted";
cin>>item;
DelNode(item);
Display(start);
}
else
if(ch==4){
Display(start);
}
else
cout<<"Wrong choice!!";
cout<<"Do you want to continue(y/n)";
cin>>ch1;
}while(ch1 == 'y' || ch == 'Y');
return 0;
}