-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedListStack.c
More file actions
74 lines (68 loc) · 1.09 KB
/
linkedListStack.c
File metadata and controls
74 lines (68 loc) · 1.09 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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
}*ptr, *pnew, *header = NULL;
void push(int dat){
pnew = malloc(sizeof(struct node));
if(pnew == NULL){
printf("No memory, element not added\n");
}else{
pnew->data = dat;
if(header == NULL){
pnew->link = NULL;
header = pnew;
}else{
pnew->link = header;
header = pnew;
}
}
}
int pop(){
int dat = header->data;
ptr = header;
header = header->link;
free(ptr);
return dat;
}
void status(){
int count = 0;
ptr = header;
while(ptr != NULL){
ptr = ptr->link;
count++;
}
if(count == 0){
printf("Stack is empty\n");
}else{
printf("%d Elements in Stack\n", count);
}
}
void pops(){
if(header == NULL){
printf("Stack Underflow\n");
}else{
printf("%d\n", pop());
}
}
void main(){
int c, n;
do{
printf("1. PUSH\n2. POP\n3. STATUS\n0. Exit\n: ");
scanf("%d", &c);
switch(c){
case 1:
printf("Enter element: ");
scanf("%d", &n);
push(n);
break;
case 2:pops();
break;
case 3:
status();
break;
default:printf("Invalid\n");
}
}while(c != 0);
}