| | |
Set Class - Insert Function
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 11
Reputation:
Solved Threads: 0
I am working on a set class and I have successfully written an insert function, but I need the function to insert in an ordered fashion.
Tell me if anything else is needed
C++ Syntax (Toggle Plain Text)
bool Set::insert( const EType & A ) { bool Flag = false; unsigned Position = 0; Node * Prev = Head; Node * Curr = Head->Succ; Node * Temp; Temp = new Node; if (Temp != NULL ) { Temp->Item = A; Temp->Succ = Curr; Prev->Succ = Temp; Num++; Flag = true; } return Flag; }
C++ Syntax (Toggle Plain Text)
Set::Set() { Num = 0; Head = new (nothrow) Node; Head->Succ = NULL; }
C++ Syntax (Toggle Plain Text)
class Set { private: struct Node { EType Item; // User data item Node * Succ; // Link to the node's successor }; unsigned Num; // Number of user data items in the set Node * Head; // Link to the head of the chain
Tell me if anything else is needed
•
•
Join Date: Mar 2009
Posts: 11
Reputation:
Solved Threads: 0
I tried doing that several separate times, but i cannot avoid a segmentation fault.
C++ Syntax (Toggle Plain Text)
bool Set::insert( const EType & A ) { bool Flag = false; unsigned Position = 0; Node * Prev = Head; Node * Curr = Head->Succ; while ( (Curr->Item < A) and (Curr->Succ != NULL) ) { Prev = Curr; Curr = Curr->Succ; cout << Curr->Item; } if ( (Curr->Item != A) or (Curr == NULL) ) { Node * Temp; Temp = new Node; Temp->Item = A; Temp->Succ = Curr; Prev->Succ = Temp; Num++; Flag = true; } return Flag; }
cpp Syntax (Toggle Plain Text)
while ( (Curr->Item < A) and (Curr->Succ != NULL) ) { Prev = Curr; Curr = Curr->Succ; cout << Curr->Item; }
That code sets Curr to the last item in the set that's smaller than A.
And then you compare the last item (curr->item) to A (to guarantee uniqueness) or NULL for the last item. And there's your problem I think, hehe.
What if Curr is NULL and you try to dereference Curr->item? Segfault. I hope that's the error.
So, correct code would be something like..
cpp Syntax (Toggle Plain Text)
if(Curr == NULL){ //create new item, append it to the set } else if (Curr->Item != A) { //create new item, insert it between the two items. }
Last edited by Clockowl; Apr 13th, 2009 at 2:44 pm.
![]() |
Similar Threads
- Need help with a Reduce Fraction as part of a Rational Class (C++)
- insert csv file into mysql through php (PHP)
- insert file data to linked list? (C++)
- Simulation for CPU Scheduling (C++)
- over-ride inaccessable javascript function? (JavaScript / DHTML / AJAX)
- VB.NET Project Help, will pay (Software Development Job Offers)
- Passing a Function, function pointer (C++)
- Unable to insert data into SQL Database (ASP)
- Modifying Class for Insert to LstBox (VB.NET)
- This ought to be simple - extra spaces (PHP)
Other Threads in the C++ Forum
- Previous Thread: Read-file using File Dump
- Next Thread: a new script language in C++...
Views: 612 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment 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 multidimensional 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 text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





