Hey guys,
Im doing a project for collage and i am running into a few difficulties. i have to be able to go back and forwards through a list by using the "f" and "r" keys. iv hacken out a bit of code to do go right but i have to pass it by reference in order to get it to work, which only ends up changing the head and causing a load of side effects. as far as going left is concernds, it doesent work at all!!
Here is the full code:
The functions i am having trouble with are called "moveright" and "moveleft".
has anybody got any idea as to how i can do this?Code:#include <iostream> #include <conio.h> #include <string> using namespace std; struct Node { string data; struct Node * next; struct Node * prev; }; typedef struct Node entry ; string UCase(string s); void list(entry * H); void del(entry * H,string OldName); void rename(entry * H, string old, string newname); void moveright(entry *&H); void Head(entry * H); void tail (entry * H); void moveleft(entry * H); void inalpha(string newname, entry * &H); main() { entry *head=NULL, *pos=head; string Command,OldName,NewName ; do { cout << "Next Command? " ; cin >> Command ; Command = UCase(Command) ; if(Command == "L") { moveleft(head); } else if(Command == "R") { moveright(head); } else if(Command == "H") { Head(head); } else if(Command == "T") { tail (head); } else if(Command == "RENAME") { cout << "OldName? " ; cin >> OldName ; cout << "NewName? " ; cin >> NewName ; rename(head,OldName,NewName); } else if(Command == "NEW") { cout << "Name? " ; cin >> OldName ; inalpha(OldName, head); } else if(Command =="DELETE") { cout << "Name? " ; cin >> OldName ; del(head,OldName); } else if(Command == "LIST") { list(head); } }while(Command != "END") ; } string UCase(string s) { int i ; for(i=0 ; i < s.length() ; i++) { s[i] = toupper(s[i]) ; } return s; } void list(entry * H) { entry *pos = H ; while(pos != NULL) { cout << pos->data <<'\t' ; pos = pos->next ; } cout<<endl; } void del(entry * H,string OldName) { entry*pos, *prev, *firstEntry=H, *first; if(H != 0){ prev=NULL; pos=H; while (pos != NULL && pos->data != OldName){ prev=pos; pos=pos->next; } if(pos == NULL){ cout<<"Name is not in the list"<<endl; //break; }else if(pos != NULL){ if(prev == NULL){ H = pos->next; delete pos; //break; }else{ prev->next=pos->next; delete pos; //break; } } } } void rename(entry * H, string old, string newname) { entry*pos=H; bool inlist(false); while (pos != NULL){ if (pos->data != newname){ pos=pos->next; }else if (pos->data == newname){ inlist=true; break; } } pos=H; while (pos != NULL){ if(pos->data==old){ if (inlist==false){ pos->data=newname; break; }else if (inlist==true){ cout<<"Already in list"<<endl; break; } }else{ pos=pos->next; } } } /*void moveright(entry * &H) { entry* current; current=H; if (current->next != NULL){ H=current->next; cout<<current->data<<endl; }else{ cout<<current->data<<endl; cout<<"End of list"<<endl; } }*/ void moveleft(entry * H) { entry *current, *T, *pre; current=H; if(current->prev != NULL){ while(current->prev !=NULL){ pre=current; current=current->next; } current=T; current->prev=pre; current=pre; cout<<current->data<<endl; }else{ cout<<current->data<<endl; cout<<"Start of list"<<endl; } } void Head(entry * H) { cout<<H->data<<endl; } void tail (entry * H) { entry * pos=H; while (pos->next != NULL){ pos=pos->next; } cout<<pos->data<<endl; } void moveright(entry *&H) { entry *pos=H; if(pos != NULL){ pos=pos->next; cout<<pos->data<<endl; }else{ cout<<pos->data<<endl; cout<<"End Of List"<<endl; } H=pos; } void inalpha(string newname, entry * &H) { entry *e, *pos=H, *prev ; bool inlist(false); while (pos != NULL){ if (pos->data != newname){ pos=pos->next; }else if (pos->data == newname){ inlist=true; break; } } if(inlist==false){ e=new entry; e->data=newname; e->next=NULL; if (H==0){ H=e; }else{ prev=NULL; pos=H; while(pos != NULL && pos->data <= newname){ prev=pos; pos=pos->next; } if (prev == NULL){ e->next=H; H=e; }else{ e->next=prev->next; prev->next=e; } } }else if(inlist==true){ cout<<"Sheet is already in the list"<<endl; } }
thanks,
Ray


Reply With Quote


Bookmarks