1 / 10

Circular List

Circular List. Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible to reach any other point in the list. Stack as Circular list. A circular list can be used to represent a stack or a queue

lynton
Télécharger la présentation

Circular List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Circular List • Next field in the last node contains a pointer back to the first node rather than null pointer • From any point in such a list it is possible to reach any other point in the list

  2. Stack as Circular list • A circular list can be used to represent a stack or a queue • Let stack be a pointer to the last node of circular list bool IsEmpty() { return ( head == NULL); }

  3. Push in circular list • void push( int x) • { • Node * temp; • temp = new Node(x); • if( IsEmpty()) head = temp; • else • {temp->next = head->next; • head->next = temp; • } • }

  4. Pop in circular list int pop() { int x; Node *temp; if( IsEmpty()) { cout<<“stack empty”; exit(-1); } temp = head->next; x = temp->item; if( temp == head) // only one node on stack head = NULL; else head -> next = temp->next; delete temp; return x; }

  5. Queue as circular list • It is easier to represent queue as a circular list than as a linear list • As linear list, a queue is specified by two pointers • Using a circular list, a queue may be specified by a single pointer q to that list • q is back, q->next is front q q->next

  6. fucntions • Empty function is same as stack • int remove() • { Node * temp; • temp = q->next; • x = temp->item; • q->next = temp->next; • delete temp; • return x; • }

  7. insert • void insert( int x) • { Node *temp = new Node(x); • if( isEmpty()) q = temp; • else • { temp ->next = q->next; • q->next = temp; • q = temp; • } • }

  8. Josephus problem • A group of soldiers surrounded by an overwhelming enemy force. There is no hop for victory without reinforcements, but there is only a single horse available for escape. The soldiers agree to a pact to determine which of them is to escape and summon help. They form a circle and a number n is picked from a hat. One of their names is also picked from a hat. Beginning with the soldier whose name is picked, they begin to count clockwise around the circle. When the count reaches n, that soldier is removed from the circle, and the count begin again with the next solider. The process continues so that each time the count reaches n, another soldier is removed from the circle. Any soldier removed from the circle is no longer counted. The last soldier remaining is to take the horse and escape.

  9. Example • Soldiers A,B,C,D,E • N = 3 start at A • Who is to escape Answer is D

  10. Outline of program • Clearly, a circular list in which each node represents one soldier is a natural data structure. • Read number n • Loop until end of file ( of list of names) • read a name and insert in circular list • While( there is more than one node) • count through n-1 nodes on the list • Print the name • Delete the n node • Print the name of the only node on the list

More Related