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

Can anyone help me to solve this? It\'s a C++ data structures course. (a) Answer

ID: 3850095 • Letter: C

Question

Can anyone help me to solve this?


It's a C++ data structures course.

(a) Answer questions below about the C++ code in lines 1-12, giving the required value or data type, or else stating the error, if any. struct Node { int info: Node* next: }: Node* n1 = new Node: Node* n2 = new Node: Node* n3 = new Node: n1 rightarrow info = 33: n1 rightarrow next = n2: n2 rightarrow info = 22: n2 rightarrow next = n3: n3 rightarrow info = 11: n3 rightarrow next = NULL: a) What is the value of n1 rightarrow next rightarrow info? b) What is the value of n2 rightarrow next rightarrow next rightarrow info? c) What is the data type of *(n1 rightarrow next rightarrow next)? d) What is the data type of *n3.next? (b) Suppose we want to delete the linked structure starting at node n1. Indicate what exactly is wrong with the code below and fix the problem.

Explanation / Answer

#include<iostream>
using namespace std;

struct Node
{
    int info;
    Node *next;
};


int main()
{
Node *n1=new Node();
Node *n2=new Node();
Node *n3=new Node();
n1->info=33;
n1->next=n2;
n2->info=22;
n2->next=n3;
n3->info=11;
n3->next=NULL;
//After above operation one link list would be created like n1->n2->n3->NULL
//33->22->11->NULL


// 1. value of n1->next->info is 22 as n1->next->info is equal to n2->info;
   cout<<n1->next->info<<endl;
  
/*2. Runtime exception as element n2->next->next->info would be equal to n3->next->info and n3->next
     is equal to NULL so while accessing that element Runtime access voilation error will come */
   cout<<n2->next->next->info<<endl;
  

// 3. Data type of *(n1->next->next) is Struct Node.
     *(n1->next->next);


   /* 4. complilation error because '.' have more precedence then '*'.
   So first n3.next is done as n3 is pointer must be access like n3->next
   so compile time error will come "left of '.next' must have class/struct/union". */
   cout<<*n3.next;
return 0;

}

a) value of n1->next->info is 22

b) Runtime exception as element n2->next->next->info would be equal to n3->next->info and n3->next
     is equal to NULL so while accessing that element Runtime access voilation error will come.

c) Data type of *(n1->next->next) is Struct Node.

d) complilation error because '.' have more precedence then '*'.    So first n3.next is done as n3 is pointer must be access like n3->next so compile time error will come "left of '.next' must have class/struct/union".

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote