Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
1K views

C Program To Implement Stack Using Array

This C program implements a stack using an array. A stack is a data structure that only allows items to be inserted and removed from one end, called the top. Items are added to the top of the stack using a PUSH function and removed from the top using a POP function, following the last-in, first-out (LIFO) principle. The program defines functions to display the stack, push new items onto the stack, and pop items off the stack. It uses a while loop that takes user input to call these functions and demonstrate a stack implemented with an array.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

C Program To Implement Stack Using Array

This C program implements a stack using an array. A stack is a data structure that only allows items to be inserted and removed from one end, called the top. Items are added to the top of the stack using a PUSH function and removed from the top using a POP function, following the last-in, first-out (LIFO) principle. The program defines functions to display the stack, push new items onto the stack, and pop items off the stack. It uses a while loop that takes user input to call these functions and demonstrate a stack implemented with an array.

Uploaded by

akurathikotaiah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C program to implement Stack using array

A STACK is a simple Data Structure, It can be implemented as an array or as Linked List, Stack
has only One End that is TOP, Item can be pushed (add) and popped (remove) by only this
End (TOP Pointer). Array follows LIFO (Last In First Out) property, it means Item that is
inserted Last will be popped first.

#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int STACK[MAX],TOP;
void display(int []);
void PUSH(int [],int);
void POP (int []);
void main()
{
int ITEM=0;
int choice=0;
TOP=-1;

while(1)
{
printf("Enter Choice (1: display, 2: insert (PUSH), 3: remove(POP)), 4:
Exit..:");
scanf("%d",&choice);
switch(choice)
{
case 1:
display(STACK);
break;
case 2:
printf("Enter Item to be insert :");
scanf("%d",&ITEM);
PUSH(STACK,ITEM);
break;
case 3:
POP(STACK);
break;
case 4:
exit(0);
            default:
printf("\nInvalid choice.");
break;
}
getch();
}// end of while(1)

void display(int stack[])


{
int i=0;
if(TOP==-1)
{
printf("Stack is Empty .\n");
return;
}

printf("%d <-- TOP ",stack[TOP]);


for(i=TOP-1;i >=0;i--)
{
printf("\n%d",stack[i]);
}
printf("\n\n");
}
void PUSH(int stack[],int item)
{
if(TOP==MAX-1)
{
printf("\nSTACK is FULL CAN't ADD ITEM\n");
return;
}
TOP++;
stack[TOP]=item;
}

void POP(int stack[])


{
int deletedItem;
if(TOP==-1)
{
printf("STACK is EMPTY.\n");
return;
}

deletedItem=stack[TOP];
TOP--;
printf("%d deleted successfully\n",deletedItem);
return;
}

You might also like