8 Replies - 1043 Views - Last Post: 14 April 2019 - 12:31 AM

#1 PROGRAMMINGisLIFE   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

linked list

Posted 05 July 2017 - 12:16 PM

hi, can someone explain the code inside else? I don't understand why head is assigned to the next element of newOne

void ContactList::addToHead(Contact* newOne)
{
	if (head == NULL)
	{
		head = newOne;
	}
	else
	{
		newOne->next = head;
		head = newOne;
	}

	size++;
}



Is This A Good Question/Topic? 0
  • +

Replies To: linked list

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15059
  • View blog
  • Posts: 60,144
  • Joined: 12-June 08

Re: linked list

Posted 05 July 2017 - 12:23 PM

03    if (head == NULL)
04    {
05        head = newOne;

Nothing is there so 'head' pointer is the new one by logical default.


07    else  // more than no objects in the list.
08    {
09        newOne->next = head; // new object's next points to the first object
10        head = newOne; // the first object pointer points to the new object

So.. if that's the case then any new object is added to the front of the list.. so prepended.

Example:

list is: a -> b -> c
Head: a

I want to add d.

d.next -> a
head: d

List is now d -> a -> b -> c
Was This Post Helpful? 1
  • +
  • -

#3 PROGRAMMINGisLIFE   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

Re: linked list

Posted 05 July 2017 - 01:13 PM

thank you very much, best explanation that I have seen about linked list
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15059
  • View blog
  • Posts: 60,144
  • Joined: 12-June 08

Re: linked list

Posted 05 July 2017 - 01:15 PM

Don't forget there are folk who have visualizations of data structures.

http://www.cs.usfca....Algorithms.html
Was This Post Helpful? 0
  • +
  • -

#5 PROGRAMMINGisLIFE   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

Re: linked list

Posted 05 July 2017 - 01:18 PM

View Postmodi123_1, on 05 July 2017 - 01:15 PM, said:

Don't forget there are folk who have visualizations of data structures.

http://www.cs.usfca....Algorithms.html



thank you very much, I didn't know about it
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11539
  • View blog
  • Posts: 19,625
  • Joined: 19-March 11

Re: linked list

Posted 05 July 2017 - 01:25 PM

And when you get to sorts, there are also some creative visualizations of those, for example this one: https://www.youtube....h?v=ywWBy6J5gz8
Was This Post Helpful? 1
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15059
  • View blog
  • Posts: 60,144
  • Joined: 12-June 08

Re: linked list

Posted 05 July 2017 - 01:32 PM

That's pretty slow quick-sorting. Maybe if it was done in burlesque it would speed things up.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11539
  • View blog
  • Posts: 19,625
  • Joined: 19-March 11

Re: linked list

Posted 05 July 2017 - 01:52 PM

Oh, no, my friend. Burlesque is not about quick and dirty. Burlesque is about slow and sultry.
Was This Post Helpful? 1
  • +
  • -

#9 PsychoCod3r   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-April 19

Re: linked list

Posted 14 April 2019 - 12:31 AM

It's because you're adding an item onto the beginning of the list, i.e. the head. This item will be the new head, and it will precede the old head in the list and point to it. This is a basic linked list operation known as enqueueing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1