1.
Write a program that uses functions to perform the following operations on
singly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the singly linked list
struct Node {
int data;
struct Node* next;
};
// Function prototypes
struct Node* createNode(int data);
struct Node* insertAtEnd(struct Node* head, int data);
struct Node* deleteNode(struct Node* head, int data);
void traverseList(struct Node* head);
int main() {
struct Node* head = NULL; // Initialize head of the list
int choice, data;
do {
printf("\nLinked List Operations:\n");
printf("1. Insert at End\n");
printf("2. Delete a Node\n");
printf("3. Traverse the List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Insert at End
printf("Enter data to insert: ");
scanf("%d", &data);
head = insertAtEnd(head, data);
printf("Data inserted successfully.\n");
break;
case 2: // Delete a Node
printf("Enter data to delete: ");
scanf("%d", &data);
head = deleteNode(head, data);
printf("Data deleted successfully.\n");
break;
case 3: // Traverse the List
printf("Linked List: ");
traverseList(head);
break;
case 4: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
// Free the allocated memory (to be implemented)
// This function should free all nodes in a real program
return 0;
}
// Function to create a new node with given data
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the linked list
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
return head;
}
// Function to delete the first occurrence of a node with the given data
struct Node* deleteNode(struct Node* head, int data) {
struct Node* temp = head;
struct Node* prev = NULL;
// If the node to be deleted is the head node
if (temp != NULL && temp->data == data) {
head = temp->next;
free(temp);
return head;
}
// Search for the node to be deleted
while (temp != NULL && temp->data != data) {
prev = temp;
temp = temp->next;
}
// If the data was not found in the list
if (temp == NULL) {
printf("Data %d not found in the list\n", data);
return head;
}
// Unlink the node from the linked list
prev->next = temp->next;
free(temp);
return head;
}
// Function to traverse and print the linked list
void traverseList(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("The list is empty.\n");
return;
}
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
2. Write a program that uses functions to perform the following operations on
doubly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the doubly linked list
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function prototypes
struct Node* createNode(int data);
struct Node* insertAtEnd(struct Node* head, int data);
struct Node* deleteNode(struct Node* head, int data);
void traverseList(struct Node* head);
int main() {
struct Node* head = NULL; // Initialize head of the list
int choice, data;
do {
printf("\nDoubly Linked List Operations:\n");
printf("1. Insert at End\n");
printf("2. Delete a Node\n");
printf("3. Traverse the List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Insert at End
printf("Enter data to insert: ");
scanf("%d", &data);
head = insertAtEnd(head, data);
printf("Data inserted successfully.\n");
break;
case 2: // Delete a Node
printf("Enter data to delete: ");
scanf("%d", &data);
head = deleteNode(head, data);
printf("Data deleted successfully.\n");
break;
case 3: // Traverse the List
printf("Doubly Linked List: ");
traverseList(head);
break;
case 4: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
// Free the allocated memory (to be implemented)
// This function should free all nodes in a real program
return 0;
}
// Function to create a new node with given data
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to insert a node at the end of the doubly linked list
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
return head;
}
// Function to delete the first occurrence of a node with the given data
struct Node* deleteNode(struct Node* head, int data) {
struct Node* temp = head;
// If the node to be deleted is the head node
if (temp != NULL && temp->data == data) {
head = temp->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
return head;
}
// Search for the node to be deleted
while (temp != NULL && temp->data != data) {
temp = temp->next;
}
// If the data was not found in the list
if (temp == NULL) {
printf("Data %d not found in the list\n", data);
return head;
}
// Unlink the node from the doubly linked list
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
return head;
}
// Function to traverse and print the doubly linked list
void traverseList(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("The list is empty.\n");
return;
}
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
3. Write a program that uses functions to perform the following operations on
circular linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the circular singly linked list
struct Node {
int data;
struct Node* next;
};
// Function prototypes
struct Node* createNode(int data);
struct Node* insertAtEnd(struct Node* head, int data);
struct Node* deleteNode(struct Node* head, int data);
void traverseList(struct Node* head);
int main() {
struct Node* head = NULL; // Initialize head of the list
int choice, data;
do {
printf("\nCircular Singly Linked List Operations:\n");
printf("1. Insert at End\n");
printf("2. Delete a Node\n");
printf("3. Traverse the List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Insert at End
printf("Enter data to insert: ");
scanf("%d", &data);
head = insertAtEnd(head, data);
printf("Data inserted successfully.\n");
break;
case 2: // Delete a Node
printf("Enter data to delete: ");
scanf("%d", &data);
head = deleteNode(head, data);
printf("Data deleted successfully.\n");
break;
case 3: // Traverse the List
printf("Circular Singly Linked List: ");
traverseList(head);
break;
case 4: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
// Function to create a new node with given data
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end of the circular singly linked list
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
newNode->next = newNode;
return newNode;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
return head;
}
// Function to delete the first occurrence of a node with the given data
struct Node* deleteNode(struct Node* head, int data) {
if (head == NULL) {
printf("The list is empty.\n");
return NULL;
}
struct Node* temp = head;
struct Node* prev = NULL;
// If the node to be deleted is the head node
if (temp->data == data) {
struct Node* last = head;
while (last->next != head) {
last = last->next;
}
if (head->next == head) { // Only one node in the list
free(head);
return NULL;
} else {
last->next = head->next;
free(head);
return last->next;
}
}
// Search for the node to be deleted
while (temp->next != head && temp->data != data) {
prev = temp;
temp = temp->next;
}
// If the data was not found in the list
if (temp->data != data) {
printf("Data %d not found in the list\n", data);
return head;
}
// Unlink the node from the circular singly linked list
prev->next = temp->next;
free(temp);
return head;
}
// Function to traverse and print the circular singly linked list
void traverseList(struct Node* head) {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
struct Node* temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("HEAD\n");
}
4. Write a program that implement stack (its operations) using
i) Arrays ii) Pointers
i) Arrays
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 // Maximum size of the stack
int main() {
int stack[MAX];
int top = -1;
int choice, i, data;
do {
printf("\nStack Operations (Array):\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display Stack\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Push
if (top == MAX - 1) {
printf("Stack overflow\n");
} else {
printf("Enter data to push: ");
scanf("%d", &data);
stack[++top] = data;
printf("Data pushed successfully.\n");
}
break;
case 2: // Pop
if (top == -1) {
printf("Stack underflow\n");
} else {
data = stack[top--];
printf("Popped data: %d\n", data);
}
break;
case 3: // Peek
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Top element: %d\n", stack[top]);
}
break;
case 4: // Display Stack
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack: ");
for (i = top; i >= 0; i--) {
printf("%d -> ", stack[i]);
}
printf("NULL\n");
}
break;
case 5: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
ii) Pointers
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a stack node
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* top = NULL; // Initialize top of the stack
struct Node* temp;
int choice, data;
do {
printf("\nStack Operations (Linked List):\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display Stack\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Push
temp = (struct Node*)malloc(sizeof(struct Node));
if (!temp) {
printf("Memory allocation failed\n");
exit(1);
}
printf("Enter data to push: ");
scanf("%d", &data);
temp->data = data;
temp->next = top;
top = temp;
printf("Data pushed successfully.\n");
break;
case 2: // Pop
if (top == NULL) {
printf("Stack underflow\n");
} else {
temp = top;
data = top->data;
top = top->next;
free(temp);
printf("Popped data: %d\n", data);
}
break;
case 3: // Peek
if (top == NULL) {
printf("Stack is empty\n");
} else {
printf("Top element: %d\n", top->data);
}
break;
case 4: // Display Stack
if (top == NULL) {
printf("The stack is empty.\n");
} else {
temp = top;
printf("Stack: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
break;
case 5: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
// Free all allocated memory (cleaning up the stack)
while (top != NULL) {
temp = top;
top = top->next;
free(temp);
}
return 0;
}
5. Write a program that implement Queue (its operations) using
i) Arrays ii) Pointers
i) Arrays
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 // Maximum size of the queue
int main() {
int queue[MAX];
int front = 0, rear = -1, size = 0;
int choice, i, data;
do {
printf("\nQueue Operations (Array):\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Display Queue\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Enqueue
if (size == MAX) {
printf("Queue is full\n");
} else {
printf("Enter data to enqueue: ");
scanf("%d", &data);
rear = (rear + 1) % MAX; // Circular increment
queue[rear] = data;
size++;
printf("Data enqueued successfully.\n");
}
break;
case 2: // Dequeue
if (size == 0) {
printf("Queue is empty\n");
} else {
data = queue[front];
front = (front + 1) % MAX; // Circular increment
size--;
printf("Dequeued data: %d\n", data);
}
break;
case 3: // Peek
if (size == 0) {
printf("Queue is empty\n");
} else {
printf("Front element: %d\n", queue[front]);
}
break;
case 4: // Display Queue
if (size == 0) {
printf("The queue is empty.\n");
} else {
printf("Queue: ");
for (i = 0; i < size; i++) {
int index = (front + i) % MAX;
printf("%d -> ", queue[index]);
}
printf("NULL\n");
}
break;
case 5: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
ii) Pointers
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a queue node
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* front = NULL; // Front of the queue
struct Node* rear = NULL; // Rear of the queue
struct Node* temp;
int choice, data;
do {
printf("\nQueue Operations (Linked List):\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Display Queue\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Enqueue
temp = (struct Node*)malloc(sizeof(struct Node));
if (!temp) {
printf("Memory allocation failed\n");
exit(1);
}
printf("Enter data to enqueue: ");
scanf("%d", &data);
temp->data = data;
temp->next = NULL;
if (rear == NULL) {
front = rear = temp;
} else {
rear->next = temp;
rear = temp;
}
printf("Data enqueued successfully.\n");
break;
case 2: // Dequeue
if (front == NULL) {
printf("Queue is empty\n");
} else {
temp = front;
data = front->data;
front = front->next;
if (front == NULL) { // If the queue becomes empty
rear = NULL;
}
free(temp);
printf("Dequeued data: %d\n", data);
}
break;
case 3: // Peek
if (front == NULL) {
printf("Queue is empty\n");
} else {
printf("Front element: %d\n", front->data);
}
break;
case 4: // Display Queue
if (front == NULL) {
printf("The queue is empty.\n");
} else {
temp = front;
printf("Queue: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
break;
case 5: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
// Free all allocated memory (cleaning up the queue)
while (front != NULL) {
temp = front;
front = front->next;
free(temp);
}
return 0;
}