0% found this document useful (0 votes)
43 views6 pages

Assignment - 3: Siddharth Singh (E3) 9918102127

The document contains C++ code to flatten a linked list that contains child nodes by connecting each node to its child node and adjusting the next pointers. The code defines a Node class with data, next, and child pointers, creates sample linked lists with child relationships, and includes a flattenList function that iterates through the list and connects each node to its child by adjusting the next pointers to flatten the overall list structure.

Uploaded by

Siddharth Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

Assignment - 3: Siddharth Singh (E3) 9918102127

The document contains C++ code to flatten a linked list that contains child nodes by connecting each node to its child node and adjusting the next pointers. The code defines a Node class with data, next, and child pointers, creates sample linked lists with child relationships, and includes a flattenList function that iterates through the list and connects each node to its child by adjusting the next pointers to flatten the overall list structure.

Uploaded by

Siddharth Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Siddharth Singh(E3)

9918102127

Assignment -3

Q1.

Code:

#include <bits/stdc++.h>

using namespace std;

#define SIZE(arr) (sizeof(arr)/sizeof(arr[0]))

class Node

public:

int data;

Node *next;

Node *child;

};

Node *createList(int *arr, int n)

Node *head = NULL;


Node *p;

int i;

for (i = 0; i < n; ++i)

if (head == NULL)

head = p = new Node();

else

p->next = new Node();

p = p->next;

p->data = arr[i];

p->next = p->child = NULL;

return head;

void printList(Node *head)

while (head != NULL)

cout << head->data << " ";

head = head->next;

cout<<endl;

}
Node *createList(void)

int arr1[] = {10, 5, 12, 7, 11};

int arr2[] = {4, 20, 13};

int arr3[] = {17, 6};

int arr4[] = {9, 8};

int arr5[] = {19, 15};

int arr6[] = {2};

int arr7[] = {16};

int arr8[] = {3};

Node *head1 = createList(arr1, SIZE(arr1));

Node *head2 = createList(arr2, SIZE(arr2));

Node *head3 = createList(arr3, SIZE(arr3));

Node *head4 = createList(arr4, SIZE(arr4));

Node *head5 = createList(arr5, SIZE(arr5));

Node *head6 = createList(arr6, SIZE(arr6));

Node *head7 = createList(arr7, SIZE(arr7));

Node *head8 = createList(arr8, SIZE(arr8));

head1->child = head2;

head1->next->next->next->child = head3;

head3->child = head4;

head4->child = head5;
head2->next->child = head6;

head2->next->next->child = head7;

head7->child = head8;

return head1;

void flattenList(Node *head)

if (head == NULL)

return;

Node *tmp;

Node *tail = head;

while (tail->next != NULL)

tail = tail->next;

Node *cur = head;

while (cur != tail)

if (cur->child)

{
tail->next = cur->child;

tmp = cur->child;

while (tmp->next)

tmp = tmp->next;

tail = tmp;

cur = cur->next;

int main(void)

Node *head = NULL;

head = createList();

flattenList(head);

printList(head);

return 0;

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy