Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Data Structures -- Linked List


16 replies to this topic

#1 varalu

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 468 posts
  • Gender:Male
  • Location:India
  • Interests:1) Web Technologies, DBMS, Data Warehousing and mining.<br />2) Basketball, football, athletics<br />3) Movies<br />4) Short stories<br />5) Online...friends...forums...blogging... etc. [Anything online]<br />6) Enjoyment matters
  • myCENT:1.17

Posted 26 October 2007 - 10:01 AM

Given a linked list, find the 5th last element with Time complexity O(n) and minimal space complexity.

Note: If you know the answer and if you feel it is simple also please post the answers so that others will come to know about the answers.

What is a linked list?? /* this is for your further reference and reading */

Quote

In computer science, a linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk, allowing the list of items to be traversed in a different order. A linked list is a self-referential datatype because it contains a pointer or link to another datum of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists.
There are various kinds of linked lists available and can be implemented in many languages and in different ways.

Applications of Linked Lists

Quote

Linked lists are used as a building block for many other data structures, such as stacks, queues and their variations.

The "data" field of a node can be another linked list. By this device, one can construct many linked data structures with lists; this practice originated in the Lisp programming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style.

Sometimes, linked lists are used to implement associative arrays, and are in this context called association lists. There is very little good to be said about this use of linked lists; they are easily outperformed by other data structures such as self-balancing binary search trees even on small data sets (see the discussion in associative array). However, sometimes a linked list is dynamically created out of a subset of nodes in such a tree, and used to more efficiently traverse that set.


even bad solutions are welcome... :P.

#2 pointybirds

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 12 posts

Posted 26 October 2007 - 12:45 PM

Hmm, presuming a singly linked list where you can't start at the end and work your way back, you could use a circular buffer with 5 elements (e.g. an array that can store the contents of the LL nodes) and traverse the list until you reach the end ... then, the "next" element in your circular buffer is the fifth last.

E.g.:

for (i=0; (e = next element) != null; i++%5) {
buf[i] = e
}
fifth_last = buf[i]

Something like that I guess. You'd also need to initialise your array to null and check for this at the end in case your LL < 5 elements.

#3 varalu

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 468 posts
  • Gender:Male
  • Location:India
  • Interests:1) Web Technologies, DBMS, Data Warehousing and mining.<br />2) Basketball, football, athletics<br />3) Movies<br />4) Short stories<br />5) Online...friends...forums...blogging... etc. [Anything online]<br />6) Enjoyment matters
  • myCENT:1.17

Posted 26 October 2007 - 01:34 PM

yours is a nice solution... Better in usage of memory also. This circular buffer thing is very good.
What is i am giving you a constraint of
1. "NO EXTRA MEMORY"
2. No additional Data structures.

Give a solution with the above constraints.

Small Hint:
Do not make it complex...think very simple. --> for a simple solution.

#4 sanjay0828

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 20 posts

Posted 26 October 2007 - 07:00 PM

Ok.. here is a very simple solution

Have two pointers.

Initially, let both the pointers point to the first node of the linked list

Now move one of the pointers to the 5th node of the linked list.
This can be done in O(1) complexity i.e. constant time.

Example:-
1-->2-->3-->4-->5-->6-->7-->8-->9-->10->11-->12

Here, one pointer points to 5 while the other pointer points to 1

Now keep moving each pointer i.e. current node to next node, until the first pointer reaches the end of the linked list i.e. 12
The second pointer will now be pointing to the 5th last element i.e. 8

#5 jlhaslip

    Insert Custom Title Here

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,040 posts
  • Gender:Not Telling
  • Location:Linux, DOS and Windows…the good, the bad and the ugly
  • myCENT:81.07
  • Spam Patrol

Posted 26 October 2007 - 07:29 PM

$item= $array[count($array)-5];

untested

may need to perform this in steps though

$num = count($array);
$n = $num - 5;
$item = $array[ $n ];

#6 varalu

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 468 posts
  • Gender:Male
  • Location:India
  • Interests:1) Web Technologies, DBMS, Data Warehousing and mining.<br />2) Basketball, football, athletics<br />3) Movies<br />4) Short stories<br />5) Online...friends...forums...blogging... etc. [Anything online]<br />6) Enjoyment matters
  • myCENT:1.17

Posted 27 October 2007 - 09:09 AM

@sanjay0828
Good one... the expected solution.

Again... i want a simpler solution. What will be the basic answer that comes to your mind if i give this problem.??
Very basic.. :P..

If no answer i will be posting it in my next post.

#7 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 04 November 2007 - 01:06 AM

Use two variable (sum4 and sum5) to store the sum of last 4 nodes and last 5 nodes respectively. At the end of the loop, the 5th node data = sum5 - sum4.

-Grant

#8 totrap

    Newbie

  • Kontributors
  • Pip
  • 1 posts

Posted 23 November 2007 - 06:08 AM

while(ptr->next->next->next->next->next!=NULL)
ptr=ptr->next;

//after loop, ptr points to the fifth last element in the list;

#9 songs2enjoy

    Newbie

  • Kontributors
  • Pip
  • 3 posts

Posted 31 August 2008 - 10:01 AM

reverse the list and find the 5th element from the start,

Not simple though just an idea.

Varalu,

Can u plz post what's the simple idea ?

Thanks

#10 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 05 September 2008 - 06:29 AM

How to find the number of element in circular link list
Data Structures -- Linked List

Hi All,
If I have a circular link list and I want to count the no of element within it.I donot want to use first_pointer==last_pointer method.
Is there any other approach to count the element?

Thanks
Sarkar

-reply by Saugata Kanti Sarkar




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users