Written by Sai Sandeep Thota on February 13, 2013 in C > Programming

C Program to Implement Stack using Linked List

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In this article we will be discussing on how to implement Stack using Linked List. If you aren’t aware of Stack take a look of the C Program to Implement Stack using Arrays. An excerpt about Stack: Stack is a memory structure in which addition and deletion of elements takes place from only one side. We can do two operations they are Inserting an element and deleting an element i.e., Push and Pop respectively.

The main disadvantage of using Stack with Arrays we will have a fixed size of stack. But with Linked list we can dynamically allocate the memory. This is the main advantage of Linked Lists. We will use malloc() to allocate memory dynamically.

Program explanation:
We will we defining three functions here, they are: Push(insert), Pop(delete) and Display(display elements of stack). Stack should remember the address of the previous node. A node comprises of two fields: 1. Data and 2. Address. Basic structure of stack using linked list will look something like this:

  +---------------+
2 | 1  : data     | --> TOP
  +---------------+
1 | 0  : data     |
  +---------------+
0 |    : data     |
  +---------------+

Where 0, 1, 2 are the addresses.

We are defining a Structure in which we will be defining variables and pointers.

push() First of all we are allocating dynamic memory by calling the malloc() function and storing the address in the variable temp. We are then requesting the user to enter the data and pointing the Data with temp. If this is the first element we are inserting then the inserted element will be the top. Else the current node should store the previous Address (top).

pop() We just point the Top to the previous Node address. We don’t delete the node but we just manipulate its address field.

Stack using Linked List C Implementation:

/* STACK using Linked Lists */
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node {
	int data;
	struct node *prev;
	}*top=NULL, *temp=NULL;
void push()
{
	temp = (struct node*)malloc(sizeof(struct node*));
	printf("\nEnter Data:");
	scanf("%d",&temp->data);
	temp->prev=NULL;
	if(top==NULL)
	{
		top=temp;
	}
	else
	{
		temp->prev=top;
		top=temp;
	}
}

void pop()
{
	temp=top->prev;
	top=temp;
	printf("\nDeleted: %d!!",top->prev);
}

void display()
{
	temp=top;
	while(temp!=NULL)
	{
		printf("\n| %d | %d |",temp->prev,temp->data);
		temp=temp->prev;
	}
}

void main()
{
	int ch;
	clrscr();
	do {
		ch=menu();
		switch(ch)
		{
			case 1:push();
				break;
			case 2:pop();
				break;
			case 3:display();
				break;
			case 4:exit(1);
			default:printf("\nEnter a valid option!!");
		}
	}while(1);
}

int menu()
{
	int ch;
	printf("\n Stack using Linked Lists");
	printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
	printf("\nEnter your choice:");
	scanf("%d",&ch);
	return ch;
}

Output:
stack_ll_push

{ 1 comment… read it below or add one }

vishwa March 5, 2017 at 9:50 pm

i love this website a lot
there is lot to learn from the website

Reply

Leave a Comment

Previous post:

Next post: