Write a program to implement the Stack using Array.
/*
File: DSPrgm05.c
Author: Aditya Saini
Date: Nov 09, 2019
Description: Program to implement the Stack using Array.
*/
#include <stdio.h>
#define SIZE 20
//Declare an array for stack
int stack[SIZE];
int top = 0;
void push (void);
void pop (void);
void display (void);
#define SIZE 20
//Declare an array for stack
int stack[SIZE];
int top = 0;
void push (void);
void pop (void);
void display (void);
//Main function
int main (void)
int main (void)
{
int choice;
//Menu
while (1)
{
printf ("\t***Menu***\n");
printf ("1. Push an element\n");
printf ("2. Pop an element\n");
printf ("3. Display stack\n");
printf ("4. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
push ( );
push ( );
break;
case 2:
pop ( );
break;
break;
case 3:
display ( );
break;
case 4:
return 0;
default:
printf ("\nError! Wrong Choice.\n");
}
}
};
//Function to push an element into the stack.
void push (void)
{
int element;
if (top >= SIZE)
{
printf ("Error! Stack Overflow\n\n");
return;
}
printf ("Input element to push in: ");
scanf ("%d", &element);
stack[top++] = element;
printf ("\n");
};
//Function to pop an element from the stack.
void pop (void)
{
if (top <= 0)
{
printf ("Error! Stack Underflow\n\n");
return;
}
printf ("%d popped out\n\n", stack[--top]);
};
//Function to display the elements in the stack.
void display (void)
{
int i;
if (top <= 0)
{
printf ("Error! Empty Stack\n\n");
return;
}
printf ("Stack: ");
for (i = top - 1; i >= 0; i--)
{
printf ("\t%d\n", stack[i]);
}
printf ("\n");
};
//Function to push an element into the stack.
void push (void)
{
int element;
if (top >= SIZE)
{
printf ("Error! Stack Overflow\n\n");
return;
}
printf ("Input element to push in: ");
scanf ("%d", &element);
stack[top++] = element;
printf ("\n");
};
//Function to pop an element from the stack.
void pop (void)
{
if (top <= 0)
{
printf ("Error! Stack Underflow\n\n");
return;
}
printf ("%d popped out\n\n", stack[--top]);
};
//Function to display the elements in the stack.
void display (void)
{
int i;
if (top <= 0)
{
printf ("Error! Empty Stack\n\n");
return;
}
printf ("Stack: ");
for (i = top - 1; i >= 0; i--)
{
printf ("\t%d\n", stack[i]);
}
printf ("\n");
};
Output
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 1
Input element to push in: 5
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 1
Input element to push in: 6
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 1
Input element to push in: 7
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 1
Input element to push in: 8
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 3
Stack: 8
7
6
5
Stack: 8
7
6
5
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 2
8 popped out
8 popped out
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 2
7 popped out
7 popped out
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 3
Stack: 6
5
Stack: 6
5
***Menu***
1. Push an element
2. Pop an element
3. Display stack
4. Exit
Choice: 4
No comments:
Post a Comment
Please do not post spam links.