-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB5Q2.c
More file actions
80 lines (67 loc) · 1.95 KB
/
LAB5Q2.c
File metadata and controls
80 lines (67 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *prev, *next;
};
struct Node* createNode(int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = newNode->next = NULL;
return newNode;
}
void insertRear(struct Node **head, struct Node **tail, int data) {
struct Node *newNode = createNode(data);
if (*head == NULL) {
*head = *tail = newNode;
} else {
(*tail)->next = newNode;
newNode->prev = *tail;
*tail = newNode;
}
}
void traverse(struct Node *head) {
struct Node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void concatenate(struct Node **head1, struct Node **tail1, struct Node *head2, struct Node *tail2) {
if (*head1 == NULL) {
*head1 = head2;
*tail1 = tail2;
} else if (head2 != NULL) {
(*tail1)->next = head2;
head2->prev = *tail1;
*tail1 = tail2;
}
}
int main() {
struct Node *head1 = NULL, *tail1 = NULL;
struct Node *head2 = NULL, *tail2 = NULL;
int n1, n2, val;
printf("Enter number of elements in list X1: ");
scanf("%d", &n1);
printf("Enter elements of X1: ");
for (int i = 0; i < n1; i++) {
scanf("%d", &val);
insertRear(&head1, &tail1, val);
}
printf("Enter number of elements in list X2: ");
scanf("%d", &n2);
printf("Enter elements of X2: ");
for (int i = 0; i < n2; i++) {
scanf("%d", &val);
insertRear(&head2, &tail2, val);
}
printf("\nList X1 before concatenation: ");
traverse(head1);
printf("List X2 before concatenation: ");
traverse(head2);
concatenate(&head1, &tail1, head2, tail2);
printf("\nAfter concatenation, List X1: ");
traverse(head1);
return 0;
}