Repository navigation
Expand file tree
/
Copy pathA6_SLLstack.c
More file actions
109 lines (67 loc) · 1.42 KB
/
Copy pathA6_SLLstack.c
File metadata and controls
109 lines (67 loc) · 1.42 KB
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
/* Stack using Singly Linked List. */
#include<stdlib.h>
#include<stdio.h>
typedef struct stack {
int data;
struct stack * ptr;
}stack;
stack * head = NULL;
stack * createnode() {
stack * newnode = (stack *)malloc(sizeof(stack));
printf("Enter the data element to be pushed : ");
scanf("%d" , &newnode->data );
newnode->ptr = NULL;
return newnode;
}
void push() {
if (head == NULL)
head = createnode();
else {
stack * newnode = createnode();
newnode->ptr = head;
head = newnode;
}
printf("%d successfully pushed on to the stack.\n" , head->data);
}
void pop() {
if (head == NULL)
printf("Stack is Empty.\n");
else {
printf("Element %d successfully deleted.\n" , head->data);
head = head->ptr;
}
}
void display() {
if (head != NULL) {
stack * strt = head;
printf("Elements of stack are : ");
while(strt != NULL) {
printf("%d ", strt->data );
strt = strt->ptr;
}
printf("\n");
}
else
printf("Stack is empty.\n");
}
void main() {
int choice , flag = 1 ;
printf("Stack using Singly Linked List.\n");
printf("Enter the operation to be performed.\n");
while(flag) {
printf("1.Push \t 2.Pop \t 3.Display \t 4.Exit : ");
scanf("%d" , &choice);
switch(choice) {
case 1 : push();
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : flag = 0 ;
break;
default : printf("Invalid input.\n");
break;
}
}
}