Simple Array Representation of Stack.

Simple Array Representation of Stack is very simple , but this is not an efficient and good representation of Stack,because Array size will be fixed. and we have to define this array size at beginning ,and we can not change the size of array when we need to store more data elements.means we can say that simple array representation of Stack is the Static representation.

  • first we have to initialize the array with specific size limitation.
  • Declare top variables : top.
  • initialize the top by -1,-1 indicates that Stack is empty.

Push & Pop (Insertion & Delete Operation)

Working Process of Push and Pop Operation is explained physically in below figure :

  • We start the Push operation from left to right or from index-0 to index (n-1).and maintain one top variable to indicate the stack pointer.

Code Implementation

                             /*Structure Implementations*/
#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<conio.h>
#define SIMPLE_ARRAY_SIZE 3   // Define the Array Size
struct SimpleArrayStack       // Structure for controlling the front ,rear,QueueSize  and DataElement of Queue.
{
	int top;           // pointing to stack pointer 
	int size;          // Stack Size
	int *SimpleArray;  // Array to store the DataElement.
};
struct SimpleArrayStack* CreateSimpleArrayStack()  //Function to Create an Stack and initilize all the required Variables
{
	struct SimpleArrayStack* NewStack = (struct SimpleArrayStack*)malloc(sizeof(struct SimpleArrayStack));
	if (!NewStack)       // if Memory is not initilized for NewStack
		return NULL;
	NewStack->top = -1;
	NewStack->size = SIMPLE_ARRAY_SIZE;
	NewStack->SimpleArray = (int *)malloc(sizeof(int)*SIMPLE_ARRAY_SIZE);
	if (!NewStack->SimpleArray)  //if Array is not initilized
		return NULL;
	else
		return NewStack;
}
bool IsFullStack(struct SimpleArrayStack* Q) //Method for checking the Full state of Stack
{
	return (Q->top == Q->size - 1); //if Stack is full then return true else false
}
void Push(struct SimpleArrayStack* Q, int DataElement)   //Method of pushing the DataElement into Stack
{
	if (!IsFullStack(Q))              // check the Overflow condition of Stack.
	{
		Q->top++;
		Q->SimpleArray[Q->top] = DataElement;
		printf("DataElement %d is Pushed into the stack :\n", DataElement);
	}
	else
		printf("Stack is in Overflow Condition......\n");
}
bool IsEmptyStack(struct SimpleArrayStack* Q)  //Method for checking the Full state of Stack
{
	return Q->top == -1;
}
void Pop(struct SimpleArrayStack* Q) //Method of popping the DataElement from Stack
{
	if (!IsEmptyStack(Q))
	{
		printf("DataElement %d is Popped from Stack :\n", Q->SimpleArray[Q->top]);
		Q->SimpleArray[Q->top] = NULL;
		Q->top--;
	}
}
int main()
{
	int choice, DataElement,ChoosePushPop;
	struct SimpleArrayStack* MyStack = CreateSimpleArrayStack(); //Create one Stack
	if (!MyStack)
		printf("Sorry Your Stack is not ready....\n");
	else
	{
		do
		{
			printf("1 : for Push Operation\n");
			printf("2 : for Pop Operation\n");
			printf("Press 1 or 2 --- : ");
			std::cin >> ChoosePushPop;
			switch (ChoosePushPop)
			{
			case 1:
			{
				do
				{
					printf("Enter the data for Pushing :");
					std::cin >> DataElement;
					Push(MyStack, DataElement);
					printf("\nPress 0 for more Push Operation :");
					std::cin >> choice;
				} while (choice == 0);
				break;
			}
			case 2:
			{
				do
				{
					Pop(MyStack);
					printf("\nPress 0 for more Pop Operation :");
					std::cin >> choice;
				} while (choice == 0);
				break;
			}
			}
		} while (ChoosePushPop == 1 || ChoosePushPop == 2);
	}
	printf("Data Element after Push and Pop Operation---> ");
	while (MyStack->top >=0)
	{
		printf("%d ", MyStack->SimpleArray[MyStack->top]);
		MyStack->top--;
	}
	_getch();
	return 0;
}

Time & Space Complexity

  • Push Operation Time Complexity will be O(1).
  • Pop Operation Time Complexity will be O(1).
  • IsEmptyStack Time Complexity will be O(1).
  • IsFullStack Time Complexity will be O(1).
  • Space Complexity will be O(n).

Leave a Reply

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