forked from mohitarora3/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinklist.cpp
More file actions
215 lines (198 loc) · 4.08 KB
/
linklist.cpp
File metadata and controls
215 lines (198 loc) · 4.08 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
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
213
214
215
#include<iostream>
#include<conio.h>
using namespace std;
class SNode {
/*
objective: Create a class for a Node for Single Linked list
input parameters: none
output value: none
description: SNode class defines the node structure
approach: Class defines data item is names element with datatype string
and link is named next pf snode type
*/
private:
string elem;
SNode* next;
friend class SLinkedList; // provides SLinkedList access to SNode
public:
SNode()
{
next=0;
}
SNode(string str,SNode *ptr=0)
{
elem=str;
next=ptr;
}
};
class SLinkedList {
/*
objective: Create a Single LInked List class
input parameters: none
output value: none
side effects: Class SlinkedList defines Single Linked LIst class
approach: Class definition
*/
public:
SLinkedList(); // empty list constructor
~SLinkedList(); // destructor
bool empty() ; // is list empty?
string front() ; // get front element
void addFront(string e); // add to front of list
void addBack(const string e); // add to back of list
void removeFront(); // remove from front
void removeEnd(); // remove from end
void print(); // prints the SLL
private:
SNode* head; // pointer to the head of list
};
SLinkedList::SLinkedList()
{
head=0;
}
SLinkedList::~SLinkedList()
{
delete head;
}
bool SLinkedList::empty()
{
if(head==0)
return true;
else
return false;
}
string SLinkedList::front()
{
if(head)
return head->elem;
cout<<"\n\tLINKLIST IS EMPTY";
return "";
}
void SLinkedList::addFront(string e)
{
if(head==0)
head=new SNode(e);
else
head=new SNode(e,head);
}
void SLinkedList::addBack(string e)
{
if(head==0)
head=new SNode(e);
else
{
SNode *prev,*temp=head;
while(temp)
{
prev=temp;
temp=temp->next;
}
prev->next=new SNode(e);
}
}
void SLinkedList::removeFront()
{
if(!head)
cout<<"\n\tUNDERFLOW";
else
{
SNode *temp=head;
if(head->next==0)
{
head=0;
}
else
head=head->next;
delete temp;
}
}
void SLinkedList::removeEnd()
{
if(!head)
cout<<"\n\tUNDERFLOW";
else
{
if(head->next==0)
{
delete head;
head=0;
return;
}
SNode *prev,*temp=head;
while(temp->next)
{
prev=temp;
temp=temp->next;
}
delete temp;
prev->next=0;
}
}
void SLinkedList::print()
{
SNode *temp=head;
while(temp)
{
cout<<temp->elem<<" ";
temp=temp->next;
}
}
int main()
{
int choice,i,n;
string el;
char ch;
SLinkedList l;
do
{
clrscr();
cout<<"\n\n\tS.I.N.G.L.Y. L.I.N.K. L.I.S.T";
cout<<"\n\n\t-----------------------------";
cout<<"\n\n\t1.ADD TO HEAD";
cout<<"\n\n\t2.ADD TO TAIL";
cout<<"\n\n\t3.DELETE FROM HEAD";
cout<<"\n\n\t4.DELETE FROM TAIL";
cout<<"\n\n\t5.FRONT ELEMENT";
cout<<"\n\n\t6.IS LIST EMPTY";
cout<<"\n\n\t7.DISPLAY";
cout<<"\n\n\t13.EXIT";
cout<<"\n\n\tENTER YOUR CHOICE";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n\n\tENTER THE ELEMENT : ";
cin>>el;
//node n(el,0);
l.addFront(el);
l.print();
break;
case 2:cout<<"\n\n\tENTER THE ELEMENT : ";
cin>>el;
l.addBack(el);
l.print();
break;
case 3:l.removeFront();
l.print();
break;
case 4:l.removeEnd();
l.print();
break;
case 5:cout<<"\n\n\tFRONT ELEMENT:";
el=l.front();
cout<<el;
break;
case 6:if(l.empty())
cout<<"\n\tLINKLIST IS EMPTY";
else
cout<<"\n\tLINKLIST IS NOT EMPTY";
break;
case 7:l.print();
break;
default:cout<<"\n\tWRONG CHOICE!!";
break;
}
cout<<"\n\n\tDO U WANT TO CONTINUE?";
cin>>ch;
}while(ch=='Y'||ch=='y');
getch();
}