-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleLinkedList.c
116 lines (113 loc) · 1.85 KB
/
singleLinkedList.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<stdlib.h>
typedef struct student
{
int no;
char name[50];
struct student *next;
}node;
node *first=NULL;
void insert();
void deletion();
void display();
void main()
{
int ch;
do
{
printf("1.Insert 2.Delete 3.Display 4.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: insert();break;
case 2: deletion();break;
case 3: display();
}
}while(ch < 4);
}
void insert()
{
node *p, *save, *ptr;
p=malloc(sizeof(node));
printf("Enter roll no and name of the student:");
scanf("%d%s", &p -> no, p -> name);
printf("Number Inserted\n");
if(first == NULL)
{
p -> next = NULL;
first = p;
return;
}
if(p -> no < first -> no)
{
p -> next = first;
first = p;
return;
}
save = first;
ptr = first -> next;
while(ptr != NULL && ptr -> no < p -> no)
{
save = ptr;
ptr = ptr -> next;
}
save -> next = p;
p -> next = ptr;
}
void deletion()
{
node *temp, *ptr;
int roll;
if(first == NULL)
{
printf("Underflow\n");
return;
}
printf("Enter roll no to be deleted");
scanf("%d", &roll);
if(roll < first -> no)
{
printf("Number not found\n");
return;
}
if(first -> no == roll)
{
ptr = first;
first = first -> next;
printf("Number deleted\n");
free(ptr);
return;
}
temp = first;
ptr = first -> next;
while(ptr != NULL && roll > ptr -> no)
{
temp = ptr;
ptr = ptr -> next;
}
if(ptr == NULL || roll < ptr -> no)
{
printf("Number not found\n");
return;
}
temp -> next = ptr -> next;
printf("Number deleted\n");
free(ptr);
}
void display()
{
node *ptr;
if(first == NULL)
{
printf("List is empty\n");
return;
}
ptr = first;
while(ptr != NULL)
{
printf("%d %s\n", ptr -> no, ptr -> name);
ptr = ptr -> next;
}
free(ptr);
}