DS Lab Programs
DS Lab Programs
OUTPUT:
1. B) Write a C program to delete an element in a single linked list
#include <stdio.h>
#include <stdlib.h>
// Structure for a single linked list node
struct Node {
int data;
struct Node* next;
};
OUTPUT:
2. C program to reverse a linked list both iteratively and recursively
#include <stdio.h>
#include <stdlib.h>
if (*head == NULL) {
*head = newNode;
return;
}
temp->next = newNode;
}
// Main function
int main() {
struct Node* head = NULL;
int n, value;
return 0;
}
OUTPUT:
Exercise 3
1. Create a program to detect and remove duplicates form a linked list
#include <stdio.h>
#include <stdlib.h>
if (*head == NULL) {
*head = newNode;
return;
}
temp->next = newNode;
}
// Main function
int main() {
struct Node* head = NULL;
int n, value;
// Remove duplicates
removeDuplicates(head);
return 0;
}
OUTPUT:
#include <stdio.h>
#include <stdlib.h>
// Main function
int main()
{
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* result = NULL;
int n1, n2, coeff, exp;
// Add polynomials
result = addPolynomials(poly1, poly2);
return 0;
}
OUTPUT:
Exercise 4
1. C Program for Doubly Linked List with Insertion and Deletion Operations
#include <stdio.h>
#include <stdlib.h>
// Structure for a doubly linked list node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
if (*head == NULL) {
*head = newNode;
return;
}
// Function to delete a node from the beginning of the doubly linked list
void deleteAtBegin(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
}
// Function to delete a node from the end of the doubly linked list
void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}
temp->prev->next = NULL;
free(temp);
}
// Main function
int main() {
struct Node* head = NULL;
int choice, value;
while (1) {
printf("\nDoubly Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete from Beginning\n");
printf("4. Delete from End\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBegin(&head, value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
deleteAtBegin(&head);
printf("Node deleted from beginning.\n");
break;
case 4:
deleteAtEnd(&head);
printf("Node deleted from end.\n");
break;
case 5:
printf("Doubly Linked List: ");
display(head);
break;
case 6:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}
OUTPUT:
2. C Program for Circular Linked List with Insertion and Deletion Operations
#include <stdio.h>
#include <stdlib.h>
if (*head == NULL) {
newNode->next = newNode; // Point to itself in circular list
*head = newNode;
return;
}
temp->next = newNode;
newNode->next = *head;
}
if (*head == NULL) {
newNode->next = newNode; // Point to itself
*head = newNode;
return;
}
newNode->next = *head;
temp->next = newNode;
*head = newNode;
}
// Function to delete a node from the beginning of the circular linked list
void deleteAtBegin(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}
*head = (*head)->next;
last->next = *head;
free(temp);
}
// Function to delete a node from the end of the circular linked list
void deleteAtEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty!\n");
return;
}
prev->next = *head;
free(temp);
}
printf("(Head)\n");
}
// Main function
int main() {
struct Node* head = NULL;
int choice, value;
while (1) {
printf("\nCircular Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete from Beginning\n");
printf("4. Delete from End\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBegin(&head, value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
deleteAtBegin(&head);
printf("Node deleted from beginning.\n");
break;
case 4:
deleteAtEnd(&head);
printf("Node deleted from end.\n");
break;
case 5:
printf("Circular Linked List: ");
display(head);
break;
case 6:
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}
OUTPUT:
\
Exercise 5
1. a) Implement a stack using Array
#include <stdio.h>
#define MAX 5
while (1)
{
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
printf("Popped: %d\n", pop());
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
OUTPUT:
1. b) Implement a stack using Linked list
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int pop()
{
if (!top)
{
printf("Stack Underflow!\n");
return -1;
}
struct Node* temp = top;
int value = temp->data;
top = top->next;
free(temp);
return value;
}
void display()
{
struct Node* temp = top;
printf("Stack: ");
while (temp)
{
printf("%d ->", temp->data);
temp = temp->next;
} printf("\n");
}
int main()
{
int choice, value;
while (1)
{
printf("\n1. Push 2. Pop 3. Display 4. Exit\nEnter choice: ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Enter value: ");
scanf("%d", &value);
push(value);
}
else if (choice == 2)
{
printf("Popped: %d\n", pop());
}
else if (choice == 3)
{
display();
}
else if (choice == 4)
{
break;
}
else
{
printf("Invalid choice!\n");
}
}
return 0;
}
OUTPUT:
Example
Evaluate: 53+82-*
Step-by-step Execution:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
int stack[MAX];
int top = -1;
void push(int value)
{
top=top+1;
stack[top] = value;
}
int pop()
{
return stack[top--];
}
int evaluatePostfix(char *exp)
{
for (int i = 0; exp[i] != '\0'; i++)
{
if (isdigit(exp[i]))
{
push(exp[i]-'0'); // Convert character to integer
}
else
{
int val2 = pop();
int val1 = pop();
switch (exp[i])
{
case '+': push(val1 + val2); break;
case '-': push(val1 - val2); break;
case '*': push(val1 * val2); break;
case '/': push(val1 / val2); break;
}
}
}
return pop(); // Final result
}
int main()
{
char exp[MAX];
printf("Enter a postfix expression: ");
scanf("%s", exp);
OUTPUT:
Exercise 6
int queue[SIZE];
int front = -1, rear = -1;
// Main function
int main() {
int choice, value;
while (1) {
printf("\nQueue Operations Menu:\n");
printf("1. Enqueue\n 2. Dequeue\n 3. Display\n 4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
}
OUTPUT:
2. Implementation of Queue using Linked List
#include <stdio.h>
#include <stdlib.h>
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d enqueued to the queue\n", value);
}
// Main function
int main() {
int choice, value;
while (1) {
printf("\nQueue Operations Menu:\n");
printf("1. Enqueue\n 2. Dequeue\n 3. Display\n 4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
OUTPUT:
3. C Program for Circular Queue
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
// Enqueue function
void enqueue(int value) {
if ((rear + 1) % SIZE == front) {
printf("Queue is full\n");
} else {
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
queue[rear] = value;
printf("%d enqueued to the queue\n", value);
}
}
// Dequeue function
void dequeue() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Dequeued element: %d\n", queue[front]);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
}
// Display function
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % SIZE;
}
printf("\n");
}
}
// Main function
int main() {
int choice, value;
while (1) {
printf("\nCircular Queue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
OUTPUT:
4. C Program for Job Scheduling using Queue
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
typedef struct {
int jobId;
int burstTime;
} Job;
Job queue[SIZE];
int front = -1, rear = -1;
void dequeue() {
if (front == -1 || front > rear) {
printf("No jobs to execute.\n");
return;
}
printf("Executing Job %d with burst time %d.\n", queue[front].jobId, queue[front].burstTime);
front++;
}
void displayQueue() {
if (front == -1 || front > rear) {
printf("Queue is empty.\n");
return;
}
printf("Jobs in queue:\n");
for (int i = front; i <= rear; i++) {
printf("Job ID: %d, Burst Time: %d\n", queue[i].jobId, queue[i].burstTime);
}
}
int main() {
int choice, id, time;
while (1) {
printf("\n1. Schedule Job\n2. Execute Job\n3. Show Jobs\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter Job ID and Burst Time: ");
scanf("%d %d", &id, &time);
enqueue(id, time);
break;
case 2:
dequeue();
break;
case 3:
displayQueue();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
OUTPUT:
5. C Program for Double Ended Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int deque[MAX];
int front = -1, rear = -1;
void insertFront(int x) {
if ((front == 0 && rear == MAX - 1) || (front == rear + 1)) {
printf("Deque is full\n");
return;
}
if (front == -1) {
front = rear = 0;
} else if (front == 0) {
front = MAX - 1;
} else {
front--;
}
deque[front] = x;
printf("Inserted %d at front\n", x);
}
void insertRear(int x) {
if ((front == 0 && rear == MAX - 1) || (front == rear + 1)) {
printf("Deque is full\n");
return;
}
if (front == -1) {
front = rear = 0;
} else if (rear == MAX - 1) {
rear = 0;
} else {
rear++;
}
deque[rear] = x;
printf("Inserted %d at rear\n", x);
}
void deleteFront() {
if (front == -1) {
printf("Deque is empty\n");
return;
}
printf("Deleted from front: %d\n", deque[front]);
if (front == rear) {
front = rear = -1;
} else if (front == MAX - 1) {
front = 0;
} else {
front++;
}
}
void deleteRear() {
if (front == -1) {
printf("Deque is empty\n");
return;
}
printf("Deleted from rear: %d\n", deque[rear]);
if (front == rear) {
front = rear = -1;
} else if (rear == 0) {
rear = MAX - 1;
} else {
rear--;
}
}
void display() {
int i = front;
if (front == -1) {
printf("Deque is empty\n");
return;
}
printf("Deque elements: ");
while (1) {
printf("%d ", deque[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
int main() {
int choice, value;
printf("Double Ended Queue (Deque) - User Input Menu\n");
while (1)
{
printf("\n--- MENU ---\n");
printf("1. Insert at Front\n");
printf("2. Insert at Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(value);
break;
case 3:
deleteFront();
break;
case 4:
deleteRear();
break;
case 5:
display();
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
OUTPUT: