-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsll(test).cpp
212 lines (194 loc) · 3.85 KB
/
sll(test).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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<iostream>
#include<iomanip>
using namespace std;
template<class T>
class node
{
public:
T data;
node *next;
node *prev;
};
template<class T>
class lists
{
public:
node <T>*head;
node <T>*tail;
int l;
lists()
{
l = 0;
head = NULL;
tail = NULL;
}
node<T> *create_new_node(T p);
void create_new_list(node<T> *);
void search(T);
void disp_list();
void add_to_pos(T,T);
void del_tail();
void concat(lists);
};
template<class T>
node<T>* lists<T>:: create_new_node(T i)
{
node<T> *newptr;
newptr=new node<T>;
newptr->data=i;
newptr->next=NULL;
return(newptr);
}
template<class T>
void lists<T> :: create_new_list(node<T>*newptr)
{
node<T> *temp;
temp=head;
head=newptr;
newptr->next=temp;
}
template<class T>
void lists<T>::search(T data)
{
int i = 1;
int count = 0;
node<T> *tmp;
tmp = head;
while(tmp != NULL)
{
if(tmp->data == data)
{
cout<<endl<<data<<" FOUND AT INDEX: "<<i<<endl;
count++;
}
tmp = tmp->next;
i++;
}
if(count == 0)
{
cout<<endl<<data<<"NOT FOUND IN THE LIST."<<endl;
}
}
template<class T>
void lists<T>::add_to_pos(T d,T pos)
{
node<T>* temp = head;
node<T>* prev = head;
node<T>* temp1 = head;
if(pos == 1)
{
temp1->next = head;
head = temp1;
}
else
{
for(int i=1; i<pos-1; i--)
{
prev = prev->next;
}
for(int i = 1; i < pos; i++)
{
temp = temp->next;
}
temp1->next = temp;
prev->next = temp1;
}
}
template<class T>
void lists<T>::del_tail()
{
node<T> *tmp;
tmp = tail;
tail = tail->prev;
tail->next = NULL;
l--;
delete tmp;
}
template<class T>
void lists<T>::concat(lists l1)
{
node<T> *tmp ;
tmp = this->head;
while(tmp->next!= NULL)
{
tmp = tmp->next;
}
tmp->next = l1.head;
}
template<class T>
void lists<T>::disp_list()
{
cout<<"\n";
node<T> *tmp;
tmp = head;
while(tmp != NULL)
{
// cout<<endl<<tmp->data<<"\n\n";
//cout<<tmp->data<<"\t"<<setw(8)<<tmp->prev<<"\t"<<tmp->next;
cout<<tmp->data<<"\t";
tmp = tmp->next;
}
}
int main()
{
char ch;
int choice;
lists<int>l,l1;
do
{
cout<<"\nTHE LINKED LIST PROGRAM: ";
cout<<"\nMENU DRIVEN PROGRAM: ";
cout<<"\n1. ADDING TO ANY RANDOM POSITION OF LIST.";
cout<<"\n2. DELETION OF TAIL POSTION OF LIST.";
cout<<"\n3. DISPLAYING THE LIST: ";
cout<<"\n4. CONCATENATION OF THE LISTS ";
cout<<"\n5. SEARCHING OF ANY ELEMENT IN THE LIST";
cout<<"\nENTER YOUR CHOICE: ";
cin>>choice;
switch(choice)
{
case 1:
{
int item,pos;
cout<<"\nENTER THE ELEMENT YOU WANT TO ADD: ";
cin>>item;
cout<<"\nAT WHICH POSITION YOU WANT TO ADD AT THE ELEMENT: ";
cin>>pos;
l.add_to_pos(item,pos);
cout<<"\nADDED.";
break;
}
case 2:
{
cout<<"\nDELETE FROM TAIL POSITION: ";
l.del_tail();
break;
}
case 3:
{
cout<<"\nTHE FOLLOWING LINKED LIST IS: ";
l.disp_list();
break;
}
case 4:
{
cout<<"\nCONCATENATION OF LISTS: ";
l1.concat(l);
break;
}
case 5:
{
int item;
cout<<"\nENTER ELEMENT YOU WANT TO SEARCH: ";
cin>>item;
cout<<"\nSEARCHING OF ELEMENT IN LIST: ";
l.search(item);
}
default:
cout<<"\nWRONG CHOICE.";
}
cout<<"\nDO YOU WANT TO CONTINUE?: ";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}