Q. 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.
Program
#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);
}
}
Output:
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 1
Enter number 5
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 1
Enter number 7
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 1
Enter number 9
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 3
9
7
5
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 2
9 deleted
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Program
#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);
}
}
Output:
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 1
Enter number 5
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 1
Enter number 7
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 1
Enter number 9
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 3
9
7
5
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
Enter Choice 0-3? : 2
9 deleted
Stack Menu
1. Push
2. Pop
3. Display
0. Exit
0 Comments