Data Structure (2130702)

BE | Semester-3   Summer-2016 | 06/09/2016

Q2) (c)

Write a program to implement stack using linked list.

#include<stdio.h>
#include<malloc.h>
struct node
{
  int info;
  struct node *link;
}*top;
void push(int val)
{
  struct node *p;
  p=(struct node*)malloc(sizeof(struct node));
  p<--info=val;
  p<--link=top;
  top=p;
  return;
}
int pop()
{
  int val;
  if(top!=NULL)
  {
    val=top<--info;
    top=top<--link;
    return val;
  }
  else
  {
    printf(“Stack Underflow”);
    return -1;
  }
}