-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLinkedList.c
92 lines (85 loc) · 1.66 KB
/
LinkedList.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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
typedef struct Node node;
node *head=NULL;
node *curr=NULL;
void insert()
{
node *temp=(node *) malloc(sizeof(node));
printf("Enter data: ");
int in;
scanf("%d",&in);
temp->data=in;
temp->next=NULL;
if(head==NULL)
{
head=temp;
curr=temp;
}
else
{
curr->next=temp;
curr=temp;
}
}
void del()
{
int flag=0;
printf("Enter item to be deleted: ");
int in;
scanf("%d",&in);
for(node *temp=head,*prev=head; temp!=NULL; prev=temp,temp=temp->next)
{
if(temp->data==in)
{
flag=1;
if(temp==head)
{
head=head->next;
free(temp);
break;
}
else
{
prev->next=temp->next;
if(temp==curr) //we need to update curr if incase we remove the last node
curr=prev;
free(temp);
break;
}
}
}
if(flag)
printf("Entery delteld");
else
printf("Entry not found");
getch();
}
void display()
{
for(node *temp=head; temp!=NULL; temp=temp->next)
printf("%d ",temp->data);
getch();
}
int main()
{
int in;
do
{
system("cls");
printf("1.Insert element\n2.Delete element\n3.Display elements\n");
scanf("%d",&in);
switch(in)
{
case 1: insert(); break;
case 2: del(); break;
case 3: display();break;
}
}while(in!=-1);
return 0;
}