DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: few Qs. on circulad doubly linked list

1. Registered User
Join Date
Oct 2005
Posts
12

## few Qs. on circular doubly linked list

Hello ..

I've got some Qs.

assume that we have this Doubly linked list

what will happened if we apply the following ?

1- list->next->next->next=list->prev

and then we apply to the new list

2- list->prev->prev->prev=list->next->next->next->prev

and then ..

3- list->next->next->next->prev=list->prev->prev->prev

and then..

4- list->next=list->next->next

and finally ..

5- list->next->prev->next=list->next->next->next

thank you
Last edited by Qatar; 10-17-2005 at 01:43 PM.

2. First of all, if you read the forum's Etiquette you would know that we don't do homework assignments here, unless you show a real effort to solve the problem by yourself (which doesn't seem to be the case). Secondly, you need to define the loose terms: what's list->next exactly? Does it refer to thr first node of the list or to recentlt traversed node?
When you know the answer to this simple question, you'll be able to solve the rest of the exam. Simply take a pencil and follow the nodes according to the directions in the each question. It really isn't difficult.

3. Registered User
Join Date
Oct 2005
Posts
12
Hello Mr. Danny The Moderator..
First of all thank you for replying !

Well, This is not a homework ! This is an exercise to improve my skills !
and I faced some difficulties in solving it because this is the first ADT course for me and even the Dr. didn't teach us doubly linked list ..

It seems that it is so easy to you ,But it is not so for me .. that is why I post it here .. to get a help !

Well, Mr. Danny ,
I think that it was better if you didn't reply in that way without any help not even a hint !
We are here to learn from each other .. not to refuse helping each other !

thank you very much ..

4. Registered User
Join Date
Oct 2005
Posts
173
Qatar,
he is trying to help you.....read his reply again.
why else would he bother to ask you to clarify ->?

5. Registered User
Join Date
Oct 2005
Posts
12
this is my trial..

# include<iostream.h>

class node{
public:
int data;
node *next,*prev;
node(){ next=prev=0;}
node( int d,node *ptr1=0,node *ptr2=0)
{data=d;
next=ptr1;
prev=ptr2;
}

};

class list {
public:
list(){ head=tail=0;}
void addtohead(int d);
void addtotail(int d);
void print();
void one();
void two();

private:
node *head,*tail;

};

void list::addtohead(int d )
{
head=new node (d,head,0);
if(tail==0)
tail=head;
else
head->next->prev=head;
}

void list::addtotail(int d)
{
if(tail==0)
{
tail->next= new node (d,0,tail);
tail=tail->next;
}
else
head=tail=new node (d,0,0);

}

void list::print()
{
node *p=head;
while(p!=0)
{
cout<<p->data<<" ";
p=p->next;
}
}

void list::one()
{

head->next->next->next=head->prev;

}

void list::two()
{

head->prev->prev->prev=head->next->next->next->prev;

}

void main()
{

list l;

l.addtohead(5);
l.print();
cout<<endl;
l.addtohead(4);
l.print();
cout<<endl;
l.addtohead(3);
l.print();
cout<<endl;
l.addtohead(2);
l.print();
cout<<endl;
l.addtohead(1);
l.print();
cout<<endl;

l.one();
l.print();
cout<<endl;

l.two();
l.print();
cout<<endl;

}

------------------------------------------------------

I tried to do it that way ..
the secound method ( two(); )
doesn't work ..

I don't know what the prob. is !
Last edited by Qatar; 10-15-2005 at 06:20 AM.

6. what exactly isn't working? does the code compile? If it doesn't, which error message are you getting? If it crashes at runtime, please say where exactly the crash occurs.

7. Originally Posted by Qatar
,
I think that it was better if you didn't reply in that way without any help not even a hint !
We are here to learn from each other .. not to refuse helping each other !

thank you very much ..
Actaully, my reply included more than a hint. Anyway, your second post does include what is considered to be "an effort on yoru side" (see my other reply below).

8. Senior Member
Join Date
Dec 2003
Posts
3,366
class node
{
public:
int data;
node *next,*prev;
node(){next=prev=0;}

---node( int d,node *ptr1=0,node *ptr2=0)
---{data=d;
---next=ptr1;
--- prev=ptr2;
}

---node( int d,node *ptr1=0,node *ptr2=0)
If this compiles, what is it supposed to do? You clear the pointers when you try
to create a node from good data? I am not sure what the result of this will be but it looks wrong ... Danny? ... consider his usage :

void list::addtohead(int d )
{
head=new node (d,head,0); //what happens here????

9. Registered User
Join Date
Oct 2005
Posts
12
thank you Danny .. Thank you very much ..
void list::two()
{

head->prev->prev->prev=head->next->next->next->prev;

}
I've got a prob. here ..
it is keep running without stopping !!
I'm not sure of my implemintation ..

-------------------------------------------------

Jonnin ,
node( int d,node *ptr1=0,node *ptr2=0)
Yes , it is compile .. this is a constructor ..

..
.

void list::addtohead(int d )
{
head=new node (d,head,0);
it creates a new node d .. next pointer is pointed to head and the previous pointed to 0

10. Senior Member
Join Date
Dec 2003
Posts
3,366
If its running without stopping and the only while loop is in the print statement, then remove the print first. If it still does not stop, you have a pointer problem and the app is hung (unlikely to to this without a crash). Let me know if removal of the print creates a program that stops.

11. Registered User
Join Date
Oct 2005
Posts
12
I've end up with this answer ..

Is it okay ?!

12. Registered User
Join Date
Oct 2005
Posts
12
I've got a full mark :D

13. wonderful!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.