Program 5
STACK USING LINKED LIST
AIM: Write a program to implement stack using linked list
PROGRAM
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
int val;
struct node *next;
};
struct node *head;
void main ()
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
case 1:
{
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting....");
break;
default:
printf("Please Enter valid choice ");
};
void push ()
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
else
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
ptr->val = val;
ptr -> next = NULL;
head=ptr;
else
ptr->val = val;
ptr->next = head;
head=ptr;
printf("Item pushed");
void pop()
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
else
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
void display()
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
else
printf("Printing Stack elements \n");
while(ptr!=NULL)
printf("%d\n",ptr->val);
ptr = ptr->next;
}
RESULT: Program to implement stack using linked list completed successfully
Program 6
STACK USING LINKED LIST
AIM: Write a program to implement queue using linked list
#include < stdio.h >
#include < stdlib.h >
struct node {
int data;
struct node * next;
};
struct node * front = NULL;
struct node * rear = NULL;
void enqueue(int value) {
struct node * ptr;
ptr = (struct node * ) malloc(sizeof(struct node));
ptr-> data = value;
ptr-> next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear->next = ptr;
rear = ptr;
printf("Node is Inserted\n\n");
}
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node * temp = front;
int temp_data = front->data;
front = front->next;
free(temp);
return temp_data;
void display() {
struct node * temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d--->", temp->data);
temp = temp->next;
printf("NULL\n\n");
}
int main() {
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", & choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", & value);
enqueue(value);
break;
case 2:
printf("Deleted element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
return 0;
RESULT: program to implement queue using linked list completed successfully.