-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
103 lines (101 loc) · 1.92 KB
/
stack.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
}*top=NULL;
void push(struct node *top,int x)
{
struct node *t;
t=(struct node*)malloc(sizeof(struct node));
if(t==NULL)
printf("Satck is full\n");
else
{
t->data=x;
t->next=top;
t=top;
}
}
int pop(struct node* top)
{
struct node *p;
int x=-1;
if(p==NULL)
{
printf("Stack is empty\n");
}
else
{
p=top;
x=p->data;
top=top->next;
free(p);
}
return x;
}
int peek(struct node*top,int pos)
{
struct node *p=top;
int i;
if(p)
{
for(i=0;i<pos-1;i++)
{
p=p->next;
}
return p->data;
}
else return -1;
}
int isEmpty(struct node *top)
{
return top?0:1;
}
int isFull(struct node*top)
{
struct node*t=(struct node*)malloc(sizeof(struct node));
return t?0:1;
}
int stackTop9(struct node *top)
{
if(top)
return top->data;
else
return -1;
}
void display(struct node*top)
{
struct node *p=top;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void main()
{
int x,y,z;
int ch;
printf("1 for push\n2for pop\n3 for peek\n4 for empty check\n5 for full check\n6 for finding the data at stack top\n0 for exit\n");
do
{
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 0:break;
case 1:printf("Enter the value\n");
scanf("%d",&x);
push(top,x);
break;
case 2:z=pop(top);
printf("The pooped element is %d\n",z);
break;
default :printf("Wrong choice\n");
}
}while(ch!=0);
display(top);
}