-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks.c
More file actions
80 lines (80 loc) · 1.75 KB
/
Stacks.c
File metadata and controls
80 lines (80 loc) · 1.75 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
79
80
#include<stdio.h>
#include<stdlib.h>
//defining stack size as 5
#define ss 5
//push operation function
void push(int item,int *top,int s[])
{
if(*top==ss-1)//if stack is full
{
printf("Stack Overflow\n");
return;
}
//to push increment stack pointer and then add the item to the top of the stack
*top=*top+1;
s[*top]=item;
printf("Item %d sucessfully inserted\n",item);
return;
}
//pop operation function
int pop(int *top,int s[])
{
int item;
//if stack is empty retuern -1
if(*top==-1)
return -1;
//access the item on top of the stack
item=s[*top];
//decrement the stack pointer
*top=*top-1;
//return the item to be deleted
return item;
}
//function to display stack contents
void display(int top,int s[])
{
int i;
//if stack is empty display empty stack
if(top==-1)
{
printf("Stack is empty\n");
return;
}
//display stack contents
printf("The contents of the stack are:\n");
for(i=0;i<=top;i++)
printf("%d\n",s[i]);
return;
}
//main function
void main()
{
int y,item,top,s[ss],choice;
top=-1;
//infinite loop menu
for(;;)
{
printf("1.Push\n2.Pop\n3.Display\n4.Exit\nEnter choice:\n");
scanf("%d",&choice);
switch(choice)
{
//if push is selected
case 1: printf("enter item to inert\n");
scanf("%d",&item);//input value to push onto stack
push(item,&top,s);//push that item onto stack
break;
//if pop is selected
case 2: y=pop(&top,s);//access the item on top of the stack
if(y==-1)//if the stack is empty
printf("Stack underflow\n");
else//else print the item deleted
printf("Item deleted=%d\n",y);
break;
//if display is selected
case 3: display(top,s);
break;
//if any other option is selcted simply exit from the infinite loop
default: exit(0);
}
}
}