7 Replies - 411 Views - Last Post: 05 July 2017 - 01:52 PM

#1 PROGRAMMINGisLIFE  Icon 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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,133
  • 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  Icon 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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,133
  • 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  Icon 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  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10735
  • View blog
  • Posts: 18,363
  • 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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,133
  • 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  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10735
  • View blog
  • Posts: 18,363
  • 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
  • +
  • -

Page 1 of 1