#include<stdio.h>
#include<stdlib.h>
struct stackarr;
typedef struct stackarr * Stack;

struct stackarr
{
	int Capacity;
	int TopOfStack;
	int *Array;
};

void MakeEmpty(Stack s)
{
	s->TopOfStack = -1;
}

Stack CreateStack(int MaxElements)
{
	Stack s;
	s = (struct stackarr*) malloc(sizeof(struct stackarr));
	s->Array = (int *)malloc(sizeof(int) * MaxElements);
	s->Capacity = MaxElements;
	MakeEmpty(s);
	return s;
}

void DisposeStack(Stack s)
{
	if(s != NULL)
	{
		free(s->Array);
		free(s);
	}
}

int isFull(Stack s)
{
	return s->TopOfStack == s->Capacity - 1;
}

int isEmpty(Stack s)
{
	return s->TopOfStack == -1;
}

void Push(int x, Stack s)
{
	if(isFull(s))
		printf("Full Stack\n\n");
	else
		s->Array[++s->TopOfStack] = x;
}

void Pop(Stack s)
{
	if(isEmpty(s))
		printf("Empty Stack\n\n");
	else
		s->TopOfStack--;
}

int Top(Stack s)
{
	if(isEmpty(s))
		printf("Empty Stack\n\n");
	else
		return s->Array[s->TopOfStack];
}

int TopAndPop(Stack s)
{
	if(isEmpty(s))
		printf("Empty Stack\n\n");
	else
		return s->Array[s->TopOfStack--];
}

void Display(Stack s)
{
	int i;
	if(isEmpty(s))
		printf("Empty Stack\n\n");
	else
	{
		printf("The Stack Elements are :: ");
		for(i=s->TopOfStack; i >= 0; i--)
			printf("%d  ",s->Array[i]);
		printf("\n\n");
	}
}



void main()
{
	int n, x, ch, i;
	Stack s;
	printf("Enter the maximum number of elements in the stack :: ");
	scanf("%d", &n);
	s = CreateStack(n);
	printf("ARRAY IMPLEMENTATION OF STACK ADT\n\n");
	do
	{
		printf("\n\n1. PUSH\t 2. POP\t 3.TOP \t 4. TOPANDPOP\t 5. PRINT\t 6. QUIT\n\nEnter the choice :: ");
		scanf("%d", &ch);
		switch(ch)
		{
			case 1:
				printf("Enter the element to be pushed :: ");
				scanf("%d",&x);
				Push(x,s);
				break;

			case 2:
				Pop(s);
				break;

			case 3:
				printf("The Top element in the stack :: %d\n\n", Top(s));
				break;

			case 4:
				printf("The popped top element in the stack :: %d\n\n", TopAndPop(s));
				break;

			case 5:
				Display(s);
				break;
		}
	}while(ch<6);
	DisposeStack(s);
	return 0;
}

 OUTPUT:

Enter the maximum number of elements in the stack :: 5
ARRAY IMPLEMENTATION OF STACK ADT



1. PUSH  2. POP  3.TOP   4. TOPANDPOP    5. PRINT        6. QUIT

Enter the choice :: 1
Enter the element to be pushed :: 10


1. PUSH  2. POP  3.TOP   4. TOPANDPOP    5. PRINT        6. QUIT

Enter the choice :: 1
Enter the element to be pushed :: 20


1. PUSH  2. POP  3.TOP   4. TOPANDPOP    5. PRINT        6. QUIT

Enter the choice :: 1
Enter the element to be pushed :: 30


1. PUSH  2. POP  3.TOP   4. TOPANDPOP    5. PRINT        6. QUIT

Enter the choice :: 5
The Stack Elements are :: 30  20  10



1. PUSH  2. POP  3.TOP   4. TOPANDPOP    5. PRINT        6. QUIT

Enter the choice :: 2


1. PUSH  2. POP  3.TOP   4. TOPANDPOP    5. PRINT        6. QUIT

Enter the choice :: 5
The Stack Elements are :: 20  10



1. PUSH  2. POP  3.TOP   4. TOPANDPOP    5. PRINT        6. QUIT

Enter the choice ::


We will provide you Codes for C, C++, Java, Python, Web Technologies and many more

C Codes FlowCharts C++ Codes Java Codes HTML Tech Videos

Submit Your Code to Share with other Friends

Signup to programming9.com

Register Here Login Here