| | |
Class Link-List help!?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 27
Reputation:
Solved Threads: 1
I am stuck, im trying to insert and print out this list of numbers from a file, however i just get an infinite loop every time... any suggestions?
c++ Syntax (Toggle Plain Text)
// This is the implementation file for class List #include <iostream> #include <stddef.h> // to access NULL #include "List.h" #include<fstream.h> using namespace std; typedef NodeType* NodePtr; struct NodeType { ItemType item; NodePtr next; }; List::List() // Post: listPtr is set to NULL. { listPtr = NULL; length = 0; } //*********************************************************** //List::List(const List& otherList) // Copy-constructor for List. //{ //} //*********************************************************** bool List::IsThere(ItemType item) const // Post: If item is in the list IsThere is // True; False, otherwise. { NodeType *cur_pos; cur_pos = listPtr; if (cur_pos == NULL) return false; while((cur_pos -> next)!= NULL) { if (cur_pos -> item == item) return true; cur_pos = cur_pos -> next; } if (cur_pos -> item == item) return true; return false; } //*********************************************************** void List::Insert(ItemType item) // Pre: item is not already in the list. // Post: item is the first item in the list. { NodeType *ptr; ptr = new NodeType; ptr->item; ptr->next = listPtr; listPtr = ptr; delete ptr; length++; } //*********************************************************** void List::Delete(ItemType item) // Pre: item is in the list. // Post: item is no longer in the list. { // FILL IN Code. } //*********************************************************** void List::Print() const // Post: Items on the list are printed on the screen. { if (listPtr == NULL) cout << "List is empty\n"; else { NodeType *ptr; ptr = listPtr; while(ptr->next !=NULL) { cout << ptr->item << endl; ptr = ptr->next; } cout << ptr->item << endl; delete ptr; } } //*********************************************************** int List::Length() const // Post: Number of items have been counted; result returned. { return length; } //*********************************************************** List::~List() // Post: All the components are deleted. { // FILL IN Code. } int main() { List mylist; ifstream data; data.open("int.dat"); if (!data) cout << "Error opening file\n"; else { int item; while (!data.eof()) { data >> item; cout << "Item: " << item << endl; mylist.Insert(item); } mylist.Print(); } system("PAUSE"); return 0; }
![]() |
Similar Threads
- Single Linked Circular Link List (Java)
- Binary Tree with Link List (C++)
- help with this code of link list (C++)
- Link list taking an object... (C)
- Link List Sorting Problem (C++)
- Link List Insert problems (C++)
- Using a class to add/delete/show numbers in a Link List (C++)
- class polynomial (C++)
Other Threads in the C++ Forum
- Previous Thread: Game evaluation - Mastermind
- Next Thread: Trouble with loops - Calculate sum of even and odd integers
| Thread Tools | Search this Thread |
api array based binary 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 integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





