0 Stack concept in Data Structure
STACK

If you have any queries please leave a message here
Your Message
×


A stack is a list like structure where the stack items can only be added or removed from the end of the stack that means stack is a Last In First Out(LIFO) data structure. There have three fundamental operations into a stack program - push, pop and stack-top.

The push operation adds a new item to the top of the stack or initializes the stack if it is empty. If the stack is full and does not contains enough space to accept the given item, the stack is then considered to be in an overflow state.



The pop operation removed an item from the top of the stack. If the stack is empty then it goes into underflow state that meansno items are present in stack to be removed.



The stack-top operation gets the data from the top most position and returns it to the user without deleting it.




Stack Programming in C using Array
//Header-File declaration
#include<stdio.h>
#include<stdlib.h>

//Global variable declaration
int a[100], pos=-1;
//Global function declaration
void push( int n);
int pop();
void print();

void main()
{
int choice, num,ret;
//Infinite loop
while(1)
{
printf("\n Press 1 for push operation");
printf("\n Press 2 for pop operation");
printf("\n Press 3 for display stack elements");
printf("\n Press 4 for exit window");
printf("\n Enter your choice...");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter the number...");
scanf("%d", &num);
push(num);
break;
case 2:
ret=pop();
if(pos != -1)
printf("\n Detete item is = %d",ret);
break;
case 3:
break;
case 4:
exit(0); //Exit from infinite loop
break;
default:
printf("\n Wrong Input");
}
}
}

//push operation
void push(int n)
{
if(pos == 99)
{
printf("\n Stack overflow");
}
else
a[++pos]=n;
}

//pop operation
int pop()
{
int r=0;
if(pos == -1)
printf("\n Stack underflow");
else
if(pos == 0)
{
printf("\n Detete item is = %d",a[pos]);
pos--;
}
else
{
r=a[pos];
pos--;
}
return r;
}

//print stack data
void print()
{
int i;
if(pos == -1)
printf("\n No element has to display");
else
{
for(i=0; i<=pos; i++)
printf("%d\t",a[i]);
}
}



Stack Programming in C using Linked List
//Header-File declaration
#include<stdio.h>
#include<stdlib.h>

//Structure declaration
struct identity
{
int number;
struct identity *next;
};
//Global variable declaration
struct identity *head,*tail,*node;

//Global function declaration
void push( int n);
int pop();
void print();

void main()
{
int choice, num,ret;
head=NULL;
tail=NULL;
//Infinite loop
while(1)
{
printf("\n Press 1 for push operation");
printf("\n Press 2 for pop operation");
printf("\n Press 3 for display stack elements");
printf("\n Press 4 for exit window");
printf("\n Enter your choice...");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter the number...");
scanf("%d", &num);
push(num);
break;
case 2:
ret=pop();
if(head != NULL)
printf("\n Detete item is = %d",ret);
break;
case 3:
print();
break;
case 4:
exit(0); //Exit from infinite loop
default:
printf("\n Wrong Input");
}
}
}

//push operation
void push(int n)
{
if(head == NULL)
{
head=(struct identity *) malloc(sizeof(struct identity));
head->number=n;
head->next=NULL;
tail=head;
}
else
{
node=(struct identity *) malloc(sizeof(struct identity));
node->number=n;
node->next=NULL;
tail->next=node;
tail=tail->next;
}
}

//pop operation
int pop()
{
int r=0;
if(head == NULL)
printf("\n Stack underflow");
else
if(head->next == NULL)
{
r=head->number;
printf("\n Detete item is = %d",r);
head=NULL;
tail=NULL;
}
else
{
node=head;
while(node->next!=tail)
node=node->next;
r=tail->number;
node->next=NULL;
tail=node;
}
return r;
}

//print stack data
void print()
{
int i;
if(head == NULL)
printf("\n No element has to display");
else
{
node=head;
while(node != NULL)
{
printf("%d\t",node->number);
node=node->next;
}
}
}


ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us