Implement the stack concept in a single dimensional array by executing C programs.
Objectievs:
To learn how to implement the stack in a single dimentional array in c program.
Introduction :
A stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end. This end is commonly referred to as the “top”, and the opposite end is known as the “base”.Items that are closer to the base have been in the stack the longest. The most recently added item is always on the top of the stack and thus will be removed first. The stack provides an ordering based on length of time in the collection; the “age” of any given item increases as you move from top to base.There are many examples of stacks in everyday situations. Consider a stack of plates on a table, where it’s only possible to add or remove plates to or from the top. Or imagine a stack of books on a desk. The only book whose cover is visible is the one on top. To access the others, we must first remove the ones sitting on top of themStack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).Mainly the following three basic operations are performed in the stack:
Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
Peek or Top: Returns top element of stack.
isEmpty: Returns true if stack is empty, else false.
The following code will implement three functions supported by a stack.
Push ( ): It adds element an on top of the stack. It takes O (1) O (1) time as each element is inserted starting from the table of the array; there is no need to shift existing elements to make room for the new element.
Pop (): It removes the element on top of the stack. It also takes O (1) O (1) time as the top contains the index of the most recently added element.
Top (): It returns the element on top of the stack. It takes O (1) O (1) time as finding the value stored at a particular index in an array is a constant time operation.
Display():finall stack display.
Total no of element in stack is 1, 2 ,8, 8, 3 +any 3 digits
Program:
#include <stdio.h>
int capcity = 8;
int topIndex = -1;
int stack[8];
void push(int data)
{
if (topIndex == capcity)
{
printf("\nStack is full.");
}
else
stack[++topIndex] = data;
}
void pop()
{
if (topIndex == -1)
{
printf("\nStack is empty");
}
else
topIndex--;
}
int top()
{
if (topIndex == -1)
{
printf("\nStack is empty");
}
else
return stack[topIndex];
}
void display ()
{
int i;
if (topIndex == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n\nAfter poping the elements in stack are: \n");
for (int i = topIndex; i >= 0; i--)
{
printf (" %d", stack[i]);
}
}
}
int main()
{
printf("********* PUSH,TOP,POP AND DISPLAY ********** \n\n");
int array[8] ={1,2,8,8,3,4,5,6};
int loop;
printf("The 8 elements in stack are ::\n");
for(loop = 0; loop < 8; loop++)
printf(" %d", array[loop]);
for(int j = 0; j < 8; j++)
push(array[j]);
printf("\n\nAfter pushing all elements the stack are==\n");
for (int i = topIndex; i >= 0; i--)
{
printf(" %d",stack[i]);
}
printf("\n\nTop element of stack : %d", top());
pop();
printf("\n\nTop element of stack after popping : %d", top());
display();
printf("\n\n...........................................");
}
Output:
0 Comments