| | |
insert file data to linked list?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
How do I get the data from file into the Insert function? Or more general how do you get data from a file and put it into a linked list?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; class List { public: void Insert(); void Print(); }; struct node { int age; node *nxt; }; node *start_ptr = 0; node *current; void List::Insert() { 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"); inFile>>number.age; display.Print(); display.Insert(); }
![]() |
Similar Threads
- Sorting A Linked List--- Please help (C++)
- Storing file data in an array? (C++)
- Sorted linked list help (C++)
- ordered linked list (C)
- help with this code of link list (C++)
- recursive linked list (C++)
- Linked List (C)
- Linked List (C++)
Other Threads in the C++ Forum
- Previous Thread: for newbies, what header file to use for graphics?
- Next Thread: Quick question
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





