Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Circular Linked List

 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had no idea how to do this, can anyone help me?
1) Declare a class CircularList which contains the proper data structure which support the implementation of the circular linked list. Each node in the linked list contain only integer element. [3 marks]
2) Implement the method showDetail, which serves the purpose of displaying all the data above circular linked list. [5 marks]
3) Add to your class another method passMark, that takes an integer pass parameter. The method should return the number of nodes with values above the pass value. [5 marks]
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all, how do implement a circular linked list?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shall this be a ringbuffer - like the clock, where it starts from the beginning after reaching the end: '..., 10, 11, 12, 1, 2, ...'?
Is the size of this circular list known in advance, or shall it be possible to in- and decrease it's size?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Circular operations often involve modulo arithmetic. The % operator gives you the mod or remainder of a division. So if I have a 5-slot list indexed 0-4 and I'm pointing at index 4, the "next" should be index 0. Mod is perfect:
index = ( index + 1 ) % 5;
5/5 has no remainder, so the index goes to 0.
Now, I'm talking about index, so you might be thinking ... array? If your circular list is of fixed size, an arry would be a fine implementation. Circular lists or buffers are usually fixed size, maybe configurable when you create it. Give you any ideas?
BTW: I get crazy trying to do mod on negatives. How would you go backward from 0 to 4 without going negative? That is, avoid this at index 0 (or any other):
index = ( index - 1 ) % 5;
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shall this be a ringbuffer - like the clock, where it starts from the beginning after reaching the end: '..., 10, 11, 12, 1, 2, ...'?

Yes that's right, the next reference of the list's last node references the first node, instead of null.

Is the size of this circular list known in advance, or shall it be possible to in- and decrease it's size?
Now, I'm talking about index, so you might be thinking ... array? If your circular list is of fixed size, an arry would be a fine implementation.

since the question does not mention whether they want it to be fixed or expandable, so to make it a good program lets implement it as a reference-base list (expandable) . any suggestions?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic