Upload files to "DS/C/Lab/Week8"
This commit is contained in:
parent
75507b7abc
commit
e9a4ce08c7
82
DS/C/Lab/Week8/DLLAdd.c
Normal file
82
DS/C/Lab/Week8/DLLAdd.c
Normal file
@ -0,0 +1,82 @@
|
||||
// Write a program to implement addition of two long positive integer numbers using doubly linked list.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_DIGITS 100
|
||||
|
||||
typedef struct node {
|
||||
int digit;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
} Node;
|
||||
|
||||
Node* createNode(int digit) {
|
||||
Node* newNode = (Node*)malloc(sizeof(Node));
|
||||
newNode->digit = digit;
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
return newNode;
|
||||
}
|
||||
|
||||
void insertDigit(Node** head, int digit) {
|
||||
Node* newNode = createNode(digit);
|
||||
if (*head == NULL) {
|
||||
*head = newNode;
|
||||
} else {
|
||||
newNode->right = *head;
|
||||
(*head)->left = newNode;
|
||||
*head = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
void addNumbers(Node* num1, Node* num2, Node** result) {
|
||||
int carry = 0, sum;
|
||||
Node *ptr1 = num1, *ptr2 = num2;
|
||||
|
||||
while (ptr1 || ptr2 || carry) {
|
||||
sum = carry;
|
||||
if (ptr1) {
|
||||
sum += ptr1->digit;
|
||||
ptr1 = ptr1->right;
|
||||
}
|
||||
if (ptr2) {
|
||||
sum += ptr2->digit;
|
||||
ptr2 = ptr2->right;
|
||||
}
|
||||
insertDigit(result, sum % 10);
|
||||
carry = sum / 10;
|
||||
}
|
||||
}
|
||||
|
||||
void printNumber(Node* head) {
|
||||
if (head == NULL) return;
|
||||
printNumber(head->right);
|
||||
printf("%d", head->digit);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Node *num1 = NULL, *num2 = NULL, *result = NULL;
|
||||
char str[MAX_DIGITS];
|
||||
int i;
|
||||
|
||||
printf("Enter first number: ");
|
||||
scanf("%s", str);
|
||||
for (i = 0; str[i] != '\0'; i++) {
|
||||
insertDigit(&num1, str[i] - '0');
|
||||
}
|
||||
|
||||
printf("Enter second number: ");
|
||||
scanf("%s", str);
|
||||
for (i = 0; str[i] != '\0'; i++) {
|
||||
insertDigit(&num2, str[i] - '0');
|
||||
}
|
||||
|
||||
addNumbers(num1, num2, &result);
|
||||
|
||||
printf("Sum: ");
|
||||
printNumber(result);
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
78
DS/C/Lab/Week8/DLLConcat.c
Normal file
78
DS/C/Lab/Week8/DLLConcat.c
Normal file
@ -0,0 +1,78 @@
|
||||
//Write a program to concatenate two doubly linked lists X1 and X2. After
|
||||
// concatenation X1 is a pointer to first node of the resulting lists.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct node {
|
||||
int data;
|
||||
struct node *llink;
|
||||
struct node *rlink;
|
||||
} *node_pointer;
|
||||
|
||||
void concatenate(node_pointer *X1, node_pointer *X2) {
|
||||
node_pointer temp;
|
||||
if (*X1 == NULL) {
|
||||
*X1 = *X2;
|
||||
} else if (*X2 != NULL) {
|
||||
temp = *X1;
|
||||
while (temp->rlink != NULL)
|
||||
temp = temp->rlink;
|
||||
temp->rlink = *X2;
|
||||
(*X2)->llink = temp;
|
||||
}
|
||||
}
|
||||
|
||||
node_pointer create_node(int data) {
|
||||
node_pointer new_node = (node_pointer)malloc(sizeof(struct node));
|
||||
new_node->data = data;
|
||||
new_node->llink = NULL;
|
||||
new_node->rlink = NULL;
|
||||
return new_node;
|
||||
}
|
||||
|
||||
void insert_end(node_pointer *head, int data) {
|
||||
node_pointer new_node = create_node(data);
|
||||
if (*head == NULL) {
|
||||
*head = new_node;
|
||||
} else {
|
||||
node_pointer temp = *head;
|
||||
while (temp->rlink != NULL)
|
||||
temp = temp->rlink;
|
||||
temp->rlink = new_node;
|
||||
new_node->llink = temp;
|
||||
}
|
||||
}
|
||||
|
||||
void print_list(node_pointer head) {
|
||||
node_pointer temp = head;
|
||||
while (temp != NULL) {
|
||||
printf("%d ", temp->data);
|
||||
temp = temp->rlink;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
node_pointer X1 = NULL, X2 = NULL;
|
||||
|
||||
insert_end(&X1, 1);
|
||||
insert_end(&X1, 2);
|
||||
insert_end(&X1, 3);
|
||||
|
||||
insert_end(&X2, 4);
|
||||
insert_end(&X2, 5);
|
||||
|
||||
printf("X1: ");
|
||||
print_list(X1);
|
||||
printf("X2: ");
|
||||
print_list(X2);
|
||||
|
||||
concatenate(&X1, &X2);
|
||||
|
||||
printf("After concatenation:\n");
|
||||
printf("X1: ");
|
||||
print_list(X1);
|
||||
|
||||
return 0;
|
||||
}
|
90
DS/C/Lab/Week8/DLLUnionIntersec.c
Normal file
90
DS/C/Lab/Week8/DLLUnionIntersec.c
Normal file
@ -0,0 +1,90 @@
|
||||
// Write a program to implement union and intersection of two doubly linked lists.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct node {
|
||||
int data;
|
||||
struct node *prev;
|
||||
struct node *next;
|
||||
};
|
||||
|
||||
typedef struct node *NODE;
|
||||
|
||||
NODE getnode() {
|
||||
NODE x;
|
||||
x = (NODE)malloc(sizeof(struct node));
|
||||
if(x == NULL) {
|
||||
printf("Out of memory\n");
|
||||
exit(0);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
void insert_rear(NODE *head, int item) {
|
||||
NODE temp, cur;
|
||||
temp = getnode();
|
||||
temp->data = item;
|
||||
temp->next = NULL;
|
||||
if(*head == NULL) {
|
||||
temp->prev = NULL;
|
||||
*head = temp;
|
||||
return;
|
||||
}
|
||||
cur = *head;
|
||||
while(cur->next != NULL)
|
||||
cur = cur->next;
|
||||
cur->next = temp;
|
||||
temp->prev = cur;
|
||||
}
|
||||
|
||||
NODE union_lists(NODE head1, NODE head2) {
|
||||
NODE result = NULL;
|
||||
NODE temp = head1;
|
||||
while(temp != NULL) {
|
||||
insert_rear(&result, temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
temp = head2;
|
||||
while(temp != NULL) {
|
||||
NODE cur = result;
|
||||
int found = 0;
|
||||
while(cur != NULL) {
|
||||
if(cur->data == temp->data) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
if(!found)
|
||||
insert_rear(&result, temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
NODE intersection_lists(NODE head1, NODE head2) {
|
||||
NODE result = NULL;
|
||||
NODE temp = head1;
|
||||
while(temp != NULL) {
|
||||
NODE cur = head2;
|
||||
while(cur != NULL) {
|
||||
if(temp->data == cur->data) {
|
||||
insert_rear(&result, temp->data);
|
||||
break;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void display(NODE head) {
|
||||
NODE temp = head;
|
||||
while(temp != NULL) {
|
||||
printf("%d ", temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
256
DS/C/Lab/Week8/DoublyLL.c
Normal file
256
DS/C/Lab/Week8/DoublyLL.c
Normal file
@ -0,0 +1,256 @@
|
||||
//Write a menu driven program to perform the following on a doubly linked list
|
||||
//DS LAB MANUAL
|
||||
// i.) Insert an element at the rear end of the list
|
||||
// ii.) Delete an element from the rear end of the list
|
||||
// iii.) Insert an element at a given position of the list
|
||||
// iv.) Delete an element from a given position of the list
|
||||
// v.) Insert an element after another element
|
||||
// vi.) Insert an element before another element
|
||||
// vii.) Traverse the list
|
||||
// viii.) Reverse the list
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node* prev;
|
||||
struct Node* next;
|
||||
};
|
||||
|
||||
struct Node* head = NULL;
|
||||
|
||||
void insertRear(int data) {
|
||||
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
|
||||
newNode->data = data;
|
||||
newNode->next = NULL;
|
||||
|
||||
if (head == NULL) {
|
||||
newNode->prev = NULL;
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
|
||||
struct Node* temp = head;
|
||||
while (temp->next != NULL)
|
||||
temp = temp->next;
|
||||
|
||||
temp->next = newNode;
|
||||
newNode->prev = temp;
|
||||
}
|
||||
|
||||
void deleteRear() {
|
||||
if (head == NULL) {
|
||||
printf("List is empty\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (head->next == NULL) {
|
||||
free(head);
|
||||
head = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
struct Node* temp = head;
|
||||
while (temp->next != NULL)
|
||||
temp = temp->next;
|
||||
|
||||
temp->prev->next = NULL;
|
||||
free(temp);
|
||||
}
|
||||
|
||||
void insertPosition(int data, int position) {
|
||||
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
|
||||
newNode->data = data;
|
||||
|
||||
if (position == 1) {
|
||||
newNode->prev = NULL;
|
||||
newNode->next = head;
|
||||
if (head != NULL)
|
||||
head->prev = newNode;
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
|
||||
struct Node* temp = head;
|
||||
for (int i = 1; i < position - 1 && temp != NULL; i++)
|
||||
temp = temp->next;
|
||||
|
||||
if (temp == NULL) {
|
||||
printf("Position out of range\n");
|
||||
return;
|
||||
}
|
||||
|
||||
newNode->next = temp->next;
|
||||
newNode->prev = temp;
|
||||
if (temp->next != NULL)
|
||||
temp->next->prev = newNode;
|
||||
temp->next = newNode;
|
||||
}
|
||||
|
||||
void deletePosition(int position) {
|
||||
if (head == NULL) {
|
||||
printf("List is empty\n");
|
||||
return;
|
||||
}
|
||||
|
||||
struct Node* temp = head;
|
||||
|
||||
if (position == 1) {
|
||||
head = head->next;
|
||||
if (head != NULL)
|
||||
head->prev = NULL;
|
||||
free(temp);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 1; i < position && temp != NULL; i++)
|
||||
temp = temp->next;
|
||||
|
||||
if (temp == NULL) {
|
||||
printf("Position out of range\n");
|
||||
return;
|
||||
}
|
||||
|
||||
temp->prev->next = temp->next;
|
||||
if (temp->next != NULL)
|
||||
temp->next->prev = temp->prev;
|
||||
free(temp);
|
||||
}
|
||||
|
||||
void insertAfter(int data, int key) {
|
||||
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
|
||||
newNode->data = data;
|
||||
|
||||
struct Node* temp = head;
|
||||
while (temp != NULL && temp->data != key)
|
||||
temp = temp->next;
|
||||
|
||||
if (temp == NULL) {
|
||||
printf("Key not found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
newNode->next = temp->next;
|
||||
newNode->prev = temp;
|
||||
if (temp->next != NULL)
|
||||
temp->next->prev = newNode;
|
||||
temp->next = newNode;
|
||||
}
|
||||
|
||||
void insertBefore(int data, int key) {
|
||||
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
|
||||
newNode->data = data;
|
||||
|
||||
if (head == NULL) {
|
||||
printf("List is empty\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (head->data == key) {
|
||||
newNode->next = head;
|
||||
newNode->prev = NULL;
|
||||
head->prev = newNode;
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
|
||||
struct Node* temp = head;
|
||||
while (temp->next != NULL && temp->next->data != key)
|
||||
temp = temp->next;
|
||||
|
||||
if (temp->next == NULL) {
|
||||
printf("Key not found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
newNode->next = temp->next;
|
||||
newNode->prev = temp;
|
||||
temp->next->prev = newNode;
|
||||
temp->next = newNode;
|
||||
}
|
||||
|
||||
void traverse() {
|
||||
struct Node* temp = head;
|
||||
while (temp != NULL) {
|
||||
printf("%d ", temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void reverse() {
|
||||
struct Node* temp = NULL;
|
||||
struct Node* current = head;
|
||||
|
||||
while (current != NULL) {
|
||||
temp = current->prev;
|
||||
current->prev = current->next;
|
||||
current->next = temp;
|
||||
current = current->prev;
|
||||
}
|
||||
|
||||
if (temp != NULL)
|
||||
head = temp->prev;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int choice, data, position, key;
|
||||
|
||||
while (1) {
|
||||
printf("\n1. Insert at rear\n2. Delete from rear\n3. Insert at position\n");
|
||||
printf("4. Delete from position\n5. Insert after element\n6. Insert before element\n");
|
||||
printf("7. Traverse\n8. Reverse\n9. Exit\n");
|
||||
printf("Enter your choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
printf("Enter data to insert: ");
|
||||
scanf("%d", &data);
|
||||
insertRear(data);
|
||||
break;
|
||||
case 2:
|
||||
deleteRear();
|
||||
break;
|
||||
case 3:
|
||||
printf("Enter data to insert: ");
|
||||
scanf("%d", &data);
|
||||
printf("Enter position: ");
|
||||
scanf("%d", &position);
|
||||
insertPosition(data, position);
|
||||
break;
|
||||
case 4:
|
||||
printf("Enter position to delete: ");
|
||||
scanf("%d", &position);
|
||||
deletePosition(position);
|
||||
break;
|
||||
case 5:
|
||||
printf("Enter data to insert: ");
|
||||
scanf("%d", &data);
|
||||
printf("Enter element after which to insert: ");
|
||||
scanf("%d", &key);
|
||||
insertAfter(data, key);
|
||||
break;
|
||||
case 6:
|
||||
printf("Enter data to insert: ");
|
||||
scanf("%d", &data);
|
||||
printf("Enter element before which to insert: ");
|
||||
scanf("%d", &key);
|
||||
insertBefore(data, key);
|
||||
break;
|
||||
case 7:
|
||||
traverse();
|
||||
break;
|
||||
case 8:
|
||||
reverse();
|
||||
break;
|
||||
case 9:
|
||||
exit(0);
|
||||
default:
|
||||
printf("Invalid choice\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user