| | |
need help with Linked list
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 15
Reputation:
Solved Threads: 0
Hey, Im having some trouble with linked lists, I have been givin a task where I am asked to crreate a linked list which reads in a random set of chars from a .txt file and stores them in a linked list, then prints them out in a seperate function.
here is my code
text file contains random chars, like "AINFUEH EIUDNUIEW IWEUNF" and whitespaces are ignored.
for some reason, when it prints out, it gets stuck in a loop and only prints out the last char in the text file over and over...
can someone please tell me what im doing wrong?
here is my code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; struct node { char base; node* next; }; void initialise(node*& head); void read(node*& head); void print(node*& head); int main() { node* head = NULL; cout << "Set linked list to NULL" << endl; initialise(head); cout << "Reading from file into linked list" << endl; read(head); cout << "Printing out linked list" << endl; print(head); return 0; } void initialise(node*& head) { head = NULL; } void read(node*& head) { node* temp; char filename[40]; ifstream ins; cout << "Filename: "; cin >> filename; ins.open(filename, ios::in); temp = new node; if (temp == NULL) { cout << "An error occured: " << endl; exit(0); } if(ins.good()) { while(!ins.eof()) { ins >> temp->base; temp->next = NULL; if (head == NULL) head = temp; else { node* z; z = head; while (z->next != NULL) { z = z->next; } z->next=temp; } } } } void print(node*& head) { node* temp; temp = head; while (temp != NULL) { cout << temp->base << endl; temp = temp->next; } }
text file contains random chars, like "AINFUEH EIUDNUIEW IWEUNF" and whitespaces are ignored.
for some reason, when it prints out, it gets stuck in a loop and only prints out the last char in the text file over and over...
can someone please tell me what im doing wrong?
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
Looks to me like no new nodes are created in this loop:
You are just continually putting a new value into temp->base and overwriting the old value. As for the infinite loop, I haven't tested it out, but I'm guessing that temp == temp->next and possibly head == temp. You need to create a new node every time you read a new value from the input file.
C++ Syntax (Toggle Plain Text)
while(!ins.eof()) { ins >> temp->base; temp->next = NULL; if (head == NULL) head = temp; else { node* z; z = head; while (z->next != NULL) { z = z->next; } z->next=temp; } }
You are just continually putting a new value into temp->base and overwriting the old value. As for the infinite loop, I haven't tested it out, but I'm guessing that temp == temp->next and possibly head == temp. You need to create a new node every time you read a new value from the input file.
•
•
Join Date: Sep 2008
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
You are just continually putting a new value into temp->base and overwriting the old value.

+rep
Last edited by opposition; Sep 9th, 2008 at 4:47 am.
![]() |
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: Binary min heap
- Next Thread: Speed and memory in STL lists as opposed to vectors
Views: 770 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






