Computer notes - C++ Code for Linked List

6,771 views

Published on

Add -we simply insert the new node after the current node. So add is a one-step operation. Remove - remove is also a one-step operation

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,771
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
236
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • End of lecture 4. The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • Computer notes - C++ Code for Linked List

    1. 1. Class No.04 Data Structures http://ecomputernotes.com
    2. 2. C++ Code for Linked List <ul><li>// position current before the first </li></ul><ul><li>// list element </li></ul><ul><li>void start() { </li></ul><ul><li>lastCurrentNode = headNode; </li></ul><ul><li>currentNode = headNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com
    3. 3. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL && </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode->setNext(currentNode->getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode->getNext(); </li></ul><ul><li> size--; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>2 6 1 headNode currentNode size=5 lastcurrentNode 8 http://ecomputernotes.com 7
    4. 4. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL && </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode->setNext(currentNode->getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode->getNext(); </li></ul><ul><li> size--; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>2 6 1 headNode currentNode size=5 lastcurrentNode 8 http://ecomputernotes.com 7 1 1
    5. 5. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL && </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode->setNext(currentNode->getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode->getNext(); </li></ul><ul><li> size--; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>2 1 headNode currentNode size=5 lastcurrentNode 8 http://ecomputernotes.com 7 1 1 2 2
    6. 6. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL && </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode->setNext(currentNode->getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode->getNext(); </li></ul><ul><li> size--; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>2 1 headNode currentNode size=4 lastcurrentNode 8 http://ecomputernotes.com 7 1 1 2 2 3 3 4 4
    7. 7. C++ Code for Linked List <ul><li>int length() </li></ul><ul><li>{ </li></ul><ul><li>return size; </li></ul><ul><li>}; </li></ul><ul><li>private: </li></ul><ul><li>int size; </li></ul><ul><li>Node *headNode; </li></ul><ul><li>Node *currentNode, *lastCurrentNode; </li></ul>http://ecomputernotes.com
    8. 8. Example of List Usage <ul><li>#include <iostream> </li></ul><ul><li>#include <stdlib.h> </li></ul><ul><li>#include &quot;List.cpp&quot; </li></ul><ul><li>int main(int argc, char *argv[]) </li></ul><ul><li>{ </li></ul><ul><li>List list; </li></ul><ul><li>list.add(5); list.add(13); list.add(4); </li></ul><ul><li>list.add(8); list.add(24); list.add(48); list.add(12); </li></ul><ul><li>list.start(); </li></ul><ul><li>while (list.next()) </li></ul><ul><li>cout << &quot;List Element: &quot;<< list.get()<<endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    9. 9. Analysis of Linked List <ul><li>add </li></ul><ul><ul><li>we simply insert the new node after the current node. So add is a one-step operation. </li></ul></ul>http://ecomputernotes.com
    10. 10. Analysis of Linked List <ul><li>add </li></ul><ul><ul><li>we simply insert the new node after the current node. So add is a one-step operation. </li></ul></ul><ul><li>remove </li></ul><ul><ul><li>remove is also a one-step operation </li></ul></ul>http://ecomputernotes.com
    11. 11. Analysis of Linked List <ul><li>add </li></ul><ul><ul><li>we simply insert the new node after the current node. So add is a one-step operation. </li></ul></ul><ul><li>remove </li></ul><ul><ul><li>remove is also a one-step operation </li></ul></ul><ul><li>find </li></ul><ul><ul><li>worst-case: may have to search the entire list </li></ul></ul>http://ecomputernotes.com
    12. 12. Analysis of Linked List <ul><li>add </li></ul><ul><ul><li>we simply insert the new node after the current node. So add is a one-step operation. </li></ul></ul><ul><li>remove </li></ul><ul><ul><li>remove is also a one-step operation </li></ul></ul><ul><li>find </li></ul><ul><ul><li>worst-case: may have to search the entire list </li></ul></ul><ul><li>back </li></ul><ul><ul><li>moving the current pointer back one node requires traversing the list from the start until the node whose next pointer points to current node. </li></ul></ul>
    13. 13. Doubly-linked List <ul><li>Moving forward in a singly-linked list is easy; moving backwards is not so easy. </li></ul>http://ecomputernotes.com
    14. 14. Doubly-linked List <ul><li>Moving forward in a singly-linked list is easy; moving backwards is not so easy. </li></ul><ul><li>To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. </li></ul>http://ecomputernotes.com
    15. 15. Doubly-linked List <ul><li>Moving forward in a singly-linked list is easy; moving backwards is not so easy. </li></ul><ul><li>To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. </li></ul><ul><li>To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node: </li></ul>http://ecomputernotes.com element next prev
    16. 16. Doubly-Linked List Node <ul><li>class Node { </li></ul><ul><li>public: </li></ul><ul><li>int get() { return object; }; </li></ul><ul><li>void set(int object) { this->object = object; }; </li></ul><ul><li>Node* getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node* nextNode) </li></ul><ul><li>{ this->nextNode = nextNode; }; </li></ul><ul><li>Node* getPrev() { return prevNode; }; </li></ul><ul><li>void setPrev(Node* prevNode) </li></ul><ul><li>{ this->prevNode = prevNode; }; </li></ul><ul><li>private: </li></ul><ul><li>int object; </li></ul><ul><li>Node* nextNode; </li></ul><ul><li>Node* prevNode; </li></ul><ul><li>}; </li></ul>http://ecomputernotes.com  
    17. 17. Doubly-linked List <ul><li>Need to be more careful when adding or removing a node. </li></ul><ul><li>Consider add: the order in which pointers are reorganized is important: </li></ul>size=5 2 6 8 7 1 current http://ecomputernotes.com head
    18. 18. Doubly-linked List <ul><li>newNode->setNext( current->getNext() ); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1
    19. 19. Doubly-linked List <ul><li>newNode->setNext( current->getNext() ); </li></ul><ul><li>newNode->setprev( current ); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1 2
    20. 20. Doubly-linked List <ul><li>newNode->setNext( current->getNext() ); </li></ul><ul><li>newNode->setprev( current ); </li></ul><ul><li>(current->getNext())->setPrev(newNode); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1 2 3
    21. 21. Doubly-linked List <ul><li>newNode->setNext( current->getNext() ); </li></ul><ul><li>newNode->setprev( current ); </li></ul><ul><li>(current->getNext())->setPrev(newNode); </li></ul><ul><li>current->setNext( newNode ); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1 2 3 4
    22. 22. Doubly-linked List <ul><li>newNode->setNext( current->getNext() ); </li></ul><ul><li>newNode->setprev( current ); </li></ul><ul><li>(current->getNext())->setPrev(newNode); </li></ul><ul><li>current->setNext( newNode ); </li></ul><ul><li>current = newNode; </li></ul><ul><li>size++; </li></ul>size=6 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1 2 3 4
    23. 23. Circularly-linked lists <ul><li>The next field in the last node in a singly-linked list is set to NULL. </li></ul><ul><li>Moving along a singly-linked list has to be done in a watchful manner. </li></ul><ul><li>Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. </li></ul><ul><li>A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . </li></ul>http://ecomputernotes.com
    24. 24. Circularly-linked lists <ul><li>The next field in the last node in a singly-linked list is set to NULL. </li></ul><ul><li>Moving along a singly-linked list has to be done in a watchful manner. </li></ul><ul><li>Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. </li></ul><ul><li>A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . </li></ul>http://ecomputernotes.com
    25. 25. Circularly-linked lists <ul><li>The next field in the last node in a singly-linked list is set to NULL. </li></ul><ul><li>Moving along a singly-linked list has to be done in a watchful manner. </li></ul><ul><li>Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. </li></ul><ul><li>A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . </li></ul>http://ecomputernotes.com
    26. 26. Circularly-linked lists <ul><li>The next field in the last node in a singly-linked list is set to NULL. </li></ul><ul><li>Moving along a singly-linked list has to be done in a watchful manner. </li></ul><ul><li>Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. </li></ul><ul><li>A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . </li></ul>http://ecomputernotes.com
    27. 27. Cicularly Linked List <ul><li>Two views of a circularly linked list: </li></ul>2 8 7 1 head current size=5 6 http://ecomputernotes.com 2 6 8 7 1 head current size=5
    28. 28. Josephus Problem <ul><li>A case where circularly linked list comes in handy is the solution of the Josephus Problem . </li></ul>http://ecomputernotes.com
    29. 29. Josephus Problem <ul><li>A case where circularly linked list comes in handy is the solution of the Josephus Problem . </li></ul><ul><li>Consider there are 10 persons. They would like to choose a leader. </li></ul>http://ecomputernotes.com
    30. 30. Josephus Problem <ul><li>A case where circularly linked list comes in handy is the solution of the Josephus Problem . </li></ul><ul><li>Consider there are 10 persons. They would like to choose a leader. </li></ul><ul><li>The way they decide is that all 10 sit in a circle. </li></ul>http://ecomputernotes.com
    31. 31. Josephus Problem <ul><li>A case where circularly linked list comes in handy is the solution of the Josephus Problem . </li></ul><ul><li>Consider there are 10 persons. They would like to choose a leader. </li></ul><ul><li>The way they decide is that all 10 sit in a circle. </li></ul><ul><li>They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. </li></ul>http://ecomputernotes.com
    32. 32. Josephus Problem <ul><li>A case where circularly linked list comes in handy is the solution of the Josephus Problem . </li></ul><ul><li>Consider there are 10 persons. They would like to choose a leader. </li></ul><ul><li>The way they decide is that all 10 sit in a circle. </li></ul><ul><li>They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. </li></ul><ul><li>The count starts with the fifth and the next person to go is the fourth in count. </li></ul>http://ecomputernotes.com
    33. 33. Josephus Problem <ul><li>A case where circularly linked list comes in handy is the solution of the Josephus Problem . </li></ul><ul><li>Consider there are 10 persons. They would like to choose a leader. </li></ul><ul><li>The way they decide is that all 10 sit in a circle. </li></ul><ul><li>They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. </li></ul><ul><li>The count starts with the fifth and the next person to go is the fourth in count. </li></ul><ul><li>Eventually, a single person remains. </li></ul>http://ecomputernotes.com
    34. 34. Josephus Problem <ul><li>A case where circularly linked list comes in handy is the solution of the Josephus Problem . </li></ul><ul><li>Consider there are 10 persons. They would like to choose a leader. </li></ul><ul><li>The way they decide is that all 10 sit in a circle. </li></ul><ul><li>They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. </li></ul><ul><li>The count starts with the fifth and the next person to go is the fourth in count. </li></ul><ul><li>Eventually, a single person remains. </li></ul>
    35. 35. Josephus Problem <ul><li>N=10, M=3 </li></ul>http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    36. 36. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    37. 37. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    38. 38. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    39. 39. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    40. 40. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    41. 41. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    42. 42. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    43. 43. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    44. 44. Josephus Problem <ul><li>N=10, M=3 </li></ul>eliminated http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    45. 45. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    46. 46. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    47. 47. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    48. 48. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    49. 49. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    50. 50. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    51. 51. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>
    52. 52. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>
    53. 53. Josephus Problem <ul><ul><li>#include &quot;CList.cpp&quot; </li></ul></ul><ul><ul><li>void main(int argc, char *argv[]) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>CList list; </li></ul></ul><ul><ul><li>int i, N=10, M=3; </li></ul></ul><ul><ul><li>for(i=1; i <= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() > 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i <= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout << &quot;remove: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout << &quot;leader is: &quot; << list.get() << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>

    ×