Ilovepdf Merged
Ilovepdf Merged
# include <stdio.h>
# include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
int enqueue();
int dequeue();
int display();
int enqueue(){
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if(newNode == NULL){
printf("Memory Allocation failed! Insertion not possible.\n\n");
return 1;
}
if(rear == NULL){
front = rear = newNode;
} else{
rear->next = newNode;
rear = newNode;
}
printf("Successfully added %d to the queue!\n",newNode->data);
return 0;
}
int dequeue(){
if(front == NULL){
printf("Queue Underflow! The queue is already empty.\n");
return 1;
}
if(front == NULL){
rear == NULL;
}
free(temp);
return 0;
}
int display(){
if(front == NULL){
printf("Queue Underflow! The queue is already empty.\n");
return 1;
}
printf("Current Queue:-");
struct Node* temp = front;
while(temp != NULL){
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
printf("\n\t\t\t******Welcome to Implementation of Queue in Linked List Program******\n");
int choice;
while (1) {
printf("\nOptions:\n");
printf("1. Enqueue an element.\n");
printf("2. Dequeue an element.\n");
printf("3. Display the queue.\n");
printf("4. Exit the program.\n");
printf("Choose an option (1-4):- ");
scanf("%d", &choice);
switch (choice) {
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4:
printf("\n\t\t\t*****Thank You, Queue Operation terminated.*****\n\n");
return 0;
default:
printf("Sorry!!! Invalid Invalid input! Choose 1-4.\n");
}
}
return 0;
}
/*Output:-
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 2
Queue Underflow! The queue is already empty.
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 3
Queue Underflow! The queue is already empty.
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 1
Enter element to insert:- 72
Successfully added 72 to the queue!
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 3
Current Queue:-72
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 1
Enter element to insert:- 89
Successfully added 89 to the queue!
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 3
Current Queue:-72 89
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 2
Successfully removed 72 from the queue.
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 3
Current Queue:-89
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 1
Enter element to insert:- 90
Successfully added 90 to the queue!
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 3
Current Queue:-89 90
Options:-
1. Enqueue an element.
2. Dequeue an element.
3. Display the queue.
4. Exit the program.
Choose an option (1-4):- 4
#include <stdio.h>
#define size 4
int queue[size];
int front = -1, rear = -1;
void enqueue() {
int value;
if ((front == 0 && rear == size - 1) || (front == (rear + 1) % size)) {
printf("Queue is full! Cannot insert elements.\n");
return;
}
if (front == -1) {
front = rear = 0;
} else {
rear = (rear + 1) % size;
}
queue[rear] = value;
printf("Element inserted!\n");
}
void dequeue() {
if (front == -1) {
printf("Queue is empty! Nothing to delete.\n");
return;
}
if (front == rear) {
front = rear = -1; // Queue becomes empty
} else {
front = (front + 1) % size;
}
}
void display() {
if (front == -1) {
printf("Queue is empty!\n");
return;
}
while (1) {
printf("\n===== MENU =====\n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Delete)\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
case 4:
printf("Exiting program. Goodbye!\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Insert at beginning
void insertAtBeginning(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Inserted %d at the beginning.\n", value);
}
// Insert at end
void insertAtEnd(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
printf("Inserted %d at the end.\n", value);
}
if (head->next == NULL) {
printf("Deleted %d from end.\n", head->data);
free(head);
head = NULL;
return;
}
temp2->next = NULL;
printf("Deleted %d from end.\n", temp1->data);
free(temp1);
}
if (temp1 == NULL) {
printf("Node with value %d not found.\n", value);
return;
}
if (temp1 == head) {
head = head->next;
free(temp1);
printf("Deleted node with value %d.\n", value);
return;
}
temp2->next = temp1->next;
free(temp1);
printf("Deleted node with value %d.\n", value);
}
if (temp == NULL) {
printf("List is empty.\n");
return;
}
while (1) {
printf("\n===== Singly Linked List Menu =====\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert After a Node\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete a Specific Node\n");
printf("7. Display List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;
case 3:
printf("Enter value to insert: ");
scanf("%d", &value);
printf("Enter value after which to insert: ");
scanf("%d", &afterValue);
insertAfter(value, afterValue);
break;
case 4:
deleteFromBeginning();
break;
case 5:
deleteFromEnd();
break;
case 6:
printf("Enter value of node to delete: ");
scanf("%d", &value);
deleteNode(value);
break;
case 7:
displayList();
break;
case 8:
printf("Exiting program. Goodbye!\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
/*
===== Singly Linked List Menu =====
1. Insert at Beginning
2. Insert at End
3. Insert After a Node
4. Delete from Beginning
5. Delete from End
6. Delete a Specific Node
7. Display List
8. Exit
Enter your choice: 1
Enter value to insert at beginning: 45
Inserted 45 at the beginning.