Repository navigation
Expand file tree
/
Copy pathSourceCode.cpp
More file actions
211 lines (190 loc) · 4.81 KB
/
Copy pathSourceCode.cpp
File metadata and controls
211 lines (190 loc) · 4.81 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
/*
Write C++ program to give implementation of singly linked list and should have the following
functionality.
-InsertFirst ( ? )
-InsertLast ( ? )
-InsertSpecific ( ? )
-DisplayList ( ? )
-DisplaySize ( ? )
-DeleteFirst ( ? )
-DeleteLast ( ? )
-DeleteSpecific( ? )
*/
#include<iostream>
#include<string>
using namespace std;
struct List {
int data; List* next;
};
List* head = NULL;
List* tail = NULL;
int count = 0, listnumber = 0;
void InsertFirst();
void InsertLast();
int InsertSpecific();
void DisplayList();
void DisplaySize();
void DeleteFirst();
void DeleteLast();
void DeleteSpecific();
int main() {
int choice = 0; bool in, run = true; int tterminate = 0;
do {
do {
system("cls");
cout << "Please Select any of the followings: " << endl;
cout << "1 for Insert at First " << endl;
cout << "2 for Insert at Last " << endl;
cout << "3 for Insert at Specific " << endl;
cout << "4 for Display List " << endl;
cout << "5 for Display Size " << endl;
cout << "6 for Delete First(" << endl;
cout << "7 for Delete Last " << endl;
cout << "8 for Delete Specific " << endl;
cin >> choice;
if (choice < 1 || choice>8)
{
cout << " invalid choice enter again";
in = true;
}
else in = false;
system("cls");
} while (in);
switch (choice)
{
case 1: InsertFirst(); break;
case 2: InsertLast(); break;
case 3: InsertSpecific(); break;
case 4: DisplayList(); break;
case 5: DisplaySize(); break;
case 6: DeleteFirst(); break;
case 7: DeleteLast(); break;
case 8: DeleteSpecific() ; break;
}
cout << endl;
cout << "Do you want to perform another function?: (Enter 0 for yes)(any number for not): "; cin >> tterminate;
if (tterminate == 0)
run = true;
else run = false;
} while (run);
return 0;
}
void InsertFirst() {
if (head == NULL)
{
List* temp = new List;
cout << "Enter the data: "; cin >> temp->data; temp->next = NULL;
head = temp;
tail = temp;
cout << "Value is Entered Sucessfully ";
listnumber++;
}
else if (head != NULL) {
List* temp = new List;
cout << "Enter the data: "; cin >> temp->data;
temp->next = head;
head = temp;
cout << "Value is entered sucessfuly ";
listnumber++;
}
}
void InsertLast() {
if (tail == NULL)
{
List* temp = new List;
cout << "Enter the data: "; cin >> temp->data; temp->next = NULL;
tail = temp;
head = temp;
cout << "List is inserted Sucessfully " << endl;
listnumber++;
}
else if (tail != NULL) {
List* temp = new List;
tail->next = temp;
tail = temp;
cout << "Enter value: "; cin >> tail->data;
tail->next = NULL;
cout << "Value is inserted sucessfuly " << endl;
listnumber++;
}
}
int InsertSpecific() {
int n = listnumber;
cout << "the total number of lists are: " << listnumber << endl;
do {
cout << "Enter after which list you want to insert the list: "; cin >> n;
if (n > listnumber) { cout << "please select the correct value max is: " << listnumber; system("cls"); }
} while (n > listnumber);
List* temp = new List;
int count;
temp = head;
count = 1;
for (int i = 1; i < n-1; i++)
{
temp = temp->next;
count = i;
}
List* temp1 = new List;
temp1->next = temp->next;//3rd
cout << "Enter value to inserted at " << count + 1 << endl;
cin >> temp1->data;
temp->next = temp1;
listnumber++;
return 0;
}
void DisplayList() {
if (head == NULL && listnumber == 0) cout << "Linklist is empty: ";
List* temp = new List;
temp = head;
for (int i = 1; i <= listnumber; i++)
{
cout << "The value of List " << i << " is: " << temp->data << endl;
temp = temp->next;
}
temp = NULL;
delete temp;
}
void DisplaySize() {
cout << "The total number of lists are: " << listnumber << endl;
}
void DeleteFirst() {
if (head == NULL && listnumber == 0) cout << "Linklist is empty: ";
List* temp = new List;
temp = head;
head = head->next;
temp->next = NULL;
temp->data = 0;
delete temp;
listnumber--;
}
void DeleteLast() {
if (head == NULL&&listnumber==0) cout << "Linklist is empty: ";
List* temp = new List;
temp = head;
for (int i = 1; i < listnumber; i++)
{
temp = temp->next;
}
if (temp->next == NULL)
delete temp;
else cout << "Wronge node it was....";
cout << "total number of nodes are: " << listnumber << endl;
listnumber--;
cout << "Deleted the last one now the total nodes are " << listnumber << endl;
}
void DeleteSpecific() {
if (head == NULL && listnumber == 0) cout << "Linklist is empty: ";
List* temp = new List; int n;
temp = head; List* tempr;
cout << "The total number of nodes are:" << listnumber << endl;
do {
cout << "Enter which not you want to delete: "; cin >> n;
if (n > listnumber || n < 0)cout << "Please enter correct node max are " << listnumber << endl;
} while (n > listnumber || n < 0);
for (int i = 1; i < n - 1; i++)
temp = temp->next;
tempr=temp->next;
temp->next = temp->next->next;
delete tempr;
listnumber--;
}