Home Tutorial Datastructure Push and Pop operation of stack using linked list.

 
 

Push and Pop operation of stack using linked list.
Posted on: June 18, 2010 at 12:00 AM
Advertisement
The fundamental operation of stack is push and pop. The term push is use to place some data element into the stack and pop is use to remove some data element from the stack.

Description:

In this tutorial of data-structure you will see push and pop operation of stack using linked list. In the previous tutorial the stack operation in done using array.

Code:

# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *push(struct node *p , int value)
{
	struct node *temp;
	temp=(struct node *)malloc(sizeof(struct node));
	if(temp==NULL)
	{
		printf("No Memory available\n");
		exit(0);
	}
		temp->data = value;
		temp->link = p;
		p = temp;
		return(p);
	}

struct node *pop(struct node *p , int *value)
{
	struct node *temp;
	if(p==NULL)
	{
	printf(" The stack is empty and cannot pop element\n");
        exit(0);
	}
	 *value = p->data;
	temp = p;
	p = p->link;
	free(temp);
	return(p);
	}

void main()
{
struct node *top = NULL;
int n,value;
do
{
  do
  {
	printf("Enter the element to be pushed in stack\n");
	scanf("%d",&value);
	top = push(top,value);
	printf("Enter 1 to continue\n");
	scanf("%d",&n);
	} while(n == 1);

  printf("Enter 1 to pop an element from stack\n");
  scanf("%d",&n);
  while( n == 1)
 {
 top = pop(top,&value);
 printf("The value poped is %d\n",value);
 printf("Enter 1 to pop an element and other to push\n");
 scanf("%d",&n);
 }
  printf("Enter 1 to continue\n");
  scanf("%d",&n);
} while(n == 1);
}

Output:

Download this code

Advertisement

Related Tags for Push and Pop operation of stack using linked list.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 18, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com