codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  C++ Linked lists project  Boyracer87 at 11:08 on Monday, May 08, 2006
 

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!!

[u]here is the full code:[/u]

#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 = toupper(s) ;
}
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 in advance for any help,

Ray








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  onChange issue
•  Remote program execution using cgi-perl
•  Chat application
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me
•  pls help me with this..


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007