playing arounf with linked list code

can anyone point ou to me the problem with this code?
for soem reason the head pointer becomes each new entry
and I can't figure out why?
thanks
BTW NOT for a class or anything just my own practice...
thx:
[code]
#include
using namespace std;

#define MAX 100

struct data{

data *next;
int num;

};



class list{
public:
list();
~list();
void add(data *num);
void del(data *num);
void search(data *num);
void print();

private:
data *head;
data *tail;

int count;

};

//constructor
list::list(){
head=NULL;
count=0;

}

//destructor
list::~list(){
data *tmp;
while(head!=NULL){
tmp=head;
head->next=head;
delete tmp;
}
}

//add function
void list::add(data *d){

if (head==NULL){
head=d;
tail=head;
head->next=NULL;
count++;
cout << "head" << head->num << endl;
return;
}

else{
cout << "tail: " << tail->num << endl;
cout << "d: " << d->num << endl;
cout << "head: " << head->num << endl;


tail->next=d;
count++;
d=tail;
d->next=NULL;


cout << "tail: " << tail->num << endl;
cout << "d: " << d->num << endl;
cout << "head: " << head->num << endl;

return;

}

}

//delete function
void list::del(data *d){



}

void list::print(){

cout << "The list has " << count << " items:"<< endl;

data *walker = head;



if(walker==NULL)
{
cout << "The List is Empty!!"<<endl;
return;
}

else if (walker!=NULL){
walker=walker->next;

}
//}

}//print



int main(){

list *L = new list;

data *D = new data;

bool boole=1;



int choice;

while(true)
{
cout<<"****** Int Linked List ******"<<endl; //simple user interface
cout<<"1.Add Node"<<endl;
cout<<"2.Delete Node"<<endl;
cout<<"3.Tracesearch for a Node"<<endl;
cout<<"4.Print"<<endl;
cout<<"5.Exit"<<endl;
cout<<"choice:";
cout<<flush;
cin>>choice;


switch(choice)
{
case 1:
{
cout << "Input new node to List: ";
cin >> D->num;
L->add(D);
break;
}
case 2:
{

break;
}
case 3:
{

break;
}
case 4:
{
L->print();
break;
}
case 5:
{
L.~list();
exit(0);
}
default:
{
cout<<"You must choose between 1-5"<<endl;
}
}
}




return 0;
};
}
[code]

Comments

  • [b][red]This message was edited by stober at 2005-4-30 19:14:43[/red][/b][hr]
    : can anyone point ou to me the problem with this code?
    : for soem reason the head pointer becomes each new entry
    : and I can't figure out why?
    : thanks
    : BTW NOT for a class or anything just my own practice...
    : thx:
    : [code]
    : #include
    : using namespace std;
    :
    : #define MAX 100
    :
    : struct data{
    :
    : data *next;
    : int num;
    :
    : };
    :
    [code]:
    : //add function
    : void list::add(data *d){
    :
    : if (head==NULL){
    : head=d;
    : tail=head;
    : head->next=NULL;
    : count++;
    : cout << "head" << head->num << endl;
    : return;
    : }
    :
    : else{
    : cout << "tail: " << tail->num << endl;
    : cout << "d: " << d->num << endl;
    : cout << "head: " << head->num << endl;
    :
    :
    : tail->next=d;
    : count++;
    [red] tail = d;[/red]

    [red]//[/red] d=tail;
    : d->next=NULL;
    :
    :
    : cout << "tail: " << tail->num << endl;
    : cout << "d: " << d->num << endl;
    : cout << "head: " << head->num << endl;
    :
    : return;
    :
    : }
    :
    : }
    :
    [/code]

  • : [b][red]This message was edited by stober at 2005-4-30 19:14:43[/red][/b][hr]
    : : can anyone point ou to me the problem with this code?
    : : for soem reason the head pointer becomes each new entry
    : : and I can't figure out why?
    : : thanks
    : : BTW NOT for a class or anything just my own practice...
    : : thx:
    : : [code]
    : : #include
    : : using namespace std;
    : :
    : : #define MAX 100
    : :
    : : struct data{
    : :
    : : data *next;
    : : int num;
    : :
    : : };
    : :
    : [code]:
    : : //add function
    : : void list::add(data *d){
    : :
    : : if (head==NULL){
    : : head=d;
    : : tail=head;
    : : head->next=NULL;
    : : count++;
    : : cout << "head" << head->num << endl;
    : : return;
    : : }
    : :
    : : else{
    : : cout << "tail: " << tail->num << endl;
    : : cout << "d: " << d->num << endl;
    : : cout << "head: " << head->num << endl;
    : :
    : :
    : : tail->next=d;
    : : count++;
    : [red] tail = d;[/red]
    :
    : [red]//[/red] d=tail;
    : : d->next=NULL;
    : :
    : :
    : : cout << "tail: " << tail->num << endl;
    : : cout << "d: " << d->num << endl;
    : : cout << "head: " << head->num << endl;
    : :
    : : return;
    : :
    : : }
    : :
    : : }
    : :
    : [/code]
    :
    : that may help something, but when i run it I am having the same problem and cannot print out the sequential entry's
    ??

  • : [b][red]This message was edited by stober at 2005-4-30 19:14:43[/red][/b][hr]
    : : can anyone point ou to me the problem with this code?
    : : for soem reason the head pointer becomes each new entry
    : : and I can't figure out why?
    : : thanks
    : : BTW NOT for a class or anything just my own practice...
    : : thx:
    : : [code]
    : : #include
    : : using namespace std;
    : :
    : : #define MAX 100
    : :
    : : struct data{
    : :
    : : data *next;
    : : int num;
    : :
    : : };
    : :
    : [code]:
    : : //add function
    : : void list::add(data *d){
    : :
    : : if (head==NULL){
    : : head=d;
    : : tail=head;
    : : head->next=NULL;
    : : count++;
    : : cout << "head" << head->num << endl;
    : : return;
    : : }
    : :
    : : else{
    : : cout << "tail: " << tail->num << endl;
    : : cout << "d: " << d->num << endl;
    : : cout << "head: " << head->num << endl;
    : :
    : :
    : : tail->next=d;
    : : count++;
    : [red] tail = d;[/red]
    :
    : [red]//[/red] d=tail;
    : : d->next=NULL;
    : :
    : :
    : : cout << "tail: " << tail->num << endl;
    : : cout << "d: " << d->num << endl;
    : : cout << "head: " << head->num << endl;
    : :
    : : return;
    : :
    : : }
    : :
    : : }
    : :
    : [/code]
    :
    that may help something, but when i run it I am having the same problem and cannot print out the sequential entry's
    ??

    void list::print(){

    cout << "The list has " << count << " items:"<< endl;

    data *walker = head;



    if(walker==NULL)
    {
    cout << "The List is Empty!!"<<endl;
    return;
    }

    else if (walker!=NULL){
    walker=walker->next;

    }


    }//print
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories