-
Notifications
You must be signed in to change notification settings - Fork 0
/
STACK USING SINGLE LINK LIST.c
158 lines (133 loc) · 2.4 KB
/
STACK USING SINGLE LINK LIST.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
#include<stdio.h>
#include<malloc.h>
void create(int);
void insert_end();
void delete_node_last();
void disp();
int node_count();
struct node{
int data;
struct node *next;
};
struct node *start = NULL;
void main()
{
int no_of_nodes = 0, choose = -999999, loop_exit = -1;
printf("Enter number of nodes: ");
scanf("%d", &no_of_nodes);
create(no_of_nodes);
disp();
while(loop_exit != 0)
{
printf("\n---------------------------------------------------\n");
printf("\nChoose the given options: \n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
printf("0. EXIT\n");
printf("\n");
printf("Choose: ");
scanf("%d", &choose);
switch(choose)
{
case 0: loop_exit = 0;
break;
case 1: insert_end();
disp();
break;
case 2: delete_node_last();
disp();
break;
case 3: disp();
break;
default: printf("\nInvalid input\n");
}
}
}
void create(int no_of_nodes)
{
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
start = p;
int item0 = 0, item = 0;
printf("Enter the data: ");
scanf("%d", &item0);
p -> data = item0;
p -> next = NULL;
for (int i = 2; i <= no_of_nodes; i++)
{
p -> next = (struct node*)malloc(sizeof(struct node));
p = p -> next;
printf("Enter the data: ");
scanf("%d", &item);
p -> data = item;
p -> next = NULL;
}
}
void insert_end()
{
struct node *p = start, *temp;
int item = 0;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &item);
temp -> data = item;
temp -> next = NULL;
if (p == NULL)
{
start = temp;
}
else
{
while(p -> next != NULL)
{
p = p -> next;
}
p -> next = temp;
}
}
void delete_node_last()
{
struct node *p = start;
int no_of_nodes = node_count();
for (int i = 1; i < no_of_nodes-1; i++)
{
p = p -> next;
}
p -> next = NULL;
}
void disp()
{
struct node *p = start;
if (p == NULL)
{
printf("No link list found.");
}
else
{
while(p != NULL)
{
printf("%d --> ", p -> data);
p = p -> next;
}
printf("NULL");
}
}
int node_count()
{
struct node *p = start;
int count = 0;
if (p == NULL)
{
count = 0;
}
else
{
while(p != NULL)
{
count++;
p = p -> next;
}
}
return count;
}