• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Stack using Linked List in Data Structures

 Stack using Linked List

Another way to represent a stack is by using a linked list.

A stack can be represented by using nodes of the linked list.

Each node contains two fields : data(info) and next(link)

The data field of each node contains an item in the stack and the corresponding next field points to the node containing the next item in the stack

The top refers to the top most node (The last item inserted) in the stack.

The empty stack is represented by setting top to nut. Because the way the nodes are pointing, push and pop operations are easy to accomplish.

 

44

 

 

 

Program:

            Program to implement stack using linked list

# include <stdio.h>

# include <conio.h>

# include <stdlib.h>

struct Node

{

int data;

struct node *top=NULL;

void insertion(int x)

{

struct node *temp;

temp=(struct node *) malloc (size of  (structure node));

temp 7 data =x;

temp7 link =top;

top=temp;

}

int deletion ()

{

struct node  *temp;

int x;

if(cop==NULL)

return -1;

else

{

temp=top;

top=top 7 link;

x=temp 7 data;

free(temp);

return x;

}

void display()

{

struct node *temp;

if(top=NULL)

printf(“stack is empty \n”);

else

{

printf(“The contents are “ \n”);

temp=top;

while(temp!=NULL)

{

printf(“%5d”,temp7data);

temp=temp7 link;

}

}

}

void main()

{

int ch, x;

clrscr();

printf(“1.INSERTION \n”);

printf(“2. DELETION \n”);

printf(“3. DISPLAY  The stack \n”);

printf(“ 4. EXIT \n”);

do

{

printf(“Enter ur choice : \n”);

scanf(“%d”, &ch);

switch(ch)

{

case 1 :

printf(“Enter Data elements : \n”)       ;

scanf(“%d”,&x);

insertion(x);

case 2 :

x=deletion();

if(x==-1)

printf(“STACK is EMPTY \n”)                 ;

else

printf(“ The Deleted value is : %d \n”,x);

break;

case 3:

display ();

break;

case 4:

Exit(0);

}

} while(ch<=4);

getch();

}

 

 

Summary
Review Date
Reviewed Item
Stack using Linked List in Data Structures
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Stack using Linked List in Data Structures"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.