-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab3(a)Q3.cpp
More file actions
170 lines (164 loc) · 3.08 KB
/
Lab3(a)Q3.cpp
File metadata and controls
170 lines (164 loc) · 3.08 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
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node*next;
Node(int d) {
data=d;
next=NULL;
}
};
class singlyLL {
public:
Node*head=NULL;
Node*tail=NULL;
void insertattail(int v) {
Node*temp=new Node(v);
if(head==NULL) {
head=temp;
tail=temp;
tail->next=head;
} else {
tail->next=temp;
tail=temp;
temp->next=head;
}
}
void display() {
Node*temp=head;
do {
cout<<temp->data<<"->";
temp=temp->next;
} while(temp!=head);
cout<<endl;
}
void insertathead(int v) {
Node*temp=new Node(v);
if(head==NULL) {
head=temp;
tail=temp;
temp->next=head;
} else {
temp->next=head;
head=temp;
tail->next=head;
}
}
void insertatmiddle(int pos,int val) {
int cnt=1;
Node*prev=NULL;
Node*curr=head;
if(head==NULL) {
cout<<"List is empty"<<endl;
return;
}
do {
curr=curr->next;
cnt++;
} while(curr!=head);
if(pos>cnt||pos<1) {
cout<<"Invalid Position"<<endl;
return;
}
cnt=1;
curr=head;
if(pos==1) {
insertathead(val);
return;
}
while(cnt<pos) {
if(curr!=NULL) {
prev=curr;
curr=curr->next;
cnt++;
}
}
Node*newnode=new Node(val);
prev->next=newnode;
newnode->next=curr;
if(curr==head) {
tail=newnode;
}
tail->next=head;
}
void Deletion(int pos) {
int cnt=1;
Node*curr=head;
Node*prev=NULL;
if(head==NULL){
cout<<"LIST IS EMPTY"<<endl;
return;
}
if(pos==1) {
if(head==tail){
head=NULL;
tail=NULL;
cout<<"Deleted"<<endl;
}else{
head=head->next;
curr->next=NULL;
tail->next=head;
}
delete curr;
return;
}
do {
prev=curr;
curr=curr->next;
cnt++;
} while(curr!=head&&cnt<pos);
if(pos>cnt||curr==head){
cout<<"OUT OF BOUND KINDLY PROVIDE CORRECT POSITION TO DELETE"<<endl;
return;
}
Node*temp=NULL;
temp=curr->next;
prev->next=temp;
if(curr==tail){
tail=prev;
}
curr->next=NULL;
delete curr;
}
};
int main() {
cout<<"DEMONSTRATING WHOLE CODE"<<endl;
singlyLL l1;
l1.insertattail(1);
l1.insertattail(2);
l1.insertattail(3);
l1.insertattail(4);
l1.insertattail(5);
cout<<endl;
l1.insertatmiddle(6,7);
l1.insertatmiddle(7,8);//so basically insertatmiddle function is also insert at any postion
l1.insertatmiddle(1,5);//we can use insertatmidle to insert at head,tail,middle
l1.insertatmiddle(11,15);
l1.insertattail(10);
cout<<"head->"<<l1.head->data<<endl;//checking head and tail
cout<<"tail->"<<l1.tail->data<<endl;
l1.display();
l1.Deletion(1);
l1.display();
l1.Deletion(2);
l1.display();
l1.Deletion(4);
l1.display();
l1.Deletion(5);
l1.display();
l1.Deletion(6);
l1.display();
l1.Deletion(1);
l1.display();
cout<<"head->"<<l1.head->data<<endl;//checking head and tail
cout<<"tail->"<<l1.tail->data<<endl;
l1.Deletion(4);
l1.display();
cout<<"head->"<<l1.head->data<<endl;//checking head and tail
cout<<"tail->"<<l1.tail->data<<endl;
l1.insertatmiddle(3,10);
l1.display();
l1.Deletion(3);
l1.display();
}