-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.1.cpp
More file actions
149 lines (149 loc) · 3.82 KB
/
8.1.cpp
File metadata and controls
149 lines (149 loc) · 3.82 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
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *start = NULL;
int n;
int insert_end(int data);
int insert_begin(int data);
int insert_after(int data);
int delete_begin();
int delete_at();
int delete_end();
void display();
int main()
{
int c=-1,data,ch,r;
while(c!=0){
cout<<"0.exit\n1.Insertion\n2.Deletion\n3.Display"<<endl;
cin>>c;
switch(c){
case 1 : cout<<"1.Insertion at Begining\n2.Insertion after an element\n3.Insertion at End"<<endl;
cin>>ch;
cout<<"enter the data"<<endl;
cin>>data;
if(ch==1)
insert_begin(data);
else if(ch==2){
r = insert_after(data);
if(r==-1) cout<<"search element is not in the list"<<endl;}
else if(ch==3)
insert_end(data);
break;
case 2 : if(start==NULL)
cout<<"linked list is empty"<<endl;
else if(start->next==NULL)
start = NULL;
else{
cout<<"1.Deletion at Begining\n2.Deletion of an specific element\n3.Deletion at End"<<endl;
cin>>ch;
if(ch==1)
delete_begin();
else if(ch==2){
r = delete_at();
if(r==-1) cout<<"search element is not in the list"<<endl;}
else if(ch==3)
delete_end();
}
break;
case 3 : if(start==NULL)
cout<<"linked list is empty"<<endl;
else
display();
break;
default : if(c!=0) cout<<"enter correct choice"<<endl;
}
}
free(start);
return 0;
}
int insert_begin(int data)
{
struct node *t;
t = (struct node*)malloc(sizeof(struct node));
t->data=data;
t->next = start;
start = t;
return 0;
}
int insert_after(int data)
{
int element;
if(start!=NULL){
cout<<"enter the element you want to insert it new data after it"<<endl;
cin>>element;
struct node *temp,*t;
t = (struct node*)malloc(sizeof(struct node));
t->data=data;
temp = start;
while(element!=temp->data){
if(temp->next == NULL)
return -1;
temp = temp->next;
}
t->next = temp->next;
temp->next = t;
}
else
insert_begin(data);
}
int insert_end(int data)
{
struct node *temp,*t;
temp = start;
t = (struct node*)malloc(sizeof(struct node));
t->data=data;
if(start==NULL){
start=t;
start->next = NULL;
return 0;
}
while (temp->next != NULL)
temp = temp->next;
temp->next = t;
t->next = NULL;
return 0;
}
int delete_begin(){
start = start->next;
}
int delete_at(){
int element;
cout<<"enter the element you want to delete."<<endl;
cin>>element;
struct node *temp,*t;
temp = start;
if(start->data==element)
{
start = start->next;
return 0;
}
while(element!=temp->data){
if(temp->next == NULL)
return -1;
t = temp;
temp = temp->next;
}
t->next = temp->next;
}
int delete_end()
{
struct node *temp,*t;
temp=start;
while(temp->next!=NULL){
t = temp;
temp = temp->next;
}
t->next = NULL;
}
void display(){
struct node *temp;
temp = start;
while(temp->next != NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data<<endl;
}