-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked.c
113 lines (109 loc) · 1.56 KB
/
linked.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
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node*find(int);
struct node*findprev(int);
struct node
{
int data;
struct node*next;
}*list=NULL,*P;
void insert(int x);
void del(int x);
void display();
void insert(int x)
{
struct node *newnode;
int position;
newnode=malloc(sizeof(struct node));
newnode->data=x;
if(list==NULL)
{
list=malloc(sizeof(struct node));
list->next=newnode;
newnode->next=NULL;
}
else
{
printf("enter the val after which the data has to be inserted:\n");
scanf("%d",&position);
P=find(position);
newnode->next=P->next;
P->next=newnode;
}
}
struct node *find(int s)
{
P=list->next;
while(P!=NULL&&P->data!=s)
P=P->next;
return P;
}
void del(int x)
{
struct node *temp;
temp=malloc(sizeof(struct node));
P=findprev(x);
if(P->next!=NULL)
{
temp=P->next;
P->next=temp->next;
printf("\n the deleted data is %d",temp->data);
free(temp);
}
else
{
printf("\n data not found!!\n");
}}
struct node *findprev(int s)
{
P=list;
while(P->next!=NULL&&P->next->data!=s)
P=P->next;
return P;
}
void display()
{
if(list->next==NULL)
printf("list is empty");
else
{
P=list->next;
printf("\n the list contains :\n");
while(P!=NULL)
{
printf("%d->",P->data);
P=P->next;
}
printf("NULL");
}
}
void main()
{
int d,choice;
//clrscr();
printf("\n1.insert\t2.delete\t3.display\t4.exit");
do{
printf("\n enter ur choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the data:");
scanf("%d",&d);
insert(d);
break;
case 2:
printf("enter the data:");
scanf("%d",&d);
del(d);
break;
case 3:
display();
break;
case 4:
exit(0);
}
}while(choice<4);
}