Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/ Q8. If the following C++ code is valid, show the output. If it is invalid, exp

ID: 3594400 • Letter: #

Question

/ Q8. If the following C++ code is valid, show the output. If it is invalid, explain why temp current; //Line 1 current current->link; //Line 2 current->link = last; //une 3 trail = arrent->link; //Line 1 trail trail.>link; //Lines 09. Assume that the node of a linked list is in the usual info-link form with the info of type int. The following data, as described in parts (a) to (d). is to be inserted into an initially linked list: 72, 43, 8, 12 Suppose that head is a pointer of type nodeType. After the linked list is created, head should point to the first node of the list. Doclare additional variables as you ncod them. Write the C++ code to create the linked list. After the linked list is created, write a code to print the list. What is the output of your code? a. Insert 72 into an empty linked list. b. Insert 43 before 72. e. Insert 8 at the end of the list. d. Insert 12 after 43. e write a code to print the list f what is the output of this code?

Explanation / Answer

9.

#include <stdio.h>

#include <stdlib.h>

struct Node

{

int data;

struct Node *next;

};

/* Given a reference (pointer to pointer) to the head of a list and

an int, inserts a new node on the front of the list. */

void push(struct Node** head_ref, int new_data)

{

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node; //move the head to point to the new node

}

/* Given a node prev_node, insert a new node after the given

prev_node */

void insertAfter(struct Node* prev_node, int new_data)

{

if (prev_node == NULL)

{

printf("the given previous node cannot be NULL");

return;

}

struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = prev_node->next; //Making next of new node as next of prev_node

prev_node->next = new_node; //move the next of prev_node as new_node

}

/* Given a reference (pointer to pointer) to the head

of a list and an int, appends a new node at the end */

void append(struct Node** head_ref, int new_data)

{

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

struct Node *last = *head_ref;

new_node->data = new_data;

new_node->next = NULL;

if (*head_ref == NULL) //If the Linked List is empty, then make the new node as head

{

*head_ref = new_node;

return;

}

while (last->next != NULL) //Else traverse till the last node

last = last->next;

last->next = new_node; //Change the next of last node

return;

}

void printList(struct Node *node) //printing linked list starting from head

{

while (node != NULL)

{

printf(" %d ", node->data);

node = node->next;

}

}

/* Driver program to test above functions*/

int main()

{

/* Start with the empty list */

struct Node* head = NULL;

// Insert 72.

append(&head, 72);

// Insert 43 at the beginning.

push(&head, 43);

// Insert 8 at the end.

append(&head, 8);

// Insert 12, after 43.

insertAfter(head, 12);

printf(" Created Linked list is: ");

printList(head);

return 0;

}

Output:

43 12 72 8