Implement Push, Pop, Peek and Display operations on stacks maintained using arrays or linked list.
/* Stack Operations - arrays */
#include
#include
#define SIZE 10
int stack[SIZE], top = 0;
void push(void);
void pop(void);
void display(void);
void main()
{
int choice = 1;
while (choice == 1 || choice == 2 || choice == 3)
{
clrscr();
printf("Stack Implementation Using Arrays :-\n");
printf("----- -------------- ----- ------\n\n");
printf("1] Push an element onto the stack\n");
printf("2] Pop an element from the stack\n");
printf("3] Display the stack elements\n");
printf("4] Exit\n\n");
printf("Enter your choice :- ");
scanf("%d",&choice);
switch (choice)
{
case 1 :
push();
break;
case 2 :
pop();
break;
case 3 :
display();
break;
case 4 :
break;
default :
printf("\nOut of range\n\n");
choice = 1;
getch();
}
}
clrscr();
printf("\nProgram finished......");
getch();
}
/* Function to push an element onto the stack */
void push(void)
{
int value;
if (top == SIZE)
{
printf("\nStack Full...cannot push...");
getch();
}
else
{
printf("\nEnter the element value : ");
scanf("%d", &value);
stack[top] = value;
top++;
}
return;
}
/* Function to pop an element from the stack */
void pop(void)
{
int value;
if (top == 0)
{
printf("\nStack Empty...can not pop...");
getch();
}
else
{
value = stack[top-1];
printf("\n%d is popped from the stack...", value);
getch();
top--;
}
return;
}
/* Function to display all the elements of the stack */
void display(void)
{
int i;
if (top == 0)
{
printf("\nStack empty...No elements to display...");
getch();
}
else
{
printf("\nStack elements are :-\n\n");
for (i=0; i<=(top-1); i++)
{
printf("%d ----> %d\n", (i+1), stack[i]);
}
getch();
}
return;
}
OUTPUT:-
Stack Implementation Using Arrays :-
----- -------------- ----- ------
1] Push an element onto the stack
2] Pop an element from the stack
3] Display the stack elements
4] Exit
Enter your choice :- 3
Stack elements are :-
1 ----> 11
2 ----> 22
3 ----> 33
4 ----> 44
Enter your choice :- 4
/* Stack Operations - arrays */
#include
#include
#define SIZE 10
int stack[SIZE], top = 0;
void push(void);
void pop(void);
void display(void);
void main()
{
int choice = 1;
while (choice == 1 || choice == 2 || choice == 3)
{
clrscr();
printf("Stack Implementation Using Arrays :-\n");
printf("----- -------------- ----- ------\n\n");
printf("1] Push an element onto the stack\n");
printf("2] Pop an element from the stack\n");
printf("3] Display the stack elements\n");
printf("4] Exit\n\n");
printf("Enter your choice :- ");
scanf("%d",&choice);
switch (choice)
{
case 1 :
push();
break;
case 2 :
pop();
break;
case 3 :
display();
break;
case 4 :
break;
default :
printf("\nOut of range\n\n");
choice = 1;
getch();
}
}
clrscr();
printf("\nProgram finished......");
getch();
}
/* Function to push an element onto the stack */
void push(void)
{
int value;
if (top == SIZE)
{
printf("\nStack Full...cannot push...");
getch();
}
else
{
printf("\nEnter the element value : ");
scanf("%d", &value);
stack[top] = value;
top++;
}
return;
}
/* Function to pop an element from the stack */
void pop(void)
{
int value;
if (top == 0)
{
printf("\nStack Empty...can not pop...");
getch();
}
else
{
value = stack[top-1];
printf("\n%d is popped from the stack...", value);
getch();
top--;
}
return;
}
/* Function to display all the elements of the stack */
void display(void)
{
int i;
if (top == 0)
{
printf("\nStack empty...No elements to display...");
getch();
}
else
{
printf("\nStack elements are :-\n\n");
for (i=0; i<=(top-1); i++)
{
printf("%d ----> %d\n", (i+1), stack[i]);
}
getch();
}
return;
}
OUTPUT:-
Stack Implementation Using Arrays :-
----- -------------- ----- ------
1] Push an element onto the stack
2] Pop an element from the stack
3] Display the stack elements
4] Exit
Enter your choice :- 3
Stack elements are :-
1 ----> 11
2 ----> 22
3 ----> 33
4 ----> 44
Enter your choice :- 4