-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.SinglyLinkedList.c
169 lines (158 loc) · 3.35 KB
/
07.SinglyLinkedList.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
7 Design, Develop and Implement a menu driven Program in C for the
following operations on Singly Linked List (SLL) of Student Data with the
fields: USN, Name, Branch, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Demonstrate how this SLL can be used as STACK
f. Exit
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char usn[10];
char name[15];
char branch[2];
int sem;
long int phno;
struct node *link;
} NODE;
typedef struct head_node
{
int count;
struct node *link;
} HEAD;
NODE *getNode();
void insfront(HEAD *head);
void insrear(HEAD *head);
void delfront(HEAD *head);
void delrear(HEAD *head);
void display(HEAD *head);
void main()
{
HEAD *head = (HEAD *)malloc(sizeof(HEAD));
int ch;
head->count = 0;
head->link = NULL;
for (;;)
{
printf("\nMenu\n");
printf("1. Insert Front\n2. Insert Rear\n3. Delete Front\n4. Delete Rear\n5. Display\n6. Exit\n");
printf("For Stack Choose 2,4 and 5 options\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
insfront(head);
break;
case 2:
insrear(head);
break;
case 3:
if (head->link == NULL)
printf("List Empty\n");
else
delfront(head);
break;
case 4:
if (head->link == NULL)
printf("List Empty\n");
else
delrear(head);
break;
case 5:
if (head->link == NULL)
printf("List Empty\n");
else
display(head);
break;
case 6:
exit(0);
}
}
}
NODE *getNode()
{
NODE *temp = (NODE *)malloc(sizeof(NODE));
if (temp == NULL)
{
printf("No Memory\n");
exit(0);
}
return temp;
}
void insfront(HEAD *head)
{
NODE *new = getNode();
(head->count)++;
printf("Enter Details such as USN Name Branch Semester PhNo\n");
scanf("%s%s%s%d%ld", (new->usn), (new->name), (new->branch), &(new->sem), &(new->phno));
new->link = head->link;
head->link = new;
return;
}
void insrear(HEAD *head)
{
NODE *new = getNode();
NODE *temp = head->link;
(head->count)++;
printf("Enter Details such as USN Name Branch Semester PhNo\n");
scanf("%s%s%s%d%ld", (new->usn), (new->name), (new->branch), &(new->sem), &(new->phno));
new->link = NULL;
if (temp == NULL)
{
head->link = new;
return;
}
while (temp->link != NULL)
{
temp = temp->link;
}
temp->link = new;
return;
}
void delfront(HEAD *head)
{
NODE *temp = head->link;
(head->count)--;
printf("Deleted Record is \n");
printf("%s\t%s\t%s\t%d\t%ld\n", (temp->usn), (temp->name), (temp->branch), (temp->sem), (temp->phno));
head->link = temp->link;
return;
}
void delrear(HEAD *head)
{
NODE *previous = NULL, *present = head->link;
(head->count)--;
if (present->link == NULL)
{
head->link = NULL;
}
else
{
while (present->link != NULL)
{
previous = present;
present = present->link;
}
previous->link = NULL;
}
printf("Deleted Record is \n");
printf("%s\t%s\t%s\t%d\t%ld\n", (present->usn), (present->name), (present->branch), (present->sem), (present->phno));
free(present);
return;
}
void display(HEAD *head)
{
NODE *temp = head->link;
printf("Total Records are %d\n", head->count);
printf("USN\tName\t\tBranch\tSem\tPhNo\n");
while (temp != NULL)
{
printf("%s\t%s\t\t%s\t%d\t%ld\n", (temp->usn), (temp->name), (temp->branch), (temp->sem), (temp->phno));
temp = temp->link;
}
}