Stack Implementation Using Linked List & Array
This is the C Program which implements Stack using a linked list
In computer science, a stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop. The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure. In a LIFO data structure, the last element added to the structure must be the first one to be removed. This is equivalent to the requirement that, considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack. Often a peek or top operation is also implemented, returning the value of the top element without removing it.
A stack may be implemented to have a bounded capacity. If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items or results in an empty stack, but, if the stack is empty, it goes into underflow state, which means no items are present in stack to be removed.
A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition. Therefore, the lower elements are those that have been on the stack the longest
Using Array:
/* Write a C program to implement stack. Stack is a LIFO data strcuture *
* LIFO - Last in First Out *
* Perform PUSH(insert operation), POP(Delete operation) and Display stack */
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 5
struct stack /* Structure definition for stack */
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
/* Function declaration/Prototype*/
void push (void);
int pop(void);
void display (void);
void main ()
{
int choice;
int option = 1;
clrscr ();
s.top = -1;
printf ("STACK OPERATIONn");
while (option)
{
printf ("------------------------------------------n");
printf (" 1 --> PUSH n");
printf (" 2 --> POP n");
printf (" 3 --> DISPLAY n");
printf (" 4 --> EXIT n");
printf ("------------------------------------------n");
printf ("Enter your choicen");
scanf ("%d", &choice);
switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?n");
scanf ("%d", &option);
}
}
/*Function to add an element to the stack*/
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Fulln");
return;
}
else
{
printf ("Enter the element to be pushedn");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/*Function to delete an element from the stack*/
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Emptyn");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/*Function to display the status of the stack*/
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is emptyn");
return;
}
else
{
printf ("nThe status of the stack isn");
for (i = s.top; i >= 0; i--)
{
printf ("%dn", s.stk[i]);
}
}
printf ("n");
}
Using Linked List:
#include<stdio.h>
typedef struct node{
int data;
struct node *next;
}STACK;
STACK *top=NULL;
main()
{
int ch,value;
printf("n1.PUSHn2.POPn3.DISPLAYn4.Exitn");
scanf("%d",&ch);
while(ch!=4)
{
switch(ch)
{
case 1:printf("Enter an integer: ");
scanf("%d",&value);
push(value);
break;
case 2:pop();
break;
case 3:display();
break;
case 4:
break;
}
printf("n?n");
scanf("%d",&ch);
}
}
push(int value)
{
STACK *newnode;
newnode=(STACK*)malloc(sizeof(STACK));
newnode->data=value;
newnode->next=top;
top=newnode;
}
pop()
{
STACK *temp;
int pop;
temp=top;
pop=top->data;
top=top->next;
free(temp);
printf("Element deleted is %dn",pop);
}
display()
{
STACK *curr=top;
while(curr!=NULL)
{
printf("%d==>",curr->data);
curr=curr->next;
}
}
Recent Comments