Assignment 7: Stack
Set A
a) Implement a stack library (ststack.h) of integers using a static implementation of the stack and implementing the above six operations. Write a driver program that includes stack library and calls different stack operations.
Library Function ( .h file )
NOTE: save file name as ' ststack.h'.
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void create()
{
top = NULL;
}
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
void destroy()
{
top1 = top;
while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
}
Program:- sstack.c
#include <stdio.h>
#include <stdlib.h>
#include "ststack.h"
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
b) Implement a stack library (dystack.h) of integers using a dynamic (linked list) implementation of the stack and implementing the above five operations. Write a driver program that includes stack library and calls different stack operations.
#include <stdio.h>
#include <stdlib.h>
#include "dystack.h"
int main()
{
int n, ch;
do
{
printf("\n\nStack Menu\n1. Push \n2. Pop\n3. Display\n0. Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter number ");
scanf("%d", &n);
push(n);
break;
case 2:
pop();
break;
case 3:
display();
break;
}
}while (ch != 0);
}
Library Function ( .h file )
NOTE: save file name as ' dystack.h'.
struct node
{
int data;
struct node *next;
};
struct node *top = NULL;
void display();
void push(int);
void pop();
void push(int item)
{
struct node *nptr = malloc(sizeof(struct node));
nptr->data = item;
nptr->next = top;
top = nptr;
}
void display()
{
struct node *temp;
temp = top;
while (temp != NULL)
{
printf("\n%d", temp->data);
temp = temp->next;
}
}
void pop()
{
if (top == NULL)
{
printf("\n\nStack is empty ");
}
else
{
struct node *temp;
temp = top;
top = top->next;
printf("\n\n%d deleted", temp->data);
free(temp);
}
}
Set B
a) Write a program to check whether the contents of two stacks are identical. Use stack library to perform basic stack operations. Neither stack should be changed.
#include <stdio.h>
#include "stststack.h"
int main(void)
{
stack s1,s2;
init(&s1);
init(&s2);
int num,n,i,j,size,number;
printf("How many elements in stack1: ");
scanf("%d",&n);
printf("Enter element to push: ");
for(i=0;i<n;i++)
{
scanf("%d",&num);
push(&s1,num);
}
printf("How many elements in stack2: ");
scanf("%d",&size);
printf("Enter elements in push: ");
for(i=0;i<size;i++)
{
scanf("%d",&number);
push(&s2,number);
}
stack t1,t2;
init(&t1);
init(&t2);
while(!isempty(&s1) && !isempty(&s2) && (peek(&s1)==peek(&s2)))
{
push(&t1,pop(&s1));
push(&t2,pop(&s2));
}
if(isempty(&s1) && isempty(&s2))
{
printf("\nStacks are identical\n");
}
else
{
printf("\nNot Equal\n");
}
while(!isempty(&t1))
push(&s1,pop(&t1));
while(!isempty(&t2))
push(&s2,pop(&t2));
}
Stack Library:
typedef struct stack
{
int data[200];
int top;
} stack;
void push(stack *ps,int num)
{
ps->data[++ps->top]=num;
}
int pop(stack *ps)
{
int num;
num=ps->data[ps->top--];
return num;
}
int isempty(stack *ps)
{
if(ps->top==-1)
return 1;
return 0;
}
int isfull(stack *ps)
{
if(ps->top==20-1)
return 1;
return 0;
}
int peek(stack *ps)
{
return ps->data[ps->top];
}
int init(stack *ps)
{
ps->top=-1;
}
b) Write a program that copies the contents of one stack into another. Use stack library to perform basic stack operations. The order of two stacks must be identical.(Hint: Use a temporary stack to preserve the order).
#include<stdio.h>
#include "stststack.h"
int main(void)
{
stack s1,t,s2;
init(&s1);
init(&s2);
init(&t);
int i,n,num;
printf("How many elements in stack1: ");
scanf("%d",&n);
printf("Enter element in stack1: ");
for(i=0;i<n;i++)
{
scanf("%d",&num);
push(&s1,num); //pushing elements in stack1
}
while(!isempty(&s1))
{
push(&t,pop(&s1)); //pushing stack1 elements in temporary stack
}
while(!isempty(&t))
{
push(&s1,peek(&t)); //pushing temporary stack element in stack1
push(&s2,pop(&t)); //pushing temporary stack element in stack2
}
printf("\n-----Elements of stack1 is copied in stack2------\n");
}
Stack Library:
typedef struct stack
{
int data[200];
int top;
} stack;
void push(stack *ps,int num)
{
ps->data[++ps->top]=num;
}
int pop(stack *ps)
{
int num;
num=ps->data[ps->top--];
return num;
}
int isempty(stack *ps)
{
if(ps->top==-1)
return 1;
return 0;
}
int isfull(stack *ps)
{
if(ps->top==20-1)
return 1;
return 0;
}
int peek(stack *ps)
{
return ps->data[ps->top];
}
int init(stack *ps)
{
ps->top=-1;
}
No comments:
Post a Comment