| | |
linked list small problem with Insert Function
![]() |
•
•
Join Date: May 2005
Posts: 42
Reputation:
Solved Threads: 0
the following code will print out all the names of patients I have in a linked list.
I have given the user an option to choose which patient they would like to move in the linked list.
i thought if I called S.insert(passed the new postition, and P (where P is the object of the patient class) it would work.
then all I had to do was remove the previous location of that position.
when I run the code it only removes the patient but dose not add it to the location I want.
is the code right by me below. I have just added snippets as the whole project would be too confusing.
thx
tim
I have given the user an option to choose which patient they would like to move in the linked list.
i thought if I called S.insert(passed the new postition, and P (where P is the object of the patient class) it would work.
then all I had to do was remove the previous location of that position.
when I run the code it only removes the patient but dose not add it to the location I want.
is the code right by me below. I have just added snippets as the whole project would be too confusing.
thx
tim
C++ Syntax (Toggle Plain Text)
for(int a=1;a<(S.size()+1);a++) { cout<<a<<". "; S.display(a); } cout<<"\nWhich Patient would you like to treat"<<endl; cin>>num; cout<<"\nYou have chosen to treat:\n"<<endl; S.displayall(num); cout<<"\nWhat position would you like to place this patient in the Priority Q?"<<endl; cin>>position; S.insert(position,P); S.remove(num); }
C++ Syntax (Toggle Plain Text)
#include <iomanip> using namespace std; typedef patient SeqItemType; class ListNode { public: SeqItemType item; ListNode *next; }; class Sequence { public: Sequence(); bool isEmpty(); int size(); void insert(int index, SeqItemType newItem); void remove(int index); SeqItemType retrieve(int index); void display(int index); void displayall(int index); private: ListNode *find(int index); int length; ListNode *head; }; Sequence::Sequence() { length = 0; head = NULL; } bool Sequence::isEmpty() { return length==0; } int Sequence::size() { return length; } void Sequence::display(int index) { ListNode *cur = find(index); cout<<"Name :"<< cur->item.get_Name() << endl; //cout<<"Date of Birth :"<<cur->item.get_dob() << endl; //cout<<"Illness :"<<cur->item.get_illness() << endl; //cout<<"Priority :"<<cur->item.get_priority() << endl; //cout<<"Ward :"<<cur->item.get_ward_no() << endl; //cout<<"\n\n"; } SeqItemType Sequence::retrieve(int index) { if ((index < 1) || (index > size())){ // can't delete something that isn't there } ListNode *cur = find(index); return cur->item; } void Sequence::insert(int index, SeqItemType newItem) { if ((index < 1) || (index > (size()+1))){ // if index is out of bounds don't do anything } else { // otherwise create new node and place newItem in it ListNode *newPtr = new ListNode(); // increment the size attribute length++; // store the data item in the new node newPtr->item = newItem; // if the index is 1, insert new node at beginning of list if (index == 1) { newPtr->next = head; head = newPtr; } else { ListNode *prev = find(index-1); newPtr->next = prev->next; prev->next = newPtr; } } } void Sequence::remove(int index) { ListNode *cur; if ((index < 1) || (index > size())){ } else { // remove the first node in the list if (index == 1) { cur = head; head = head->next; } else{ ListNode *prev = find(index-1); cur = prev->next; prev->next = cur->next; } // clean things up! length--; cur->next = NULL; delete cur; } } ListNode *Sequence::find(int index){ if ((index <1) || (index >size())){ return 0; } else{ ListNode *cur = head; for(int skip =1; skip<index; ++skip) cur=cur->next; return cur; } } void Sequence::displayall(int index) { ListNode *cur = find(index); cout<<index<<". Name: "<<cur->item.get_Name() << endl; cout<<" Date of Birth: "<<cur->item.get_dob() << endl; cout<<" Illness: "<<cur->item.get_illness() << endl; cout<<" Priority: "<<cur->item.get_priority() << endl; cout<<" Ward: "<<cur->item.get_ward_no() << endl; cout<<"\n\n"; } /*void Sequence::displayall() { for (ListNode *cur = head; cur!= NULL; cur = cur->next) { cout<<"Name :"<< cur->item.get_Name() << endl; //cout<<"Date of Birth :"<<cur->item.get_dob() << endl; //cout<<"Illness :"<<cur->item.get_illness() << endl; //cout<<"Priority :"<<cur->item.get_priority() << endl; //cout<<"Ward :"<<cur->item.get_ward_no() << endl; //cout<<"\n\n"; } }*/
Last edited by Dave Sinkula; Dec 15th, 2005 at 12:07 am. Reason: Replaced color tags with [code][/code] tags.
•
•
Join Date: May 2005
Posts: 42
Reputation:
Solved Threads: 0
solution thanks to perniciosus
C++ Syntax (Toggle Plain Text)
void Sequence::move(int from, int to) { SeqItemType item = find( from )->item ; remove(from) ; insert( to, item ) ; }
![]() |
Similar Threads
- Doubly Linked List Problem (C++)
- linked list errors (C++)
- adding and deleting node in a linked list (C++)
- Searching linked list (C)
- Linked list search problems (Computer Science)
- remove method linked list (C)
- Linked List (C++)
Other Threads in the C++ Forum
- Previous Thread: appending and subtracting a number in an array
- Next Thread: Please I Need U Again
| Thread Tools | Search this Thread |
ace_thread api array based binary bitmap borland c++ c/c++ calling char class classes code coding compile console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





