-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.cpp
More file actions
195 lines (177 loc) · 3.49 KB
/
6.cpp
File metadata and controls
195 lines (177 loc) · 3.49 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
// 6. Write a program to create a Linked List of n elements and do the following operaons:
// a) Insert at 1sst posion, last posion and nth posion. b) Delete at 1st posion, last posion and kth posion.
// c) Check a given element is present or not in Linked list.
#include<stdio.h>
#include<malloc.h>
struct node{
int data;
struct node* next;
};
struct node* head=NULL;
void print()
{
struct node* current=head;
while(current!=NULL)
{
printf("%d",current->data);
printf(" -> ");
current=current->next;
}
printf("NULL\n\n");
}
void insertatbeg(int x)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=head;
head=temp;
}
void insertatend(int x){
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=NULL;
if(head==NULL)
head=temp;
else
{
struct node* temp2=head;
while(temp2->next!=NULL)
temp2=temp2->next;
temp2->next=temp;
}
}
void insertn(int n,int value)
{ int i;
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
if(head==NULL)
{
temp->next=head;
head=temp;
}
struct node* temp3=head;
for(i=0;i<n-2;i++)
{
temp3=temp3->next;
}
temp->next=temp3->next;
temp3->next=temp;
}
void deletionfrombeg()
{
struct node* temp2=head;
if(head==NULL)
{
printf("Linked list is empty\n");
}
else
{
head=head->next;
}
}
void deletionfromk(int k)
{
int i;
struct node* temp3=head;
if(head==NULL)printf("linked list is empty.\n");
else if(k==1)head=head->next;
else
{
struct node* temp4=head;
for(i=0;i<k-2;i++)
temp4=temp4->next;
temp4->next=temp4->next->next;
}
}
void deletionfromend()
{ int i;
struct node* current=head;
if(head==NULL)
printf("linked list is empty\n");
else
{
struct node* t=head;
while(current->next!=NULL)
{
t=current;
current=current->next;
}
t->next=NULL;
}
}
void search(int x)
{
struct node* current=head;
while(current!=NULL)
{
if(current->data==x)
{
printf("element %d is present in the linked list\n\n",x);
return;
}
current=current->next;
}
printf("element %d is not present in linked list\n",x);
}
int main()
{ //initial linked list is
insertatend(9);
insertatend(11);
insertatend(12);
insertatend(13);
insertatend(14);
printf("initial linked list is:\n");
print();
int i,n,choice;
printf("-----------MENU-------------\n");
printf("1.Insert at first position\n");
printf("2.Insert at last position\n");
printf("3.Insert at nth position\n");
printf("4.Delete from front position\n");
printf("5.Delete from end position\n");
printf("6.Delete from nth position\n");
printf("7.Delete from front position\n");
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insertatbeg(1);
insertatbeg(2);
insertatbeg(3);
print();
break;
case 2:
insertatend(4);
insertatend(5);
insertatend(6);
print();
break;
case 3:
insertn(2,10);
insertn(2,15);
insertn(2,9);
print();
break;
case 4:
deletionfrombeg();
deletionfrombeg();
print();
break;
case 5:
deletionfromend();
deletionfromend();
print();
break;
case 6:
deletionfromk(2);
deletionfromk(1);
deletionfromk(1);
print();
break;
case 7:
search(5);
search(50);
return 0;
}
}