0% found this document useful (0 votes)
7 views5 pages

Exp 8 Ds

Uploaded by

batharva24comp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Exp 8 Ds

Uploaded by

batharva24comp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Stack Using Linked List

#include<stdio.h>
#include <stdlib.h>
struct Stack {
int data;
struct Stack *next;
};
struct Stack *top = NULL;

void push(int ele){


struct Stack *new_node = malloc(sizeof(struct Stack));
new_node->data = ele;
new_node->next = top;
top = new_node;
}

void pop(){
struct Stack * temp = top;
if(top == NULL){
printf("Stack Underflow");
return;
}
top = top->next;
printf("\nDeleted Element: ",temp->data);
free(temp);
}
void display(){
struct Stack *ptr = top;
while(ptr != NULL){
printf(" %d",ptr->data);
ptr = ptr->next;
}
}
int main()
{
int choice, ele;

while (1) {
printf("\nMain Menu:\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter element to push : ");
scanf("%d", &ele);
push(ele);
break;

case 2:
pop();
break;

case 3:
display();
break;

case 4:
exit(0);
}
}

return 0;
}

Output :-

Main Menu:
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter element to push : 55

Main Menu:
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 1
Enter element to push : 66

Main Menu:
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 3
66 55
Main Menu:
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 2

Deleted Element:
Main Menu:
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 3
55
Main Menu:
1. PUSH
2. POP
3. Display
4. Exit
Enter your choice: 4

Queue Using Linked list

#include<stdio.h>
#include <stdlib.h>
struct Queue {
int data;
struct Queue *next;
};
struct Queue *front = NULL;
struct Queue *rear = NULL;

void enque(int ele){


struct Queue *new_node = malloc(sizeof(struct Queue));
new_node->data = ele;
new_node->next = NULL;
if(front == NULL){
front = rear = new_node;
return;
}
rear->next = new_node;
rear = new_node;
}

void deque(){
struct Queue * temp = front;
if(front == NULL){
printf("Queue Underflow");
return;
}
front = front->next;
printf("\nDeleted Element: %d",temp->data);
free(temp);
}
void display(){
struct Queue *ptr = front;
printf("\nQueue: ");
while(ptr != NULL){
printf(" %d",ptr->data);
ptr = ptr->next;
}
}
int main()
{
int choice, ele;

while (1) {
printf("\nMain Menu:\n");
printf("1. Enque\n");
printf("2. Deque\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter element to add : ");
scanf("%d", &ele);
enque(ele);
break;

case 2:
deque();
break;

case 3:
display();
break;

case 4:
exit(0);
}
}

return 0;
}
Output :-

Main Menu:
1. Enque
2. Deque
3. Display
4. Exit
Enter your choice: 1
Enter element to add : 22

Main Menu:
1. Enque
2. Deque
3. Display
4. Exit
Enter your choice: 1
Enter element to add : 67

Main Menu:
1. Enque
2. Deque
3. Display
4. Exit
Enter your choice: 2

Deleted Element: 22
Main Menu:
1. Enque
2. Deque
3. Display
4. Exit
Enter your choice: 3

Queue: 67
Main Menu:
1. Enque
2. Deque
3. Display
4. Exit
Enter your choice: 4

You might also like