forked from gouravthakur39/beginners-C-program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack - Linked List.c
88 lines (62 loc) · 1.5 KB
/
Stack - Linked 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
#include<stdio.h>
#include<stdlib.h>
#define SIZE 11
typedef struct node{
char data;
struct node *link;
}ctype, *LinkStack;
void push(LinkStack * L, char elem);
void pop(LinkStack *L);
char top(LinkStack L);
void display(LinkStack L);
int main() {
LinkStack A = NULL;
int i;
char word[SIZE] = {'P', 'R', 'O','G','R','A','M','M','I','N','G'};
char first;
for (i = 0; i < SIZE; i++) {
push(&A, word[i]); /* INSERTING THE CHARACTER IN LINK LIST*/
}
printf("Example word:\n");
display(A);
printf("\n\nAfter popping/deleting the first/top most element:\n");
pop(&A);
display(A);
first = top(A);
printf("\n\nTop most element in the stack: %c\n", first);
return 0;
}
/* PUSHING/INSERTING EACH CHARACTER TO THE STACK USING LINKLIST
NOTE:
Since this is a stack, I used the First in Last out order and we dont need to traverse,
thats why the element is interchanged/reversed.
*/
void push(LinkStack * L, char elem) {
LinkStack temp;
temp = (LinkStack)malloc(sizeof(ctype));
if (temp != NULL) {
temp->data = elem;
temp->link = *L;
*L = temp;
}
}
/* DELETING/REMOVING THE TOP MOST ELEMENT IN THE STACK*/
void pop(LinkStack *L) {
LinkStack temp;
temp = *L;
*L = temp->link;
free(temp);
}
/* RETURNING THE TOP MOST ELEMENT IN THE STACK*/
char top(LinkStack L) {
char elem;
elem = L->data;
return elem;
}
/* FOR DISPLAYING THE ELEMENTS */
void display(LinkStack L) {
LinkStack trav;
for (trav = L; trav != NULL; trav = trav->link) {
printf("%c", trav->data);
}
}