-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglelinklist.c
117 lines (109 loc) · 2.56 KB
/
singlelinklist.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
#include<stdio.h>
#include<malloc.h>
struct NODE
{
int info;
struct NODE *link;
} ;
typedef struct NODE node;
node *head=NULL;
node *newnode()
{ node *p;
p=(node *)malloc(sizeof(node));
}
void displaylist(node *h)
{
node *curr;
curr=h;
while(curr!=NULL)
{ printf("%d", curr->item);
curr=curr->link;
if(curr !=NULL)
printf(" --> ");
}
}
void insertf(int item)
{ node *p;
p=newnode();
p->info=item;
p->link=head;
head=p;
}
void insertBF(int item,int a)
{ 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();
p->item=val;
p->link= curr;
if(curr==head)
head=p;
else
prev->link=p;
}
}
void insertafter(int item1,int val)
{ node *curr=head,*p;
while(curr!=NULL && curr-> item != item1)
{curr=curr->link;}
if(curr==NULL)
printf("Item Not Found\n");
else
{p=newnode();
p->item=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)
head=curr->link;
else
prev->link=curr->link;
free(curr);
}
}
int main()
{
int o,a,item;
do
{ printf("\n1)insert first\n2)Insert after\n3)insert before\n4)display \n5)Delete \n6)END");
scanf("%d",&o);
switch(o)
{ case 1:printf("\nvalue to be inserted first:");
scanf("%d",&a);
insertf(a);
break;
case 2:printf("\nenter item to insert an other value after that value ");
scanf("%d",&item);
printf("\nvalue to be inserted after %d",item);
scanf("%d",&a);
insertAF(iteam,a);
break;
case 3:printf("\nenter item to insert an other value before this item");
scanf("%d",&item);
printf("\nvalue to be inserted before %d",item);
scanf("%d",&a);
insertBF(item,a);
break;
case 4:display();
break;
case 5:printf("\nEnter the value to be Deleted");
scanf("%d",&po);
delete(a);
break;
default:printf("\nENTER A VAID CHOOSE or plz press 6 to exit");
}
}while(ch!=6);
}