From 75507b7abc41ed697cc1c9b8cac0428671477875 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 17 Sep 2024 02:25:54 +0530 Subject: [PATCH] Upload files to "DS/C/Lab/Week7" --- DS/C/Lab/Week7/DSwithLL.c | 144 +++++++++++++++++++++++++++++++++++ DS/C/Lab/Week7/LLConcat.c | 56 ++++++++++++++ DS/C/Lab/Week7/RecursiveLL.c | 54 +++++++++++++ DS/C/Lab/Week7/XYZLL.c | 82 ++++++++++++++++++++ 4 files changed, 336 insertions(+) create mode 100644 DS/C/Lab/Week7/DSwithLL.c create mode 100644 DS/C/Lab/Week7/LLConcat.c create mode 100644 DS/C/Lab/Week7/RecursiveLL.c create mode 100644 DS/C/Lab/Week7/XYZLL.c diff --git a/DS/C/Lab/Week7/DSwithLL.c b/DS/C/Lab/Week7/DSwithLL.c new file mode 100644 index 0000000..ea355da --- /dev/null +++ b/DS/C/Lab/Week7/DSwithLL.c @@ -0,0 +1,144 @@ +// Write a program to implement stack & queue using Singly linked lists. + +#include +#include + +typedef struct node +{ + int data; + struct node *next; +} node; + +node *top = NULL; // For stack +node *front = NULL, *rear = NULL; // For queue + +// Stack operations +void push(int x) +{ + node *newNode = (node*)malloc(sizeof(node)); + newNode->data = x; + newNode->next = top; + top = newNode; +} + +int pop() +{ + if (top == NULL) + { + printf("Stack is empty\n"); + return -1; + } + int x = top->data; + node *temp = top; + top = top->next; + free(temp); + return x; +} + +// Queue operations +void enqueue(int x) +{ + node *newNode = (node*)malloc(sizeof(node)); + newNode->data = x; + newNode->next = NULL; + if (rear == NULL) + { + front = rear = newNode; + return; + } + rear->next = newNode; + rear = newNode; +} + +int dequeue() +{ + if (front == NULL) + { + printf("Queue is empty\n"); + return -1; + } + int x = front->data; + node *temp = front; + front = front->next; + if (front == NULL) + rear = NULL; + free(temp); + return x; +} + +// Function to display stack +void displayStack() +{ + node *temp = top; + while (temp != NULL) + { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); +} + +// Function to display queue +void displayQueue() +{ + node *temp = front; + while (temp != NULL) + { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); +} + +int main() +{ + int choice, x; + while (1) + { + printf("\n1. Push to stack\n"); + printf("2. Pop from stack\n"); + printf("3. Display stack\n"); + printf("4. Enqueue to queue\n"); + printf("5. Dequeue from queue\n"); + printf("6. Display queue\n"); + printf("7. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + switch (choice) + { + case 1: + printf("Enter element to push: "); + scanf("%d", &x); + push(x); + break; + case 2: + x = pop(); + if (x != -1) + printf("Popped element: %d\n", x); + break; + case 3: + printf("Stack: "); + displayStack(); + break; + case 4: + printf("Enter element to enqueue: "); + scanf("%d", &x); + enqueue(x); + break; + case 5: + x = dequeue(); + if (x != -1) + printf("Dequeued element: %d\n", x); + break; + case 6: + printf("Queue: "); + displayQueue(); + break; + case 7: + exit(0); + default: + printf("Invalid choice\n"); + } + } + return 0; +} diff --git a/DS/C/Lab/Week7/LLConcat.c b/DS/C/Lab/Week7/LLConcat.c new file mode 100644 index 0000000..eb6c7c0 --- /dev/null +++ b/DS/C/Lab/Week7/LLConcat.c @@ -0,0 +1,56 @@ +// Let list1 = (x1, x2…..xn) and list2= (y1, y2…..ym). Write a function to merge list1 and +// list2 to obtain list3 = (x1, y1, x2, y2….xm,ym,xm+1…xn) for m<=n; and list3=(x1, +// y1,x2,y2…..xn, yn, xn+1….xm) for m>n. + +#include +#include + +#define MAX_SIZE 100 + +void merge_lists(int list1[], int n, int list2[], int m, int list3[]) { + int i = 0, j = 0, k = 0; + + while (i < n && j < m) { + list3[k++] = list1[i++]; + list3[k++] = list2[j++]; + } + + while (i < n) { + list3[k++] = list1[i++]; + } + + while (j < m) { + list3[k++] = list2[j++]; + } +} + +int main() { + int list1[MAX_SIZE], list2[MAX_SIZE], list3[MAX_SIZE * 2]; + int n, m, i; + + printf("Enter the number of elements in list1: "); + scanf("%d", &n); + + printf("Enter the elements of list1:\n"); + for (i = 0; i < n; i++) { + scanf("%d", &list1[i]); + } + + printf("Enter the number of elements in list2: "); + scanf("%d", &m); + + printf("Enter the elements of list2:\n"); + for (i = 0; i < m; i++) { + scanf("%d", &list2[i]); + } + + merge_lists(list1, n, list2, m, list3); + + printf("Merged list:\n"); + for (i = 0; i < n + m; i++) { + printf("%d ", list3[i]); + } + printf("\n"); + + return 0; +} diff --git a/DS/C/Lab/Week7/RecursiveLL.c b/DS/C/Lab/Week7/RecursiveLL.c new file mode 100644 index 0000000..8653847 --- /dev/null +++ b/DS/C/Lab/Week7/RecursiveLL.c @@ -0,0 +1,54 @@ +// Write recursive functions for i) Creating a linked list ii) Traversing a linked list. + +#include +#include + +struct node { + int data; + struct node *link; +}; + +struct node *create(int); +void traverse(struct node *); +void freeList(struct node *); + +int main() { + struct node *list; + int n; + printf("Enter the number of nodes: "); + scanf("%d", &n); + list = create(n); + printf("Linked list created:\n"); + traverse(list); + freeList(list); + return 0; +} + +struct node *create(int n) { + struct node *temp; + if (n == 0) + return NULL; + temp = (struct node *)malloc(sizeof(struct node)); + if (temp == NULL) { + printf("Memory allocation failed\n"); + exit(1); + } + printf("Enter data: "); + scanf("%d", &temp->data); + temp->link = create(n - 1); + return temp; +} + +void traverse(struct node *list) { + if (list == NULL) + return; + printf("%d ", list->data); + traverse(list->link); +} + +void freeList(struct node *list) { + if (list == NULL) + return; + freeList(list->link); + free(list); +} diff --git a/DS/C/Lab/Week7/XYZLL.c b/DS/C/Lab/Week7/XYZLL.c new file mode 100644 index 0000000..7d2d01a --- /dev/null +++ b/DS/C/Lab/Week7/XYZLL.c @@ -0,0 +1,82 @@ +// Let X = (x1, x2….xn) and Y = (y1, y2….yn) be 2 linked lists. Assume that, in each list, +// the nodes are in non-decreasing order of the data field values. Write an algorithm to +// merge two lists to obtain a new linked list Z in which the nodes are also in the non- +// decreasing order. Following the merge, X and Y do not exist as individual lists. Each +// node initially in X or Y is now in Z. Do not use additional nodes. + +#include +#include + +typedef struct node *nodePtr; +struct node { + int data; + nodePtr link; +}; + +nodePtr merge(nodePtr x, nodePtr y) +{ + nodePtr z, temp; + if (!x) return y; + if (!y) return x; + if (x->data <= y->data) { + z = x; + x = x->link; + } else { + z = y; + y = y->link; + } + temp = z; + while (x && y) { + if (x->data <= y->data) { + temp->link = x; + x = x->link; + } else { + temp->link = y; + y = y->link; + } + temp = temp->link; + } + if (x) temp->link = x; + if (y) temp->link = y; + return z; +} + +void printList(nodePtr head) +{ + while (head) { + printf("%d ", head->data); + head = head->link; + } + printf("\n"); +} + +nodePtr createNode(int data) +{ + nodePtr newNode = (nodePtr)malloc(sizeof(struct node)); + newNode->data = data; + newNode->link = NULL; + return newNode; +} + +int main() +{ + nodePtr x = createNode(1); + x->link = createNode(3); + x->link->link = createNode(5); + + nodePtr y = createNode(2); + y->link = createNode(4); + y->link->link = createNode(6); + + printf("List X: "); + printList(x); + printf("List Y: "); + printList(y); + + nodePtr z = merge(x, y); + + printf("Merged list Z: "); + printList(z); + + return 0; +}