| | |
linked list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Im just getting garbage in the output. It outputs the correct number of items but its not the right numbers. Im not sure why it is happening.
The text file just has numbers like this:
12
34
56
34
67
23
64
23
The text file just has numbers like this:
12
34
56
34
67
23
64
23
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; class List { public: void Insert(int); void Print(); }; struct node { int age; node *nxt; }; node *start_ptr = 0; node *current; void List::Insert(int age) { node *temp, *temp2; // Temporary pointers // Reserve space for new node and fill it with data temp = new node; temp->age; temp->nxt = NULL; // Set up link to this node if (start_ptr == NULL) { start_ptr = temp; current = start_ptr; } else { temp2 = start_ptr; // We know this is not NULL - list not empty! while (temp2->nxt != NULL) { temp2 = temp2->nxt; // Move to next link in chain } temp2->nxt = temp; } } void List::Print() { node *temp; temp = start_ptr; if (temp == NULL) cout << "The list is empty" << endl; else { while (temp != NULL) { // Display details for what temp points to cout << "Age : " << temp->age << " "; if (temp == current) cout << " <-- Current node"; cout << endl; temp = temp->nxt; } cout << "End of list!" << endl; } } void main() { start_ptr = NULL; List display; fstream inFile; node number; inFile.open("int.txt"); while(inFile>>number.age) { display.Insert(number.age); } display.Print(); }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
Im just getting garbage in the output. It outputs the correct number of items but its not the right numbers. Im not sure why it is happening.
The text file just has numbers like this:
12
34
56
34
67
23
64
23
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; class List { public: void Insert(int); void Print(); }; struct node { int age; node *nxt; }; node *start_ptr = 0; node *current; void List::Insert(int age) { node *temp, *temp2; // Temporary pointers // Reserve space for new node and fill it with data temp = new node; temp->age; temp->nxt = NULL; // Set up link to this node if (start_ptr == NULL) { start_ptr = temp; current = start_ptr; } else { temp2 = start_ptr; // We know this is not NULL - list not empty! while (temp2->nxt != NULL) { temp2 = temp2->nxt; // Move to next link in chain } temp2->nxt = temp; } } void List::Print() { node *temp; temp = start_ptr; if (temp == NULL) cout << "The list is empty" << endl; else { while (temp != NULL) { // Display details for what temp points to cout << "Age : " << temp->age << " "; if (temp == current) cout << " <-- Current node"; cout << endl; temp = temp->nxt; } cout << "End of list!" << endl; } } void main() { start_ptr = NULL; List display; fstream inFile; node number; inFile.open("int.txt"); while(inFile>>number.age) { display.Insert(number.age); } display.Print(); }
These should really be data members of your List class, not global variables:
C++ Syntax (Toggle Plain Text)
node *start_ptr; node *current;
This line does nothing:
C++ Syntax (Toggle Plain Text)
temp->age;
I assume you meant:
C++ Syntax (Toggle Plain Text)
temp->age = age;
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: Quick question
- Next Thread: Twofish Crypto algorithm
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






