-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLINK
194 lines (182 loc) · 3.46 KB
/
SLINK
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* head=NULL;
void insert();
void delet();
void display();
void insertfront(int);
void insertposition(int);
void insertlast(int);
void deletefront();
void deleteposition();
void deletelast();
int main()
{
int choice;
while(1)
{
printf("\n1.insert \n2.delete \n3.display \n4.exit\n");
printf("enter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);
default : printf("!!invalid option!!");
}
}
return(0);
}
void insert()
{
int option,data;
printf("enter the data: ");
scanf("%d",&data);
printf("\n1.insert at front \n2.insert at position \n3.insert at last\n");
printf("enter the option: \n");
scanf("%d",&option);
switch(option)
{
case 1: insertfront(data);
break;
case 2: insertposition(data);
break;
case 3: insertlast(data);
break;
default:printf("invalid option!!");
}
}
void insertfront(int data)
{
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->link=head;
newnode->data=data;
head=newnode;
}
void insertposition(int data)
{
int i,n;
struct node* newnode,* temp=head;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the position: ");
scanf("%d",&n);
for(i=0;i<n-2;i++)
{
temp=temp->link;
}
newnode->data=data;
newnode->link=temp->link;
temp->link=newnode;
}
void insertlast(int data)
{
struct node* newnode,* temp=head;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->link=NULL;
newnode->data=data;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=newnode;
}
void delet()
{
int option;
if(head==NULL)
{
printf("!!!LIST IS EMPTY!!!\n");
}
else
{
printf("\n1.delete from front \n2.delete from position \n3.delete fom last\n");
printf("Enter the option: ");
scanf("%d",&option);
switch(option)
{
case 1: deletefront();
break;
case 2: deleteposition();
break;
case 3: deletelast();
break;
default:printf("invalid option");
}
}
}
void deletefront()
{
struct node*temp=head;
head=head->link;
printf("deleted number is %d",temp->data);
free(temp);
}
void deleteposition()
{
int i,n;
struct node*temp=head;
struct node*ptr=head;
printf("enter position: ");
scanf("%d",&n);
for(i=1;i<n-2;i++)
{
temp=temp->link;
}
if(temp->link==NULL)
{
printf("list less than the position");
return;
}
else{
for(i=1;i<n-1;i++)
{
ptr=ptr->link;
}
temp->link=ptr->link;
printf("deleted number is %d",ptr->data);
free(ptr);
}
}
void deletelast()
{
struct node*temp=head;
struct node*ptr=head;
while(temp->link!=NULL)
{
temp=temp->link;
}
while(ptr->link->link!=NULL)
{
ptr=ptr->link;
}
printf("deleted element is %d",temp->data);
free(temp);
ptr->link=NULL;
}
void display()
{
struct node*tmp;
if(head==NULL)
{
printf("LIST IS EMPTY\n");
}
tmp=head;
printf("THE LIST IS:");
while(tmp!=NULL)
{
printf("%d",tmp->data);
tmp=tmp->link;
}
printf("\n");
}