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

Consider a linked chain of three nodes, such that each node contains an integer

ID: 653220 • Letter: C

Question

Consider a linked chain of three nodes, such that each node contains an integer data, the first node entered contains 10, the second node entered contains 20 and the third node entered contains 30.

(a) Write C++ code that creates the described linked chain beginning with the head pointer topptr that contains NULL, and inserts the nodes in the order they are entered last in first out, 30, 20, 10.

The node definition is

int data;

node * next;

Explanation / Answer

#include #include #include struct node { int data; struct node *next; }*toptr=NULL; void main() { addNode(); displayNode(); } void addNode() {for(int i=0;idata); new_node->next=NULL; if(toptr==NULL) { toptr=new_node; ptr=new_node; } else { ptr=new_node; ptr->next=toptr; toptr=new_node; } }} void displayNode() { struct node *inode; printf("The Linked Chaim : n"); inode=toptr; while(inode!=NULL) { printf("%d--->",inode->data); inode=inode->next; } printf("NULL"); }
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