Stack operation: Push and Pop using array in c

S

If you are a computer science student or wants to start learning Data structure using practical hands-on then you are landing in the right place. This practice is part of the Data structure subject taught in GTU (Gujarat Technological University) Diploma course, but it is not limited to this. You can use this tutorial to have a better understanding of detail explanations. if you like to get more out of this practice then I highly recommend you to do code follow along is a best possible way. To know more about Author please visit here

/*
* @Author: abdulkaiyum
* @Date:   2019-08-14 09:04:59
* @Last Modified by:   abdulkaiyum
* @Last Modified time: 2019-08-14 09:20:21
*/


//  Stack Push and pop

#include <stdio.h>
#define SIZE 5

int Stack[SIZE],top = -1, bottom = SIZE;

void options()
{
	printf("%s\n", "----- Stack Operations--------");
	printf("%s\n", "1. Push");
	printf("%s\n", "2. Pop");
	printf("%s\n", "0. exit");
}

void push(int val)
{
	if(top >= bottom - 1)
	{
		printf("%s\n", "Stack overflow");
	}
	else
	{
    top++;
		Stack[top] = val;
	}
}

void pop()
{
	if(top < 0){
		printf("%s\n", "Stack is underflow");
	}
	else{
		top --;
	}
}

void display()
{
	printf("%s", "Stack :");
	for (int i = 0; i <=top; i++)
	{
		printf("%d\t",Stack[i]);
	}
	printf("\n");
}


int main(int argc, char const *argv[])
{
	
	int choice,element;

	do{
		options();
		
		printf("%s\n", "Please enter your option");
		scanf("%d",&choice);
		
		switch(choice){
			case 0: 
				printf("%s\n", "exiting......" );
				break;
				
			case 1: 
				printf("%s\n", "Please enter value to push into the stack");
				scanf("%d",&element);

				push(element);
				display();
				break;
			case 2:
				pop();
				display();
				break;
			
			default:
				printf("%s\n", "Please enter valid option");
				break;
		}
	}while(choice != 0);


	return 0;
}

Add comment

Recent Posts

Recent Comments

Archives

Categories

Email Subscriber