Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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

3 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); }

4 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; }

5 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; }

6 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

7 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; }

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

9 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.

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

11 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


Download ppt "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."

Similar presentations


Ads by Google