Solved

Doubly linked list

Posted on 2003-11-07
10
256 Views
Last Modified: 2011-09-20
Why do we refer to say
C = cur->prev->next  

instead of just saying

C = cur

for given 3 nodes A,B & C in a doubly circular sorted linked list.

If I am incorrect then please clearify what does one mean by
cur->prev->next  given that 3 nodes were given node A,B & C.
0
Comment
Question by:Mallp
  • 5
  • 3
10 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9703523
I would say that it's just the same.

If you post a code using that, maybe I can help you a little more.

Tincho
0
 

Author Comment

by:Mallp
ID: 9704197
Here is an example.

void SpecializedList::InsertEnd(int item)
{
      struct node *temp = get_node(item);
      temp->info = item;
      if (head==NULL)
      {       head = temp;                  // head is assigned the value of temp.
            head->prev = temp;            
            head->next = temp;
            temp->prev = head;
            temp->next = head;
            tail = temp;                  // head is also a tail if only one node exists.
      }
      else
      {      
            cur=head;
            while (cur->next != head)
                  cur=cur->next;
            temp->prev = head->prev;
            temp->next = head;
            head->prev->next = temp;
            head->prev = temp;
            tail=temp;
      }
}

0
 
LVL 9

Expert Comment

by:tinchos
ID: 9704611
Then Mallp

I would say that it's just the same, but I would say that it's just a matter of coding style

It may be clearer to the person who wrote it.

Tincho
0
 
LVL 15

Accepted Solution

by:
efn earned 20 total points
ID: 9705722
Your question and your example are different.  In the form in the question, they are the same, assuming the list is properly linked.  But in the example, the code makes sense, and the substitution would not make sense.  I'll try to explain it.

You have a circular list, so at the start the tail is linked to the head:

tail -> head
tail <- head

The code is inserting the node addressed by temp between tail and head.  The first thing it does is set the pointers in temp:

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

So then we have:

tail -> head
tail <- head
tail <- temp
temp -> head

So now temp has the right pointers, but we need to change the forward pointer in tail and the backward pointer in head.  The next line changes the forward pointer in tail.

head->prev->next = temp;

So then we have

tail -> temp
tail <- head
tail <- temp
temp -> head

The old value of head->prev->next is indeed head, but we are not using the value here, we are changing it:  we are referring to head->prev->next in order to change it.  So it makes sense and definitely does not have the same effect as

head = temp;

At the risk of running the point into the ground, I'll give a non-pointer example.  If you had

a = 5;
b = a;

you could just as well code

b = 5;

but if you have

a = 5;
a = 10;

you can't just as well code

5 = 10;

--efn
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:tinchos
ID: 10242612
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in approximately one week.  I would appreciate any comments by the experts that would help me in making a recommendation.

It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help.jsp#hs5

Tinchos
EE Cleanup Volunteer
0
 
LVL 15

Expert Comment

by:efn
ID: 10242836
I think I answered it.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10243063
efn

I didn't post a recommendation for this as I'm directly involved in it and personally I would rather avoid judging the questions I participated in unless the recommendation is fairly clear to me.
I just posted the message just waiting for the asker to close his question.

Lets wait to see if he shows up and if that does not happen I'll post a recommendation for it.

Tincho
0
 
LVL 15

Expert Comment

by:efn
ID: 10243195
Tincho,

OK, no problem, thanks for your cleanup efforts.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286282
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: efn {http:#9705722}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now