Menu Close

Stack Program using arrays

Stack can be implemented in 2 ways:

  • Using arrays
  • Using linked lists

Implementing Stack using Arrays:

  • In stack implementation using arrays, we use simple array to represent the stack as follows.
  • We use primitive variable top to represent index and perform operations on Stack.

Stack implementation without functions:

  • In this implementation we use simple array to represent stack.
  • In this code program, we perform all the stack operations such as push, pop, traverse without using functions.
#include<stdio.h>
int stack[5], top=-1, size=5 ;
int main()
{
	int ch,ele;
	while(1)
	{
		printf("1.Push \n");
		printf("2.Pop \n");
		printf("3.Peek \n");
		printf("4.Traverse \n");
		printf("5.Quit \n");

		printf("Enter choice : ");
		scanf("%d", &ch);

		if(ch==1)
		{
			printf("Enter element : ");
			scanf("%d", &ele);
		}
		switch(ch)
		{
			case 1 : if(top==size-1)
				  printf("Stack is OverFlow\n\n");
				  else
				  {
					stack[++top] = ele;
					printf("Element pushed onto stack\n\n");
				}
				break ;

			case 2 : if(top==-1)
					printf("Stack is Underflow\n\n") ;
				  else
					printf("Popped item is : %d\n\n", stack[top--]);
				  break ;

			case 3 : if(top==-1)
					printf("Stack is Underflow\n\n") ;
				  else
					printf("Peek element is : %d\n\n",stack[top]);
				  break ;

			case 4 : if(top==-1)
					printf("Stack has no elements to display\n\n");
				  else
				  {
					int i ;
					printf("Stack elements :\n");
					for(i=top ; i>=0 ; i--)
						printf("%d\n",stack[i]);
				  }
				  break;

			case 5 : exit(1);
			default: printf("Invalid choice\n\n");
		}
	}
	return 0 ;
}

Stack implementation using functions:

  • We use functions to implement the stack in this approach.
  • We call the stack operations functions from the main function.
  • We represent the prototypes of each function before definition.
#include<stdio.h>
#define SIZE 5

void push();
void pop();
void peek();
void traverse();

int stack[SIZE], top=-1;
int main()
{
	int ch, ele;
	while(1)
	{
		printf("Stack operations :\n");
		printf("1. Push\n");
		printf("2. Pop\n");
		printf("3. Peek\n");
		printf("4. Display\n");
		printf("5. Quit\n");
		
		printf("Enter your choice : ");
		scanf("%d", &ch);
		
		switch(ch)
		{
			case 1 :	push();
					break;
			
			case 2 :	pop();
					break ;
			
			case 3 :	peek();
					break ;
		
			case 4 :	traverse();
					break;
			
			case 5 :	exit(0);
			default:	printf("Invalid choice \n");
		}
	}
	return 0;	
}

void push()
{
	if(top==SIZE-1)
	{
		printf("Stack is Full \n");
	}
	else
	{
		int ele;
		printf("Enter ele to push : ");
		scanf("%d", &ele);
		stack[++top]=ele;
		printf("Element inserted...\n");
	}
}

void pop()
{	
	if(top==-1)
	{
		printf("Stack is Empty\n");
	}
	else
	{
		printf("Popped : %d \n", stack[top--]);
	}
}

void peek()
{
	if(top==-1)
	{
		printf("Stack is Empty\n");
	}
	else
	{
		printf("Peek : %d \n", stack[top]);
	}	
}

void traverse(void)
{
	if(top==-1)
	{
		printf("Stack is Empty \n");
	}
	else
	{
		int i;
		printf("Stack elements are :\n");
		for(i=top ; i>=0 ; i--)
		{
			printf("%d \n", stack[i]);
		}
	}
}

Stack Algorithm implementation:

  • In this implementation we use static array to represent the stack.
  • We follow the exact prototype of each function such as
    • Push function taking input and not returning output
    • Pop function not taking input but returning output.
  • The Program code as follows:
#include<stdio.h>
#define SIZE 5

void push(int);
int pop(void);
int peek(void);
void traverse(void);
int isFull(void);
int isEmpty(void);

int stack[SIZE], top=-1;
int main()
{
	int ch, ele;
	while(1)
	{
		printf("Stack operations :\n");
		printf("1. Push\n");
		printf("2. Pop\n");
		printf("3. Peek\n");
		printf("4. Display\n");
		printf("5. Quit\n");
		
		printf("Enter your choice : ");
		scanf("%d", &ch);
		
		switch(ch)
		{
			case 1 :	printf("Enter element to push : ");
						scanf("%d", &ele);
						push(ele);
						break;
			
			case 2 :	ele = pop();
						if(ele)
							printf("Popped : %d \n", ele);
						else
							printf("Stack is Empty \n");
						break ;
			
			case 3 :	ele = peek();
						if(ele)
							printf("Peek item : %d \n", ele);
						else
							printf("Stack is Empty \n");
						break ;
		
			case 4 :	traverse();
						break;
			
			case 5 :	exit(0);
			default:	printf("Invalid choice \n");
		}
	}
	return 0;	
}

void push(int ele)
{
	if(isFull())
	{
		printf("Stack is Full \n");
	}
	else
	{
		++top;
		stack[top]=ele;
		printf("Element inserted...\n");
	}
}

int pop(void)
{
	int ele;	
	if(isEmpty())
	{
		return 0;
	}
	else
	{
		ele=stack[top];
		--top;
		return ele;
	}
}

int peek(void)
{
	if(isEmpty())
	{
		return 0;
	}
	else
	{
		return stack[top];
	}	
}

void traverse(void)
{
	if(isEmpty())
	{
		printf("Stack is Empty \n");
	}
	else
	{
		int i;
		printf("Stack elements are :\n");
		for(i=top ; i>=0 ; i--)
		{
			printf("%d \n", stack[i]);
		}
	}
}

int isFull(void)
{
	if(top==SIZE-1)
		return 1;
	else
		return 0;	
}

int isEmpty(void)
{
	if(top==-1)
		return 1;
	else
		return 0;
}