-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglylinked list implementation.c
109 lines (102 loc) · 2.61 KB
/
singlylinked list implementation.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
#include<stdio.h>
#include<malloc.h>
struct NODE
{
int item;
struct NODE *link;
} ;
typedef struct NODE node;
node *head=NULL;
node *newnode(int val)
{ node *p;
p=(node *)malloc(sizeof(node));
p->item=val;
p->link=NULL;
return p;
}
void displaylist(node *h)
{
node *curr;
curr=h;
while(curr!=NULL)
{ printf("%d", curr->item);
curr=curr->link;
if(curr !=NULL)
printf(" --> ");
}
}
void insertfirst(int val)
{ node *p;
p=newnode(val);
p->link=head;
head=p;
}
void insertbefore(int item1,int val)/*insert val after item1*/
{ node *curr=head,*prev=NULL,*p;
while(curr!=NULL && curr-> item != item1)
{prev=curr;curr=curr->link;}
if(curr==NULL)
printf("Item Not Found\n");
else
{ p=newnode(val);
p->link= curr;
if(curr==head)/* iserting before the first node */
head=p;
else
prev->link=p;
}
}
void insertafter(int item1,int val)/*insert val before item1*/
{ node *curr=head,*p;
while(curr!=NULL && curr-> item != item1)
{curr=curr->link;}
if(curr==NULL)
printf("Item Not Found\n");
else
{p=newnode(val);
p->link= curr->link;
curr->link=p;
}
}
void delete(int val)
{ node *curr=head,*prev=NULL,*p;
while(curr!=NULL && curr-> item != val)
{prev=curr;curr=curr->link;}
if(curr==NULL)
printf("Item Not Found\n");
else
{
if(curr==head) /* node to be deleted is the first node*/
head=curr->link;
else
prev->link=curr->link;
free(curr);
}
}
int main()
{
int ch,po,it;
do
{ printf("\n1. insert first\n2. Insert after\n3. insert before\n4. display \n5. Delete \n6. quit");
scanf("%d",&ch);
switch(ch)
{ case 1:printf("value to be inserted first:");
scanf("%d",&po); insertfirst(po);break;
case 2:printf("enter item after which an element is inserted ");
scanf("%d",&it);
printf("value to be inserted:");
scanf("%d",&po);insertafter(it,po);break;
case 3:printf("enter item before which an ele is inserted");
scanf("%d",&it);
printf("value to be inserted:");
scanf("%d",&po);
insertbefore(it,po);
break;
case 4:displaylist(head);break;
case 5:printf("value to be Deleted:");
scanf("%d",&po);delete(po);break;
case 6:break;
default:printf("Invalid choice");
}
}while(ch!=6);
}