Implementation of Stack Using Array in C

Implementation of Stack Using Array in C. A stack is a group of elements of the same kind in a linear data structure. The Last In First Out (LIFO) method is used in Stack, which means that the last piece inserted is the first one to be pulled out. The insertion and deletion of elements in stacks occurs only at one endpoint.

#include<stdio.h>
int stack_array[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of stack_array[MAX=100]:");
scanf("%d",&n);
printf("\n\t stack_array operations using array");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Select your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t Exit Point");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tstack_array is over flow");

}
else
{
printf(" Enter a value to be pushed");
scanf("%d",&x);
top++;
stack_array[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t stack_array is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack_array[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in stack_array \n");
for(i=top; i>=0; i--)
printf("\n%d",stack_array[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The stack_array is empty");
}

}

 

Output:

Enter the size of stack_array[MAX=100]:45

stack_array operations using array
——————————–
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Select your choice

Array to Implementation of Stack

An array is a collection of data elements of the same type stored in contiguous memory regions. Arrays are a derived data type in the C programming language that may hold primitive data types like int, char, double, float, and so on.  

An array is a grouping of variables with the same data type. In an array, you can store a collection of data of the same kind. Any of the data types can contain an array. The size of the array must be constant. To store array elements in memory, contiguous (adjacent) memory regions are always used.

Push and Pop to Implementation of Stack Using Array in C

A stack is an abstract data type in computer science that serves as a collection of components. It has two major operations: push, which adds an element to the collection, and pop, which removes an element from the collection. Pop, which removes the most recently introduced but not yet removed piece. Using this basic concept to Implementation of Stack Using Array in C for beginners.

For Loop Statement in C

A for loop is a control flow statement that allows code to be executed repeatedly by specifying iteration. When the number of iterations is known before entering the loop, for loops are commonly used. While loops that increment and test a loop variable can be thought of as shorthands for for loops.

How the For Loop Works

The init step is run first, and it is only done once. Any loop control variables can be declared and initialised in this step. You don’t have to use a statement here as long as you use a semicolon.

The condition is then assessed. The body of the loop is executed if it is true. If it is false, the body of the loop is skipped and control is passed to the next statement immediately after the ‘for’ loop.

The flow of control jumps back up to the increment statement after the body of the ‘for’ loop executes. You can use this statement to update any loop control variables. This statement can be left blank as long as the condition is followed by a semicolon. Implementation of Stack Using Array in C using this program we can easily implement stack in c.

Switch Statement in C

A switch statement allows a variable to be compared against a list of values for equality. Each value is referred to as a case, and each switch case is compared to the variable that is being turned on.

How the Switch Statement Works

In C, the switch statement compares the value of a variable to multiple cases. A block of statements associated with that particular case is executed once the case match is found.

Each case in a switch block has a unique name or number that is referred to as an identifier. The user’s value is compared to all of the cases within the switch block until a match is found.

If there isn’t a case match, the default statement is executed, and the control leaves the switch block.

Push and Pop How it Work in Implementation of Stack Using Array in C

PUSH: Pushes a new item into the stack. An overflow condition occurs when the stack is completely full. Stacks are a type of container adaptor that works on the LIFO (Last In First Out) principle, in which a new element is added at one end and (top) an element is removed from the opposite end. stack:: The function push() is used to push an element to the top of the stack.

POP: Removes an item from the stack by popping it. Items are popped in the opposite order that they were pushed. It’s called an Underflow condition when the stack is empty.