-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack Linkedlist.txt
89 lines (71 loc) · 1.61 KB
/
Stack Linkedlist.txt
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
#include<stdio.h>
#include<stdlib.h>
//creating structure
struct Node
{
char data;
struct Node *next;
};
//defining type as short
typedef struct Node node;
void push(node* stack, char c)
{
//creating a new node
node * newNode = (node *) malloc(sizeof(node));
newNode-> data = c;
newNode-> next = NULL;
//insert node in the last. find the last node
while(stack -> next != NULL)
{
stack = stack -> next;
}
stack -> next = newNode;
printf("%c is pushed in the stack\n", c);
}
char pop(node * stack)
{
//creating temporary node to hold the value
node * temp ;
char data = '#';
//check is stack is empty or not
if(stack->next == NULL)
{
printf("Underflow \n");
return data;
}
//insert node in the last. find the last node
while(stack -> next -> next != NULL)
{
stack = stack -> next;
}
data = stack -> next -> data;
temp = stack -> next;
stack -> next = NULL;
printf("%c is popped from the stack\n", data);
free(temp);
return temp;
}
void print_stack(node * stack)
{
if (stack != NULL)
{
printf("%c \n", stack -> data);
print_stack(stack -> next);
}
}
int main()
{
//creating header node
node * stack = (node *) malloc(sizeof(node));
stack-> next = NULL;
//push hdata into stack
push(stack, 'S');
push(stack, 'E');
push(stack, 'C');
print_stack(stack-> next);
pop(stack);
pop(stack);
pop(stack);
pop(stack);
return 0;
}