0% found this document useful (0 votes)
10 views14 pages

Ilovepdf Merged

The document contains implementations of various data structures in C, including a linear queue using linked lists, a circular queue using arrays, and a singly linked list. Each implementation provides functions for inserting, deleting, and displaying elements, along with a menu-driven interface for user interaction. The code includes error handling for operations like underflow and invalid inputs.

Uploaded by

thecoderpiyush18
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)
10 views14 pages

Ilovepdf Merged

The document contains implementations of various data structures in C, including a linear queue using linked lists, a circular queue using arrays, and a singly linked list. Each implementation provides functions for inserting, deleting, and displaying elements, along with a menu-driven interface for user interaction. The code includes error handling for operations like underflow and invalid inputs.

Uploaded by

thecoderpiyush18
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/ 14

/* Experiment No:- 9

Title :-Implement Linear Queue ADT using Linked List.


Name :- PIYUSH CHANDE
Class :-F.E.(COMPS)
Division:-D
Roll No :-12
UIN :-241P080 */

# include <stdio.h>
# include <stdlib.h>

struct Node{
int data;
struct Node* next;
};

struct Node* front = NULL;


struct Node* rear = NULL;

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;
}

printf("Enter element to insert:- ");


scanf("%d",&newNode->data);
newNode->next = NULL;

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;
}

struct Node* temp = front;

printf("Successfully removed %d from the queue.\n",front->data);


front = front->next;

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:-

******Welcome to Implementation of Queue in Linked List Program******

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

*****Thank You, Queue Operation terminated.*****


*/
/*
EXP NO:6
NAME : PIYUSH CHANDE
TITLE: Array Implementation of Circular Queue.
UIN: 241P080
CLASS & DIV: FE-COMPS-D
ROLL NO: 12
*/

#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;
}

printf("Enter element to insert: ");


scanf("%d", &value);

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;
}

printf("Element %d removed!\n", queue[front]);

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;
}

printf("Queue elements are: ");


int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear)
break;
i = (i + 1) % size;
}
printf("\n");
}
int main() {
int choice;
printf("\n\t\tArray Implementation of Circular Queue\n");

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;
}

/* Array Implementation of Circular Queue

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 2
Queue is empty! Nothing to delete.

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 3
Queue is empty!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 1
Enter element to insert: 774
Element inserted!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 1
Enter element to insert: 5
Element inserted!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 1
Enter element to insert: 22
Element inserted!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 1
Enter element to insert: 99
Element inserted!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 3
Queue elements are: 774 5 22 99

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 1
Queue is full! Cannot insert elements.

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice:2
Element 774 removed!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 2
Element 5 removed!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 2
Element 22 removed!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 2
Element 99 removed!

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 2
Queue is empty! Nothing to delete.

===== MENU =====


1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter your choice: 4
Exiting program. Goodbye! */
/*
EXP NO:7
NAME : PIYUSH CHANDE
TITLE: Implement Singly Linked List
UIN: 241P080
CLASS & DIV: FE-COMPS-D
ROLL NO: 12
*/

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* head = NULL;

// 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);
}

// Insert after a specific node


void insertAfter(int value, int afterValue) {
struct Node* temp = head;
while (temp != NULL && temp->data != afterValue) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with value %d not found.\n", afterValue);
return;
}

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = value;
newNode->next = temp->next;
temp->next = newNode;
printf("Inserted %d after %d.\n", value, afterValue);
}

// Delete from beginning


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}

struct Node* temp = head;


head = head->next;
printf("Deleted %d from beginning.\n", temp->data);
free(temp);
}

// Delete from end


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}

if (head->next == NULL) {
printf("Deleted %d from end.\n", head->data);
free(head);
head = NULL;
return;
}

struct Node* temp1 = head;


struct Node* temp2 = NULL;

while (temp1->next != NULL) {


temp2 = temp1;
temp1 = temp1->next;
}

temp2->next = NULL;
printf("Deleted %d from end.\n", temp1->data);
free(temp1);
}

// Delete a specific node by value


void deleteNode(int value) {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}

struct Node* temp1 = head;


struct Node* temp2 = NULL;

while (temp1 != NULL && temp1->data != value) {


temp2 = temp1;
temp1 = temp1->next;
}

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);
}

// Display the list


void displayList() {
struct Node* temp = head;

if (temp == NULL) {
printf("List is empty.\n");
return;
}

printf("Linked List: ");


while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function with menu


int main() {
int choice, value, afterValue;

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.

===== 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: 57
Inserted 57 at the beginning.

===== 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: 2
Enter value to insert at end: 88
Inserted 88 at the end.

===== 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: 3
Enter value to insert: 74
Enter value after which to insert: 88
Inserted 74 after 88.

===== 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: 7
Linked List: 57 -> 45 -> 88 -> 74 -> NULL

===== 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: 4
Deleted 57 from beginning.

===== 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: 5
Deleted 74 from end.

===== 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: 6
Enter value of node to delete: 88
Deleted node with value 88.

===== 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: 7
Linked List: 45 -> NULL

===== 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: 4
Deleted 45 from beginning.

===== 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: 7
List is empty.

===== 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: 8
Exiting program. Goodbye! */

You might also like