DS Programs
DS Programs
#include <stdio.h>
#include <stdlib.h>
// 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 4: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
// 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;
#include <stdio.h>
#include <stdlib.h>
// 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 4: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
// 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;
#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 4: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
// 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;
}
return head;
}
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 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>
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 5: // Exit
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
5. Write a program that implement Queue (its operations) using
i) Arrays ii) Pointers
i) Arrays
#include <stdio.h>
#include <stdlib.h>
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 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>
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;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}