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.
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".
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.