-
Notifications
You must be signed in to change notification settings - Fork 1
/
singly.c
191 lines (178 loc) · 2.98 KB
/
singly.c
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
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
} *head=NULL;
void insertf();
void insertr();
void deletef();
void deleter();
void insertran();
void deleteran();
void display();
int search();
void main()
{
int x;
loop:
printf("\n\nSelect an operation :\n1. insert(front)\n2. insert(rear)\n3. delete(front)\n4. delete(rear)\n5. insert(specified position)\n6. delete(specified position)\n7. display\n8. search\n");
scanf("%d",&x);
switch (x)
{
case 1: insertf();
goto loop;
case 2: insertr();
goto loop;
case 3: deletef();
goto loop;
case 4: deleter();
goto loop;
case 5: insertran();
goto loop;
case 6: deleteran();
goto loop;
case 7: display();
goto loop;
case 8: x=search();
goto loop;
default : printf("Wrong selection, try again\n");
goto loop;
}
}
//Functions
void insertf()
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
printf("Enter the element to be inserted\n");
scanf("%d",&newNode->data);
newNode->link = head;
head = newNode;
}
void insertr()
{
struct node *newNode,*temp = head;
newNode = malloc(sizeof(struct node));
printf("Enter the element to be inserted\n");
scanf("%d",&newNode->data);
if(head==NULL)
{
newNode->link = head;
head = newNode;
}
else
{
while(temp->link != NULL)
temp = temp->link;
temp->link = newNode;
newNode->link = NULL;
}
}
void deletef()
{
struct node *temp;
if(head == NULL)
{
printf("list is empty\n");
}
else
{
printf("Element %d has been deleted\n",head->data);
head = head->link;
}
}
void deleter()
{
struct node *temp = head,*prev;
if(head == NULL)
{
printf("list is empty\n");
}
else
{
while(temp->link != NULL)
{
prev = temp;
temp = temp->link;
}
if(temp->link == NULL)
{
printf("Element %d has been deleted\n",head->data);
head=head->link;
}
else
{
printf("Element %d has been deleted\n",temp->data);
prev->link = temp->link;
}
}
}
void insertran()
{
struct node *newNode,*prev,*temp = head;
newNode = malloc(sizeof(struct node));
printf("Enter the element to be inserted\n");
scanf("%d",&newNode->data);
int a,i;
printf("Enter the position\n");
scanf("%d",&a);
for(i=0;i<a-1;i++)
{
prev = temp;
temp = temp->link;
}
prev->link = newNode;
newNode->link = temp;
printf("Element %d has been inserted at position %d.\n",newNode->data,a);
}
void deleteran()
{
struct node *next,*prev,*temp = head;
int a,i;
printf("Enter the position\n");
scanf("%d",&a);
for(i=0;i<a-1;i++)
{
prev = temp;
temp = temp->link;
}
prev->link = temp->link;
printf("Element %d has been deleted from position %d.\n",temp->data,a);
free(temp);
}
void display()
{
struct node *temp = head;
if(head == NULL)
{
printf("list is empty\n");
}
else
{
printf("\n\nList elements are : \n");
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->link;
}
}
}
int search()
{
struct node *temp = head;
int a,i;
printf("Enter the item to be searched\n");
scanf("%d",&a);
for (i=0;temp != NULL;i++)
{
if(temp->data == a)
{
printf("Element %d has been found at position %d.\n",temp->data,i+1);
return 1;
}
temp = temp->link;
}
printf("Element not found.\n");
return 0;
}