Archives for

Data Structures

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

Page 2 of 2«12