| | |
Class Link-List help!?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 30
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
Views: 493 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





