Home » Data Structure Tutorial

Implement of stack using array

In this article, we are going to learn how to implement/create a stack using array in data structure?
Submitted by Manu Jemini, on December 17, 2017

A stack is a very important data structure because it can store data in a very practical way. A stack is a linear data structure. Stack array list follows the Last in First Out principle. The element gets to add up at the TOP and deleted from the TOP.

stack implementation using Array in C

Image source: https://upload.wikimedia.org/wikipedia/commons/thumb/2/29/Data_stack.svg/391px-Data_stack.svg.png

In the Code below there are four parts. First three function to implement three different operations like Insert an item, delete an item and display the Stack. The Fourth part is the main function, in that a do while loop is implemented to keep the user engaged and provide him the all the given choices, according to the choice one of the three function get called.

The Push function checks whether the stack is full or not. If the stack is full it’s not do anything but if not it takes input from the user and push it to the top of the stack.

The Pop function checks whether the stack is empty, if not then it prints the topmost item and deletes it from the stack.

The Third function will simply print all the elements of the stack if exist. If not, then it will say stack is Empty.

Note - The Stack can hold only 5 items, for changing the capacity edit the second line.

Read more about algorithms and examples to implement stack in C and C++.

C language program to implement stack using array

#define MAX 5
int top = -1;
int stack_arr[MAX];

/*Begin of push*/
void push()
	int pushed_item;
	if(top == (MAX-1))
		printf("Stack Overflow\n");
		printf("Enter the item to be pushed in stack : ");
		stack_arr[top] = pushed_item;
/*End of push*/

/*Begin of pop*/
void pop()
	if(top == -1)
		printf("Stack Underflow\n");
		printf("Popped element is : %d\n",stack_arr[top]);
/*End of pop*/

/*Begin of display*/
void display()
	int i;
	if(top == -1)
		printf("Stack is empty\n");
		printf("Stack elements :\n");
		for(i = top; i >=0; i--)
			printf("%d\n", stack_arr[i] );
/*End of display*/

/*Begin of main*/
	int choice;

		printf("Enter your choice : ");
		 case 1 :
		 case 2:
		 case 3:
		 case 4:
			printf("Wrong choice\n");
/*End of main*/


stack implementation output using Array in C

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.