-
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.
-
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.
Danny Kalev
-
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 ..
-
Qatar,
he is trying to help you.....read his reply again.
why else would he bother to ask you to clarify ->?
-
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.
-
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.
Danny Kalev
-
 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).
Danny Kalev
-
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????
-
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
-
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.
-
I've end up with this answer ..


Is it okay ?!
-
-
Similar Threads
-
Replies: 2
Last Post: 03-31-2005, 05:17 PM
-
By white94cam in forum C++
Replies: 2
Last Post: 03-30-2005, 11:02 PM
-
Replies: 0
Last Post: 07-20-2001, 08:42 AM
-
By Larry Rebich in forum vb.announcements
Replies: 1
Last Post: 06-28-2001, 01:22 PM
-
By Hemant in forum VB Classic
Replies: 0
Last Post: 10-11-2000, 03:38 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|