TheAlgorithms-C/data_structures/stack/main.c

99 lines
1.7 KiB
C
Raw Normal View History

2019-02-14 17:51:36 +03:00
//program for stack using array
#include <stdio.h>
2018-02-10 23:00:19 +03:00
void push();
void pop();
2019-02-14 17:51:36 +03:00
void peek();
2018-02-10 23:00:19 +03:00
void update();
2019-02-14 17:51:36 +03:00
int a[100], top = -1;
int main()
{
2019-02-14 17:51:36 +03:00
2018-02-10 23:00:19 +03:00
int x;
2019-02-14 17:51:36 +03:00
while (1)
2018-02-10 23:00:19 +03:00
{
2019-02-14 17:51:36 +03:00
printf("\n0.exit");
2018-02-10 23:00:19 +03:00
printf("\n1.push");
printf("\n2.pop");
2019-02-14 17:51:36 +03:00
printf("\n3.peek");
2018-02-10 23:00:19 +03:00
printf("\n4.update");
2019-02-14 17:51:36 +03:00
printf("\nenter your choice? ");
scanf("%d", &x);
switch (x)
2018-02-10 23:00:19 +03:00
{
2019-02-14 17:51:36 +03:00
case 0:
return 0;
2018-02-10 23:00:19 +03:00
case 1:
push();
break;
case 2:
pop();
break;
case 3:
2019-02-14 17:51:36 +03:00
peek();
2018-02-10 23:00:19 +03:00
break;
case 4:
update();
break;
default:
printf("\ninvalid choice");
}
}
2019-02-14 17:51:36 +03:00
return (0);
}
//function for pushing the element
void push()
{
int n = 0;
printf("\nenter the value to insert? ");
scanf("%d", &n);
top += 1;
a[top] = n;
2018-02-10 23:00:19 +03:00
}
2019-02-14 17:51:36 +03:00
//function for poping the element out
void pop()
{
if (top == -1)
{
printf("\nstack is empty");
}
else
{
int item;
item = a[top];
top -= 1;
printf("\npoped item is %d ", item);
}
}
//function for peeping the element from top of the stack
void peek()
{
if (top >= 0)
printf("\n the top element is %d", a[top]);
else
printf("\nstack is empty");
}
//function to update the element of stack
void update()
{
int i, n;
printf("\nenter the position to update? ");
scanf("%d", &i);
printf("\nenter the item to insert? ");
scanf("%d", &n);
if (top - i + 1 < 0)
{
printf("\nunderflow condition");
}
else
{
a[top - i + 1] = n;
}
}