-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLL.cpp
More file actions
113 lines (99 loc) · 1.95 KB
/
Copy pathSLL.cpp
File metadata and controls
113 lines (99 loc) · 1.95 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
#include<iostream>
#include<string.h>
#include<cstddef>
using namespace std;
class StringNode{
private:
string elem;
StringNode* next;
friend class StringLinkedList;
};
class StringLinkedList{
public:
StringLinkedList();
~StringLinkedList();
bool empty() const;
const string& front() const;
void addFront(const string& e);
void removeFront();
void showlist();
private:
StringNode* head;
};
StringLinkedList::StringLinkedList(){
head=NULL;
}
StringLinkedList::~StringLinkedList(){
while (!empty()) {
removeFront();
}
}
bool StringLinkedList::empty() const{
return head==NULL;
}
const string& StringLinkedList::front() const{
return head->elem;
}
void StringLinkedList::addFront(const string& e){
StringNode* v=new StringNode;
v->elem = e;
v->next = NULL;
if(head==NULL){
head=v;
}else{
v->next=head;
head=v;
}
}
void StringLinkedList::removeFront(){
StringNode* old = head;
head=old->next;
delete old;
}
void StringLinkedList::showlist(){
StringNode* p = head;
if(!p){
cout<<"NO NODE !!! list empty !! returning...";
return;
}
cout<<"head";
while(p->next!=NULL){
cout<<" -> "<<p->elem;
p=p->next;
}
cout<<" -> "<<p->elem;
}
int main(){
cout<<"Lnked list initialisation....."<<endl;
StringLinkedList list;
cout<<"Enter value for head : ";
string ele;
cin>>ele;
list.addFront(ele);
int choice;
while(choice!=4){
cout<<"\n1.) add element in list"<<endl;
cout<<"2.) remove element from list"<<endl;
cout<<"3.) show list"<<endl;
cout<<"4.) quit"<<endl;
cout<<"\nEnter your choice : ";
cin>>choice;
switch (choice) {
case 1:
cout<<"Enter element : ";
cin>>ele;
list.addFront(ele);
break;
case 2:
list.removeFront();
break;
case 3:
list.showlist();
break;
case 4:
cout<<"Exit\n"; break;
default:
cout<<"Enter correct choice!!!!";
}
}
}