-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSLL.cpp
More file actions
223 lines (198 loc) · 4.53 KB
/
SLL.cpp
File metadata and controls
223 lines (198 loc) · 4.53 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
216
217
218
219
220
221
222
#include<iostream>
using namespace std;
template <typename NT>
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:
NT elem;
SNode* next;
template <typename T>
friend class SLinkedList; // provides SLinkedList access to SNode
};
template <typename T>
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
SNode<T>* getNode(T e); // creates a new SNode and returns its pointer
const T input(); // takes an input of type T
bool empty() const; // is list empty?
const T front() const; // get front element
void addFront(const T e); // add to front of list
void addBack(const T e); // add to back of list
void removeFront(); // remove from front
void removeEnd(); // remove from end
void print(); // prints the SLL
private:
SNode<T>* head; // pointer to the head of list
};
template <typename T>
SLinkedList<T>::SLinkedList(){
/*
Objective: To construct an object of class SLinkedList and initialize head with NULL.
Input: None
Output: None
Return value: None
*/
head = NULL;
}
template <typename T>
SLinkedList<T>::~SLinkedList(){
/*
Objective: To destruct the object of class SLinkedList.
Input: None
Output: None
Return value: None
*/
SNode<T>* curr = head;
while(curr != NULL){
SNode<T>* next = curr->next;
delete curr;
curr = next;
}
head = NULL;
}
template <typename T>
SNode<T>* SLinkedList<T>::getNode(T e){
/*
Objective: To create a new object of class SNode, initialize its members and return its pointer.
Input: A string
Output: None
Return value: A Pointer to the object of class SNode.
*/
SNode<T>* temp = new SNode<T>;
temp->elem = e;
temp->next = NULL;
return temp;
}
template <typename T>
const T SLinkedList<T>::input(){
T ele;
cin>>ele;
return ele;
}
template <typename T>
bool SLinkedList<T>::empty() const{
/*
Objective: To test whether the list is empty.
Input: None
Output: None
Return value: true, if list is empty
false, if list is not empty.
*/
if(head==NULL)
return true;
else
return false;
}
template <typename T>
const T SLinkedList<T>::front() const{
/*
Objective: To return the front element of the list.
Input: None
Output: None
Return value: element at the front, if list is not empty
NULL, if list is empty
*/
if(!empty())
return head->elem;
}
template <typename T>
void SLinkedList<T>::addFront(const T e){
/*
Objective: To insert a node at the front of list.
Input: e -> The element to be inserted.
Output: None
Return value: None
*/
SNode<T>* temp = getNode(e);
if(head == NULL)
head = temp;
else{
temp->next = head;
head = temp;
}
}
template <typename T>
void SLinkedList<T>::addBack(const T e){
/*
Objective: To insert a node at the end of list.
Input: e -> The element to be inserted.
Output: None
Return value: None
*/
SNode<T>* temp = getNode(e);
if(head == NULL)
head = temp;
else{
SNode<T>* t = head;
while(t->next != NULL)
t = t->next;
t->next = temp;
}
}
template <typename T>
void SLinkedList<T>::removeFront(){
/*
Objective: To remove a node from the front of list.
Input: None
Output: None
Return value: None
*/
if(!empty()){
SNode<T>* temp = head;
head = head->next;
delete temp;
}
}
template <typename T>
void SLinkedList<T>::removeEnd(){
/*
Objective: To remove a node from the end of list.
Input: None
Output: None
Return value: None
*/
if(!empty()){
SNode<T>* curr = head;
if(head->next == NULL)
head = NULL;
else{
SNode<T>* prev = curr;
while(curr->next != NULL){
prev = curr;
curr = curr->next;
}
prev->next = NULL;
}
delete curr;
}
}
template <typename T>
void SLinkedList<T>::print(){
/*
Objective: To display the queue
Input: None
Output: Queue data
Return value: None
*/
SNode<T>* curr = head;
while(curr != NULL){
cout<<curr->elem<<"\t";
curr = curr->next;
}
}