Exp 8 Ds
Exp 8 Ds
#include<stdio.h>
#include <stdlib.h>
struct Stack {
int data;
struct Stack *next;
};
struct Stack *top = NULL;
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
#include<stdio.h>
#include <stdlib.h>
struct Queue {
int data;
struct Queue *next;
};
struct Queue *front = NULL;
struct Queue *rear = NULL;
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