-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink list.cpp
221 lines (215 loc) · 8.16 KB
/
link list.cpp
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
//Actually I'm the greatest Archisman Bhattacharjee
//Roll-1711031
#include<iostream>
using namespace std;
int count=0;
class node{ // I'm trying to define node class
public:
int data; //I am sure here the data hold the data of every elements in linked list
node *next;//Basically "next" stores the address of previous node
};
class l_list{
node *head; //Here "head" store the address of the first element of the linked list
node *tail; //"tail" stores the address of the last element of the linked list
public:
l_list(){ //constructor to make the head and tail of the linklist null(they point nothing )
head=NULL;
tail=NULL;
}
//I'm Archisman Bhattacharjee. I put my name in the middle of the program because if someone wants tomcopy from me be I will fuck him!
void insert(int data); //to insert the data at the end of the linked list
void insert_at(int data , int pos);// to insert the data of the linked list in a certain position
void delt(); //to delete the last element
void delete_at(int pos); //to delete the element at a certain position
void show(); //to display the element
};
//I'm Archisman Bhattacharjee. I put my name in the middle of the program because if someone wants tomcopy from me I will fuck him.
int main(){
char chc='a';
l_list ll; //defining a linked list named ll
while(chc!='q'){ //this looop will run until you enter q when asked
cout<<endl<<endl;
cout<<"-----------------------------------------------------------------------"<<endl;
cout<<"This is a program which store data using link list."<<endl;
cout<<"-----------------------------------------------------------------------"<<endl;
cout<<"a. Prepare link list"<<endl;
cout<<"b. Add multiple element at the end of the link list "<<endl;
cout<<"c. Add element by positions "<<endl;
cout<<"d. Remove the last element of the linked list "<<endl;
cout<<"e. Remove the element by its position "<<endl;
cout<<"f. Show the linked list"<<endl;
cout<<"g. Show the number of elements"<<endl;
cout<<" Enter q to exit "<<endl;
cout<<"-----------------------------------------------------------------------"<<endl;
cout<<endl<<endl;
cout<<"Your choice: "; //user friendly representation
cin>>chc; //get a choice
if (chc=='a'){
cout<<"Created."<<endl;
}
//I'm Archisman Bhattacharjee. I put my name in the middle of the program because if someone wants tomcopy from me he will confirmly be recognised.
if (chc=='b'){//program to insert the data at the end
cout<<"How many data you have "<<endl;
int num ;
cin>>num;
for(int i=0; i<num; i++){
cout<<"Data"<<i+1<<" ";
int data;
cin>>data;//taking input as data
ll.insert(data); //inserting the data by calling insert function
}
}
if(chc=='c'){ //program to insert at a certain position
cout<<"Add data in a certain position"<<endl;
cout<<"How many babies do you have ?"<<endl;
int num;
cin>>num;
int data,pos;
for (int i=0;i<num; i++){ //taking data repeatedly
cout<<"Data: ";
cin>>data;
cout<<"position: ";
cin>>pos;
ll.insert_at(data, pos); //inserting data by calling insert_at function
cout<<"New linked list"<<endl;
ll.show(); // showing the modified link list by calling show function
}
}
if (chc=='d'){ //program to delete the last element
cout<<"I dont want to delete the last element of this linked list"<<endl;
cout<<"If yes please enter y"<<endl;
char choice='n';
cin>>choice;
if (choice =='y' || choice =='Y'){
ll.delt();// delete the last element of ll by calling delt function
cout<<"New linked list"<<endl;
ll.show(); // again showing the link list
}
}
//program to delete elements of linked list at a certain position
if(chc=='e'){
cout<<"How many data you want to delete : "<<endl;
int num,pos;
cin>>num;
for(int i=0; i<num;i++ ){
cout<<"Enter the position of the data "<<endl;
cin>>pos;
ll.delete_at(pos); ///deleting by calling delete_at function
cout<<"New linked list "<<endl;
ll.show();
}
}
//program to show the linked list
if(chc=='f'){
cout<<"Elements of the linked list"<<endl;
ll.show();// showing the linked list by calling show function
}
if (chc=='g'){
cout<<"The number of node in this linked list is : "<<count<<endl;
}
if(chc=='q'){
break;
}
char foo;
cout<<endl<<"Enter any key and press |[Enter]|"<<endl;
cin>>foo;
}
return 0;
}
//I'm Archisman Bhattacharjee. I put my name in the middle of the program because if someone wants tomcopy from me I will fuck him.
//insert function to insert a new element at the end of the list
void l_list::insert(int data){
node* temp = new node; // declaring a new object of type node
temp->data=data;// inserting the data at the objectk
if (head == NULL){ //if the list is empty
head=temp; // head will point temp
tail =temp; //tail will point temp
temp->next=NULL; //making next of temp to NULL
}
else {
tail->next= temp; //making link with new object to tail
temp->next=NULL; //making the next of new element to NULL
tail = temp; //pointing tail to the new element
}
count++; //incrementing the count as a new element is added
}
//I'm Archisman Bhattacharjee. I put my name in the middle of the program because if someone wants tomcopy from me he will confirmly be recognised.
void l_list::insert_at(int data, int pos){
if(pos<=count+1){
node* pos1; // pointer to point the previous of nth element where n=pos
pos1=head;
node* pos2; //point to point the nth element
pos2=NULL; // making the pos2 to be null
node *temp; //declaring a new object which is to be added to the link
temp->data=data; // inserting data to the newly created node
for (int i = 2; i<pos; i++ ){
pos1= pos1->next; //this loop is to point the pos1 to the n-1 th node
}
pos2= pos1->next; //pointing nth node
pos1->next =temp; //adding the new node at the end of n-1th node, now new node is nth node
temp->next=pos2; //linking new node with the previous nth node
count++; //because new element gets added
}
else cout<<"YOU DON'T HAVE THAT MUCH NUMBER OF ELEMENT."<<endl;
}
void l_list::delt(){
node* temp=tail; //declaring a pointer to point the element which has to be deleted
if(count>1){ //or if there is more than one element in the list
tail=head;
for (int i=2; i<count; i++){
tail=tail->next;
}
tail->next=NULL;
delete temp; //I'm trying to delete temp
count--; //decrementing count as one item has been deleted
}
else if(count==1){
head=NULL; //making the link list empty
tail=NULL;
delete temp; //deleting the temp or the last object
count --;
}
else cout<<"There is nothing to delete."<<endl;
}
void l_list::delete_at(int pos){
node* temp=head;
if (pos<=count && count!=1 && pos!=1){ //if we are not adding element in a linked list containing more than one element and position of the insertion is not 1
node* previous=head;
for (int i=2; i<pos; i++){
previous=previous->next; //this loop is to reach the nth node wher n-1 =pos
}
temp =previous->next; //pointing temp to the nt element
previous->next=temp->next; //linking the n-1th element to n+1th element to break the link with temp
delete temp; //deleting temp
count --; //decrement count as one item deleted;
}
else if (pos==1 && count >1){
head =head->next;
delete temp;
count --;
}
else if(count==1){ //if there is only one node at the linked list
node* temp; //declaring pointer to point the element ought to be deleted
head =NULL; //making the link empty
temp = tail; //pointing the temp to tail because to delete the remaining object we need its address
tail=NULL;
delete temp;//deleted object
count--;
}
else cout<<"You don't have "<<pos<<"th element in linked list."<<endl;
}
//I'm Archisman Bhattacharjee. I put my name in the middle of the program because if someone wants tomcopy from me he will confirmly be recognised.
void l_list::show(){
node* temp=head; //declaring a new pointer to point the nodes which have to be shown
if(head== NULL){ //if the link list is empty
cout<<"Oops..Nothing is here to print."<<endl;
}
else{
while(temp!=NULL){ //untill reach the last element// the last element's next is NULL
cout<<temp->data<<" -> "; //showing the data of the nodes as mentioned
temp=temp->next; //proceeding to next element
}
cout<<endl;
}
}
//Wow, Finally I finished. I cant believe how I patiently copied 221 lines of code! Terrible!