Answered by:
printing linked list

Question
-
I am practicing to master implementation of linked lists for my advanced data structures class. Your help would be great.
#include<iostream> using namespace std; //Every node in the linked list is an instance of the following class class IntSLLNode { //default constructor public: int info; IntSLLNode *next; IntSLLNode() { next = 0; } //overloaded constructor IntSLLNode(int i, IntSLLNode *in = 0) //node pointer with default parameter { info = i; next = in; } void print(IntSLLNode *p) { while (p->next != 0) { cout << p->info<<endl; //outputs 10, 8 p = p->next; } } }; //----main--------- #include"LinkedList.h" int main() { IntSLLNode *p = new IntSLLNode(10); p->next = new IntSLLNode(8); p->next->next = new IntSLLNode(50); p->print(p); return 0; }
The "print" methods just prints out 10 and 8 instead of 10,8,50
Saturday, August 8, 2015 6:39 AM
Answers
-
void print(IntSLLNode *p) { while (p->next != 0) { cout << p->info<<endl; //outputs 10, 8 p = p->next; } } }; The "print" methods just prints out 10 and 8 instead of 10,8,50
Well, the last class instance in the list won't have a "next" pointer as there won't be
another instance following it in the list. Fix your "print" logic.
- WayneAlso, your loop assumes that there will always be at least one instance in the list.
That is, it assumes that p will not be 0 when the loop starts. This will
result in a run time error from cout if you pass a zero pointer to print();
You can fix both problems like this:
while (p != 0) { cout << p->info<<endl; p = p->next; }
- Wayne
- Proposed as answer by WayneAKing Saturday, August 8, 2015 8:03 AM
- Marked as answer by askatral Monday, August 10, 2015 6:11 AM
Saturday, August 8, 2015 7:52 AM
All replies
-
void print(IntSLLNode *p) { while (p->next != 0) { cout << p->info<<endl; //outputs 10, 8 p = p->next; } } }; //----main--------- #include"LinkedList.h" int main() { IntSLLNode *p = new IntSLLNode(10); p->next = new IntSLLNode(8); p->next->next = new IntSLLNode(50); p->print(p); return 0; }
The "print" methods just prints out 10 and 8 instead of 10,8,50
Well, the last class instance in the list won't have a "next" pointer as there won't be
another instance following it in the list. Fix your "print" logic.
- WayneSaturday, August 8, 2015 7:29 AM -
void print(IntSLLNode *p) { while (p->next != 0) { cout << p->info<<endl; //outputs 10, 8 p = p->next; } } }; The "print" methods just prints out 10 and 8 instead of 10,8,50
Well, the last class instance in the list won't have a "next" pointer as there won't be
another instance following it in the list. Fix your "print" logic.
- WayneAlso, your loop assumes that there will always be at least one instance in the list.
That is, it assumes that p will not be 0 when the loop starts. This will
result in a run time error from cout if you pass a zero pointer to print();
You can fix both problems like this:
while (p != 0) { cout << p->info<<endl; p = p->next; }
- Wayne
- Proposed as answer by WayneAKing Saturday, August 8, 2015 8:03 AM
- Marked as answer by askatral Monday, August 10, 2015 6:11 AM
Saturday, August 8, 2015 7:52 AM -
I corrected it by testing pointer p itself as it has been assigned next for every iteration
void print(IntSLLNode *p)
{
while (p != 0)
{
cout << p->info<<endl; //outputs 10, 8
p = p->next;
}
}Thanks for your help!
-Apuroopa
- Edited by askatral Saturday, August 8, 2015 7:56 AM
Saturday, August 8, 2015 7:56 AM