Solved

Doubly linked list

Posted on 2003-11-07
10
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 67
Dialogbox API leak? 18 119
Which Linux flavors will this run on? 6 116
Indy 10 not Receiving UDP broadcast 3 57
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

752 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