-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclonell.cpp
108 lines (104 loc) · 2.74 KB
/
clonell.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// A linked list node
struct Node
{
int data;
struct Node *next;
struct Node *arbit;
};
void append(struct Node** head_ref, int new_data)
{
struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
if(*head_ref == NULL)
{
*head_ref = new_node;
(*head_ref)->next = NULL;
}
else
{
struct Node *temp = *head_ref;
while(temp->next != NULL)
temp = temp->next;
temp->next = new_node;
new_node->next = NULL;
}
}
// This function prints contents of linked list starting from head
void printList(struct Node *node)
{
struct Node *temp = node;
printf("data %d \n",temp->data);
printf("next %d \n",temp->next->data);
printf("arbit %d \n",temp->arbit->data);
printf("data %d \n",temp->next->data);
printf("next %d \n",temp->next->next->data);
printf("arbit %d \n",temp->next->arbit->data);
printf("data %d \n",temp->next->next->data);
printf("next %d \n",temp->next->next->next->data);
printf("arbit%d \n",temp->next->next->arbit->data);
printf("data %d \n",temp->next->next->next->data);
printf("next %d \n",temp->next->next->next->next->data);
printf("arbit %d \n",temp->next->next->next->arbit->data);
printf("data %d \n",temp->next->next->next->next->data);
printf("next NULL \n");
printf("arbit %d \n",temp->next->next->next->next->arbit->data);
}
void clone(struct Node *head)
{
struct Node *temp = head;
struct Node *cln = NULL;
while(temp != NULL)
{
append(&cln,temp->data);
temp = temp->next;
}
struct Node *temp2 = cln;
temp = head;
while(temp2 != NULL)
{
temp2->arbit = temp;
temp = temp -> next;
temp2 = temp2 -> next;
}
temp2 = cln;
temp = head;
struct Node *t;
while(temp2 != NULL)
{
t = temp->next;
temp->next = temp2;
temp2 = temp2->next;
temp = t;
}
temp2 = cln;
temp = head;
while(temp2 != NULL)
{
temp2->arbit = temp2->arbit->arbit->next;
temp2 = temp2->next;
}
printList(cln);
}
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
// Insert 6. So linked list becomes 6->NULL
append(&head, 1);
append(&head, 2);
append(&head, 3);
append(&head, 4);
append(&head, 5);
head->arbit = head->next->next;
head->next->arbit = head;
head->next->next->arbit = head->next->next->next->next;
head->next->next->next->arbit = head->next->next;
head->next->next->next->next->arbit = head->next;
printf("\n Cloned Linked list is: ");
printList(head);
clone(head);
return 0;
}