-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedlist_operations.c
129 lines (112 loc) · 2.55 KB
/
Linkedlist_operations.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
void insert()
{
struct node *temp,*r,*s;
int info;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value to be inserted ");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
printf("\nEnter the value after which you want to insert a new value ");
scanf("%d",&info);
r=start;
while(r->data!=info)
r=r->next;
s=r->next;
r->next=temp;
temp->next=s;
}
printf("\nNew value %d is inserted",temp->data);
}
void del()
{
struct node *temp,*r,*s;
int info;
if(start==NULL)
{
del();
printf("\nLinked List is empty");
}
else
{
printf("\nEnter the value you want to delete ");
scanf("%d",&info);
temp=start;
while(temp->data!=info)
temp=temp->next;
if(temp==NULL)
{
printf("\n%d is deleted",temp->data);
r=start;
while(r->next!=temp)
r=r->next;
r->next=NULL;
temp->next=NULL;
}
else if(temp==start)
{
printf("\n%d is deleted",temp->data);
start=start->next;
temp->next=NULL;
}
else
{
printf("\n%d is deleted",temp->data);s=start;
while(s->next!=temp)
s=s->next;
s->next=temp->next;
temp->next=NULL;
}
free(temp);
}
}
void display()
{
struct node *temp;
if(start==NULL)
printf("\nLinked List is empty");
else
{
temp=start;
while(temp!=NULL);
{
printf("\n%d",temp->data);
temp=temp->next;
}
}
}
void main()
{
int choice;
do
{
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1 : insert();
break;
case 2 : del();
break;
case 3 : display();
break;
case 4 : exit(0);
break;
}
}while(choice!=4);
}