Advertisement
Guest User

Untitled

a guest
Sep 14th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.10 KB | None | 0 0
  1. /* ------------------------------------------------
  2.   * OverBoard
  3.   *
  4.   * Class: CS 251, Fall 2014.  Thur 8am lab.
  5.   * System: Terminal on Mac
  6.   * Author: Alex Alvizo
  7.   *
  8.   * ToDo: Using Circular linked lists to get rid of androids
  9.   * -------------------------------------------------
  10.   */
  11.  
  12.  
  13. #include <iostream> //for basic I/O
  14. #include <fstream>  //to read from file
  15. using namespace std;
  16.  
  17. //create a node
  18. struct Node{
  19.     char livingThing;
  20.     Node *pNext;
  21. };
  22.  
  23.  
  24. //function that adds a node to the end of the list.  It takes in a list and character.
  25. void appendNode(Node * &pTail, char c){
  26.     //make a new node to insert
  27.     Node *pTemp = new Node;
  28.     //insert value
  29.     pTemp->livingThing = c;
  30.     pTemp->pNext = NULL;
  31.  
  32.    
  33.     //if it is the first node
  34.     if(pTail == NULL){
  35.         pTail = pTemp;
  36.         pTail->pNext = pTail;
  37.     }
  38.    
  39.  
  40.     //if the list is not empty
  41.     if(pTail != NULL){
  42.         //make the new node point to the head of the list
  43.         pTemp->pNext = pTail->pNext;
  44.         //connect it to the list by making the previous tail pointing to the new node
  45.         pTail->pNext = pTemp;
  46.     }
  47.     else{
  48.         pTemp->pNext = pTemp;
  49.     }
  50.     //reset tail pointer
  51.     pTail = pTemp;
  52. }//end of appendNode()
  53. //-----------------------------------------------------------------------------------------------
  54.  
  55.  
  56. //function to print out the list.  It takes in a list
  57. void printList(Node * &pTail){
  58.     //node to keep track of the tail
  59.     Node *pTemp = pTail->pNext;
  60.     //since its a circularly, singly linked list, i have to use a do-while so that it prints out the first thing then updates the
  61.         //pNext and stops when it reaches pTail again.
  62.     if(pTail != NULL){
  63.         do{
  64.             cout << pTemp->livingThing;
  65.             pTemp= pTemp->pNext;
  66.         }while(pTemp != pTail->pNext);
  67.     }  
  68.     cout << endl << endl;
  69. }//end of printList
  70. //-----------------------------------------------------------------------------------------------
  71.  
  72.  
  73. //function to read input from file and store into list.  It takes in a list.
  74. void readFromFileAndStore(Node * &pTail){
  75.     char c = ' ';
  76.  
  77.     //create file pointer
  78.     FILE *pInputFile;
  79.     //associate file pointer with actual file and try to open it
  80.     pInputFile = fopen("abcd.txt", "r");
  81.     //verify that it did open it
  82.     if(pInputFile == NULL){
  83.         cout << "Can't open " << pInputFile << "Verify that it is correct!" << endl;
  84.         exit(-1);
  85.     }
  86.     while (fscanf(pInputFile, "%c", &c) != EOF){
  87.         appendNode(pTail, c);
  88.     }
  89. }//end of readFromFileAndStore()
  90. //------------------------------------------------------------------------------------------------
  91.  
  92.  
  93. //function to count the number of nodes in the list.  It takes in a list and returns the number of nodes in a list
  94. int numOfNodes(Node * &pTail){
  95.     //node to keep track of the tail
  96.     Node *pTemp = pTail;
  97.     //counter
  98.     int current = 0;
  99.     //since its a circularly, singly linked list, i have to use a do-while so that it prints out the first thing then updates the
  100.         //pNext and stops when it reaches pTail again.
  101.     do{
  102.         current++;
  103.         pTemp= pTemp->pNext;
  104.     }while(pTemp != pTail);
  105.    
  106.     return current;
  107. }//end of numOfNodes
  108. //------------------------------------------------------------------------------------------------
  109.  
  110.  
  111. //function to delete a node.  It takes in a node to delete and a list
  112. void deleteNode(Node * pCurrent, Node * &pTail){
  113.     //keep track of where you started
  114.     Node * pStart = pCurrent;
  115.  
  116.     //if list has one element
  117.     if(pCurrent->pNext == pStart){
  118.         delete pCurrent;
  119.         pTail->pNext = NULL;
  120.         cout << "List is Empty!\n";
  121.     }
  122.     //if it has more than 1 element
  123.     while(pCurrent->pNext != pStart){
  124.         //pCurrent at this point points to the previous node
  125.         pCurrent= pCurrent->pNext;
  126.     }
  127.     //connect the previous node to the one after the deleted node
  128.     Node * pTemp = pCurrent->pNext;
  129.     pCurrent->pNext = pTemp->pNext;
  130.  
  131.     //
  132.     if(pTail == pStart){
  133.         pTail = pTemp->pNext;
  134.     }
  135.     delete pTemp;
  136.    
  137. }//end of popNode
  138. //-------------------------------------------------------------------------------------------------
  139.  
  140.  
  141. //function to make a copy of the list.  It takes in a list and returns a new list.  
  142. Node * cpyList(Node * pTail){
  143.     //new list that is being created
  144.     Node * pHead = NULL;
  145.     //set node to the beggining of the list
  146.     Node *pTemp = pTail->pNext;
  147.     //since its a circularly, singly linked list, i have to use a do-while so that copies the first thing then updates the
  148.         //pNext and stops when it reaches pTail again.
  149.     if(pTail != NULL){
  150.         do{
  151.             //add node to the new list
  152.             appendNode(pHead, pTemp->livingThing);
  153.             //increment pTemp
  154.             pTemp= pTemp->pNext;
  155.         }while(pTemp != pTail->pNext);
  156.     }  
  157.     return pHead;
  158. }//end of Node * cpyList()
  159. //--------------------------------------------------------------------------------------------------
  160.  
  161.  
  162. //function that checks whether or not there are any androids.  It takes in a list and returns true or false.
  163. int areThereAndroids(Node * &pTail){
  164.     int value = 0;
  165.     //node to keep track of the tail
  166.     Node *pTemp = pTail->pNext;
  167.     //since its a circularly, singly linked list, i have to use a do-while so that it prints out the first thing then updates the
  168.         //pNext and stops when it reaches pTail again.
  169.     if(pTail != NULL){
  170.         do{
  171.             if(pTemp->livingThing == 'A'){
  172.                 value = 1;
  173.                 break;
  174.             }
  175.             else{
  176.                 value = 0;
  177.             }
  178.             pTemp= pTemp->pNext;
  179.         }while(pTemp != pTail->pNext);
  180.  
  181.     }
  182.     return value;  
  183. }//end of areThereAndroids()
  184. //--------------------------------------------------------------------------------------------------
  185.  
  186.  
  187. //function checks whether or not the current node is a human or not  It takes in a list and number (n) and returns true or false.  
  188. int isSolution(Node * &pTail, Node * current){
  189.     if(current->livingThing == 'H'){
  190.         return 0;
  191.     }
  192.     else{
  193.         return 1;
  194.     }
  195.     return 1;
  196. }//end of solution()
  197. //---------------------------------------------------------------------------------------------------
  198.  
  199.  
  200. //function to get nth node
  201. Node * nThNode(Node * &pTail, int n){
  202.     //keeping track of head pointer
  203.     Node * pHead = pTail->pNext;
  204.     //traverse list until we reach the node we want
  205.     while (n > 1 ){
  206.         pHead = pHead->pNext;
  207.         n--;
  208.     }
  209.  
  210.     return pHead;
  211. }//end of nThNode
  212. //---------------------------------------------------------------------------------------------------
  213.  
  214.  
  215. //main
  216. int main()
  217. {
  218.     Node *pTail = NULL;
  219.     Node *pHead = NULL;
  220.     Node *pTemp = NULL;
  221.  
  222.     readFromFileAndStore(pTail);
  223.     pHead = cpyList(pTail);
  224.  
  225.     int n = 1;
  226.     int flag = 1;
  227.  
  228.     while(areThereAndroids(pHead) == 1){
  229.        
  230.         if(isSolution(pHead, nThNode(pHead, n)->pNext) == 1 && flag == 1){
  231.             flag = 0;
  232.             pTemp = nThNode(pHead, n);
  233.             deleteNode(nThNode(pHead, n)->pNext, pHead);
  234.             pHead = pTemp;
  235.         }  
  236.         if(isSolution(pHead, nThNode(pHead, n-1)->pNext) == 1 && flag == 0){
  237.             pTemp = nThNode(pHead, n-1);
  238.             deleteNode(nThNode(pHead, n-1)->pNext, pHead);
  239.             pHead = pTemp;
  240.  
  241.             if(areThereAndroids(pHead) == 0){
  242.                 cout << "\nSolution = " << n << endl;
  243.                 return 1;
  244.             }
  245.         }
  246.         else{
  247.             pHead = cpyList(pTail);
  248.             n = n+1;
  249.             flag = 1;
  250.             if(n > numOfNodes(pHead)){
  251.                 cout << "\nThere is no solution!\n\n";
  252.                 return 0;
  253.             }
  254.         }
  255.     }
  256.     if(areThereAndroids(pHead) == 0){
  257.         cout << "\nThere is no solution or the solution is 0!\n";
  258.     }
  259.     return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement