Your SlideShare is downloading. ×
0
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Computer notes  - C++ Code for Linked List
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Computer notes - C++ Code for Linked List

6,240

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

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
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,240
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
200
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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.
  • Transcript

    • 1. Class No.04 Data Structures http://ecomputernotes.com
    • 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. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL &amp;&amp; </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode-&gt;setNext(currentNode-&gt;getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode-&gt;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. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL &amp;&amp; </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode-&gt;setNext(currentNode-&gt;getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode-&gt;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. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL &amp;&amp; </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode-&gt;setNext(currentNode-&gt;getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode-&gt;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. C++ Code for Linked List <ul><li>void remove() { </li></ul><ul><li>if( currentNode != NULL &amp;&amp; </li></ul><ul><li>currentNode != headNode) { </li></ul><ul><li> lastCurrentNode-&gt;setNext(currentNode-&gt;getNext()); </li></ul><ul><li> delete currentNode; </li></ul><ul><li> currentNode = lastCurrentNode-&gt;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. 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. Example of List Usage <ul><li>#include &lt;iostream&gt; </li></ul><ul><li>#include &lt;stdlib.h&gt; </li></ul><ul><li>#include &amp;quot;List.cpp&amp;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 &lt;&lt; &amp;quot;List Element: &amp;quot;&lt;&lt; list.get()&lt;&lt;endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com
    • 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. 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. 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. 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. 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. 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. 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. 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-&gt;object = object; }; </li></ul><ul><li>Node* getNext() { return nextNode; }; </li></ul><ul><li>void setNext(Node* nextNode) </li></ul><ul><li>{ this-&gt;nextNode = nextNode; }; </li></ul><ul><li>Node* getPrev() { return prevNode; }; </li></ul><ul><li>void setPrev(Node* prevNode) </li></ul><ul><li>{ this-&gt;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. 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. Doubly-linked List <ul><li>newNode-&gt;setNext( current-&gt;getNext() ); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1
    • 19. Doubly-linked List <ul><li>newNode-&gt;setNext( current-&gt;getNext() ); </li></ul><ul><li>newNode-&gt;setprev( current ); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1 2
    • 20. Doubly-linked List <ul><li>newNode-&gt;setNext( current-&gt;getNext() ); </li></ul><ul><li>newNode-&gt;setprev( current ); </li></ul><ul><li>(current-&gt;getNext())-&gt;setPrev(newNode); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1 2 3
    • 21. Doubly-linked List <ul><li>newNode-&gt;setNext( current-&gt;getNext() ); </li></ul><ul><li>newNode-&gt;setprev( current ); </li></ul><ul><li>(current-&gt;getNext())-&gt;setPrev(newNode); </li></ul><ul><li>current-&gt;setNext( newNode ); </li></ul>size=5 2 6 8 7 current 9 newNode http://ecomputernotes.com head 1 1 2 3 4
    • 22. Doubly-linked List <ul><li>newNode-&gt;setNext( current-&gt;getNext() ); </li></ul><ul><li>newNode-&gt;setprev( current ); </li></ul><ul><li>(current-&gt;getNext())-&gt;setPrev(newNode); </li></ul><ul><li>current-&gt;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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Josephus Problem <ul><li>N=10, M=3 </li></ul>http://ecomputernotes.com 9 8 7 6 5 4 3 2 1 10
    • 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. 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. 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. 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. 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. 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. 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. 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. 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. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    • 46. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    • 47. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    • 48. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    • 49. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    • 50. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>http://ecomputernotes.com 
    • 51. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>
    • 52. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>
    • 53. Josephus Problem <ul><ul><li>#include &amp;quot;CList.cpp&amp;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 &lt;= N; i++ ) list.add(i); </li></ul></ul><ul><ul><li>list.start(); </li></ul></ul><ul><ul><li>while( list.length() &gt; 1 ) { </li></ul></ul><ul><ul><li> for(i=1; i &lt;= M; i++ ) list.next(); </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;remove: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>list.remove(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>cout &lt;&lt; &amp;quot;leader is: &amp;quot; &lt;&lt; list.get() &lt;&lt; endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>

    ×