Implementaion of Stack as an Array in C language

/////////////////////////////////////////////////////

// Implementation of stack(containing names) as an //

// array, with its basic operations PUSH,POP and   //

// also the IS_EMPTY and IS_FULL operations.       //

// Program written by Satish Gandham on 01-09-08   //

// Feel free to modify or use it directly          //

// Program compiled with GCC Compiler              //

/////////////////////////////////////////////////////

#include<stdio.h>
#define size 5
char stack[size][15],ele[20];
int tos;
void push();
char* pop();
void show();
int isempty();
int isfull();

int main()
{
	int choice;
	tos=0;
	do
	{
		printf("\tEnter 1 for push,2 for pop,3 to show,and 4 to exit\n");
		scanf("%d",&choice);
		switch(choice)
		{
		case 1:
		if (isfull())
		printf("\nThe stack is full");
		else
			{
		printf("\n Enter element to insert");
		scanf("%s",ele);
		push(ele);
			}
		break;
		case 2:
		if(isempty())
		printf("\n The stack is empty");
		else
		printf("\nThe removed element is:%s",pop());
		break;	
		case 3:
		if(isempty())
		printf("\nThe stack is empty, I cant show you any elements");
		else
		show();
		break;
		case 4:
		exit(1);
		default:
		printf("\nDo you understand english and numbers??");
		}
	}while(1);
}
int isempty()
{
	return(tos==0);
}
int isfull()
{
	return(tos==size);
}
void push(ele)
{
	//stack[tos]=ele;
	strcpy(stack[tos],ele);
	tos++;
}
char* pop()
{
	tos--;
	return(stack[tos]);
}
void show()
{
	int x=tos;	
	printf("\nThe Stack elements are.....\n");
	while(x!=0)
	printf("\t%s\n",stack[--x]);
}

Download the Program

5 thoughts on “Implementaion of Stack as an Array in C language

  1. […] yard algorithm to convert an infix expression to post fix expression, This is a extension of the stack program published […]

  2. […] For implementation of stack using a array refer to this post Implementaion of Stack as an Array in C language […]

  3. baljit kaur says:

    stack

  4. Thanks a lot………
    I have referred ur stack program…..

  5. Deep jadhav says:

    Thanks a lot………

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>