-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLL_implementation.cpp
More file actions
160 lines (160 loc) · 3.97 KB
/
SLL_implementation.cpp
File metadata and controls
160 lines (160 loc) · 3.97 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
#include<iostream>
using namespace std;
struct node{
int data;
node* link;
};
struct list{
int count;
node* head;
};
void createList(list* &);
bool search(list* , node* & ,node *& ,int);
void insert(list*&,node* &,int);
void display(list *);
void deleteNode(list *& , node*& ,node*& , int&);
bool retrieveNode(list* , int ,int&);
bool EmptyList(list* );
int ListCount(list* );
void destroyList(list*& );
int main(){
list* sll =NULL;
node* ppre;
node* ploc;
int o;
bool f;
do{
cout<<"1.Create list, 2.Insert node, 3.search, 4.display, 5.deleteNode, 6.retriveNode, 7.Is empty, 8.List count, 9.Destroy List\n";
cout<<"Enter the option:";
cin>>o;
int x,r;
switch(o){
case 1:
createList(sll);
break;
case 2:
cout<<"enter the element you want to insert: ";
cin>>x;
f = search(sll , ppre ,ploc ,x);
insert(sll, ppre , x);
break;
case 3:
cout<<"Enter the elemnt you want to search: ";
cin>>x;
f = search(sll , ppre ,ploc ,x);
if(f) cout<<"Element found!\n";
else cout<<"Element not found!\n";
break;
case 4:
display(sll);
break;
case 5:
cout<<"enter the element you want to delete";
cin>>x;
if(search(sll ,ppre , ploc ,x)){
deleteNode(sll , ppre,ploc,r);
cout<<"element deleted is : "<<r<<endl;
}
else{
cout<<"element not present cannot be deleted!"<<endl;
}
break;
case 6:
cout<<"enter the elemnt you want to retrive: ";
cin>>x;
retrieveNode(sll , x, r);
cout<<"After retrieving: "<<r<<endl;
break;
case 7:
if(EmptyList(sll)) cout<<"The list i empty!\n";
else cout<<"The list is not empty\n";
break;
case 8 :
cout<<"No.of nodes in the list : "<<ListCount(sll)<<endl;
break;
case 9 :
destroyList(sll);
cout<<"List Destroyed!\n";
break;
}
}while(o<=9);
}
void createList(list* &ref){
ref = new list;
ref->head = NULL;
ref->count =0;
}
bool search(list *s , node*& ppre ,node*& ploc,int target){
bool found;
ppre =NULL;
ploc = s->head;
while(ploc!=NULL && target> ploc->data){
ppre = ploc;
ploc= ploc->link;
}
if(ploc==NULL)
found = false;
else{
if(ploc->data == target)
found = true;
else
found = false;
}
return found;
}
void insert(list*& s , node*& ppre ,int dataIn){
node* temp = new node;
temp->data = dataIn;
if(ppre==NULL){
temp->link = s->head;
s->head = temp;
}
else{
temp->link = ppre->link;
ppre->link = temp;
}
s->count++;
}
void display(list *s){
node* temp = s->head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->link;
}
cout<<endl;
}
void deleteNode(list *& s , node*& ppre ,node*& ploc, int& dataOut){
dataOut = ploc->data;
if(ppre==NULL)
s->head = ploc->link;
else
ppre->link = ploc->link;
delete ploc;
s->count--;
}
bool retrieveNode(list* s, int key ,int& dataOut){
node* ppre;
node* ploc;
bool fo = search(s, ppre , ploc , key);
if(fo)
dataOut = ploc->data;
return fo;
}
bool EmptyList(list* s){
if(s->count==0) return true;
else return false;
}
int ListCount(list* s){
return s->count;
}
void destroyList(list*& s){
node* t1 = s->head;
node* t2 = NULL;
while(t1!=NULL){
t2 = t1->link;
delete t1;
t1 = t2;
}
s->count = 0;
s->head = NULL;
}