Solved

Doubly linked list

Posted on 2003-11-07
10
255 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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
Comment Utility
I think I answered it.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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
Comment Utility
Tincho,

OK, no problem, thanks for your cleanup efforts.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

762 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

12 Experts available now in Live!

Get 1:1 Help Now