Guest User

C++ Linked List Project

a guest
Feb 18th, 2018
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.33 KB | None | 0 0
  1. #include <iostream>
  2. #include<fstream>
  3. #include <iomanip>
  4. #include<cstring>
  5. #include<cctype>
  6. #include<cstdlib>
  7. using namespace std;
  8.  
  9. struct Nodetype;
  10. typedef Nodetype* Nodeptr;
  11.  
  12. struct Nodetype //struct to hold information about each dog
  13. {
  14. long id;
  15. char name[20];
  16. char breed[50];
  17. int position; //Keeps track of the position of the dog in the line
  18. Nodeptr Left, Right;
  19. int numnodes;  //Field is used only in the header node to keep track of # nodes
  20. };
  21. ofstream outs;
  22.  
  23. int main()
  24. {
  25. char choice[3];     //to hold choice made by user
  26. char ch1,ch2;       //to separate out the characters choices.
  27. Nodeptr Head;
  28.  
  29.  
  30. void menu();                    
  31. Nodeptr InitializeHeader();    
  32. void Createlist(Nodeptr Head);  
  33. void PrintList(Nodeptr Head);
  34. void PrintFirst(Nodeptr Head);
  35. void PrintLast(Nodeptr Head);
  36. void Deletenode(Nodeptr Head);
  37. void Replacenode(Nodeptr Head);
  38. void Reposition(Nodeptr Head);
  39. void Printanode(Nodeptr Head);
  40. Nodeptr Search(Nodeptr Head, long idnum);
  41. Nodeptr Search2(Nodeptr Head, int pos);
  42. bool Empty(Nodeptr Head);
  43. void Announce_Winner(Nodeptr Head);
  44.  
  45. Head = InitializeHeader();
  46.  
  47.  
  48. menu();
  49. cout<<endl;
  50. for(int i=0;i<3;i++)
  51.    choice[i] = ' ';
  52. cout<<"Enter your choice."<<endl;
  53. cin.getline(choice,3);
  54. ch1 = choice[0];
  55. ch1 = toupper(ch1);
  56. ch2 = choice[1];
  57. ch2 = toupper(ch2);
  58. while (ch1 != 'Q')
  59.    {
  60.      switch(ch1)
  61.     {
  62.      case 'C':Createlist(Head);
  63.               break;
  64.      case 'P':PrintList(Head);
  65.               break;
  66.      case 'F': PrintFirst(Head);
  67.                break;
  68.      case 'L':PrintLast(Head);
  69.               break;
  70.      case 'D':switch(ch2)
  71.           {
  72.            case 'R':Replacenode(Head);
  73.                     break;
  74.            case '\0': Deletenode(Head);
  75.                       break;
  76.           }
  77.            break;
  78.      case 'R': Reposition(Head);
  79.                break;
  80.      case 'S': Printanode(Head);
  81.                break;
  82.      default : cout<<"Invalid choice. Try again!"<<endl;
  83.     }
  84.      cout<<endl;
  85.      menu();
  86.      for(int i=0;i<3;i++)
  87.     choice[i] = ' ';
  88.      cout<<"Please enter your choice."<<endl;
  89.      cin.getline(choice,3);
  90.      ch1 = choice[0];
  91.      ch1 = toupper(ch1);
  92.      ch2 = choice[1];
  93.      ch2 = toupper(ch2);
  94.    }
  95.   Announce_Winner(Head);   
  96.   system("PAUSE");
  97.   return 0;
  98. }
  99.  
  100. /**************************************************************************
  101. The following function displays the menu of option to the screen.
  102. ****************************************************************************/
  103.  
  104. void menu()
  105. {
  106.   cout<<"C or c    --- Create list of dogs competing in Best of Show."<<endl;
  107.   cout<<"P or p    --- Print the list of dogs by position in line."<<endl;
  108.   cout<<"F or f    --- Print the id, name, and breed of first place dog."<<endl;
  109.   cout<<"L or l    --- Print the id, name, and breed of last place dog."<<endl;
  110.   cout<<"D or d    --- Deletes a dog to be identified by id number."<<endl;
  111.   cout<<"DR or dr  --- Replace a dog whose id number you specify."<<endl;
  112.   cout<<"R or r    --- Allows the judge to reposition a dog in the line."<<endl;
  113.   cout<<"S or s    --- Show the position,id,name,breed of dog speicifed by id."<<endl;
  114.   cout<<"Q or q    --- Quit the program and announce the winner."<<endl;
  115.   cout<<endl;
  116.   return;
  117. }
  118.  
  119. /**************************************************************************
  120. This function initializes the list to an empty list.
  121. ****************************************************************************/
  122.  
  123. Nodeptr InitializeHeader()
  124. {
  125.    Nodeptr q;
  126.    q = new Nodetype;
  127.    q->Right = q;
  128.    q->Left = q;
  129.    q->numnodes = 0;
  130.    q->position = 0;
  131.    return q;
  132. }
  133.  
  134. /****************************************************************************
  135. This function adds a new node to the end of the existing list.
  136. ******************************************************************************/
  137.  
  138. void Createlist(Nodeptr Head)
  139. {
  140.   Nodeptr p,q;
  141.   int idnum;
  142.   int count;
  143.   bool Empty(Nodeptr List);
  144.   Nodeptr Search(Nodeptr List, long num);
  145.   cout<<"Enter id number of the dog(-1 to stop):";
  146.   cin>>idnum;
  147.   cin.ignore();
  148.   count = 0;
  149.   while(idnum != -1)
  150.      {
  151.     p = Search(Head, idnum);
  152.     if (p != NULL)
  153.        cout<<"This dog is already in line!"<<endl;
  154.     else
  155.        {
  156.          count++;
  157.          q = new Nodetype;
  158.          q->id = idnum;
  159.          cout<<"Enter the dog's name:";
  160.          cin.getline(q->name,20);
  161.          cout<<"Enter the dog's breed:";
  162.          cin.getline(q->breed,50);
  163.          q->position = count;
  164.          p = Head->Left;
  165.          q->Right = Head;
  166.          q->Left = p;
  167.          p->Right = q;
  168.          Head->Left = q;
  169.          Head->numnodes++;
  170.        }
  171.     cout<<endl;
  172.     cout<<"Enter id number of the dog(-1 to stop):";
  173.     cin>>idnum;
  174.     cin.ignore();
  175.      }
  176.   return;
  177. }
  178.  
  179. /********************************************************************
  180. This function will display the contents of each node in the list
  181. both at the screen and in the external text file.
  182. **************************************************************************/
  183.  
  184. void PrintList(Nodeptr Head)
  185. {
  186.    Nodeptr p;
  187.    bool Empty(Nodeptr List);
  188.    ofstream outs;
  189.    
  190.    outs.open("dogs.txt", ios::app);
  191.  
  192.    p = Head;
  193.   if (Empty(Head))
  194.       cout<<"List is empty."<<endl;
  195.    else
  196.       {
  197.        cout<<"List of all dogs:"<<endl<<endl;
  198.        outs<<"List of all dogs:"<<endl<<endl;
  199.        
  200.        cout<<"Position"<<setw(15)<<"ID Number"<<setw(19)<<"Breed"<<setw(15)<<"Name"
  201.            <<endl;
  202.            
  203.        outs<<"Position"<<setw(15)<<"ID Number"<<setw(19)<<"Breed"<<setw(15)<<"Name"
  204.            <<endl;
  205.      
  206.        do
  207.        {
  208.          p = p->Right;
  209.          cout<<right<<setw(4)<<p->position<<setw(15)<<p->id<<setw(26)
  210.              <<p->breed<<"        "<<left<<setw(10)<<p->name<<endl;
  211.          outs<<right<<setw(4)<<p->position<<setw(15)<<p->id<<setw(26)
  212.              <<p->breed<<"        "<<left<<setw(10)<<p->name<<endl;
  213.        }
  214.        while (p->Right !=Head);
  215.       }
  216.       cout<<right;
  217.      
  218.       outs.close();
  219.    
  220.      return;
  221. }
  222.  
  223.  
  224. /******************************************************************
  225. This function is to display the name, breed, and position of the
  226. dog who is in first place.
  227. *********************************************************************/
  228.  
  229. void PrintFirst(Nodeptr Head)
  230. {
  231.   bool Empty(Nodeptr List);
  232.   ofstream outs;
  233.    
  234.   outs.open("dogs.txt", ios::app);
  235.  
  236.    if (Empty(Head))
  237.       cout<<"List is empty."<<endl;
  238.    else
  239.   {
  240.    cout<<endl;
  241.    cout<<"The dog first in line is:\n";
  242.    cout<<"#"<<Head->Right->id<<": "<<Head->Right->name<<"  "<<Head->Right->breed<<endl;
  243.    outs<<endl;
  244.    outs<<"The dog first in line is:\n";
  245.    outs<<"#"<<Head->Right->id<<": "<<Head->Right->name<<"  "<<Head->Right->breed<<endl;
  246.   }
  247.   cout<<endl<<endl;
  248.   outs<<endl<<endl;
  249.  
  250.   outs.close();
  251.  
  252.   return;
  253. }
  254.  
  255.  
  256. /**********************************************************************
  257. This function is to display the name, breed, and position of the
  258. dog who is in last place.
  259. *********************************************************************/
  260.  
  261. void PrintLast(Nodeptr Head)
  262. {
  263.   bool Empty(Nodeptr List);
  264.   ofstream outs;
  265.    
  266.   outs.open("dogs.txt", ios::app);
  267.  
  268.   if (Empty(Head))
  269.       cout<<"List is empty."<<endl;
  270.    else
  271.   {
  272.    cout<<endl;
  273.    cout<<"The dog last in line is:\n";
  274.    cout<<"#"<<Head->Left->id<<": "<<Head->Left->name<<"  "<<Head->Left->breed<<endl;
  275.    outs<<endl;
  276.    outs<<"The dog last in line is:\n";
  277.    outs<<"#"<<Head->Left->id<<": "<<Head->Left->name<<"  "<<Head->Left->breed<<endl;
  278.   }
  279.   cout<<endl;
  280.   outs<<endl;
  281.  
  282.   outs.close();
  283.  
  284.   return;
  285. }
  286.  
  287. /**********************************************************************
  288. This function deletes a node from the list based upon the id number
  289. of the dog that is specified.
  290. ***********************************************************************/
  291.  
  292. void Deletenode(Nodeptr Head)
  293. {
  294.  long idnum;
  295.  int count;
  296.  Nodeptr p;
  297.  Nodeptr Search(Nodeptr List, long idnum);
  298.  bool Empty(Nodeptr List);
  299.  outs.open("dogs.txt", ios::app);
  300.  
  301.  if (Empty(Head))
  302.     cout<<"Empty List!"<<endl;
  303.  else
  304.     {
  305.       cout<<"Enter id number of the dog you wish to delete."<<endl;
  306.       cin>>idnum;
  307.       cin.ignore();
  308.       p = Search(Head, idnum);
  309.       if (p==NULL)
  310.      cout<<"The dog is not in the line!"<<endl;
  311.       else
  312.      {
  313.        p->Left->Right = p->Right;
  314.        p->Right->Left = p->Left;
  315.        delete p;
  316.        Head->numnodes--;
  317.        cout<<"Dog "<<idnum<<" removed from the line!"<<endl<<endl;
  318.        outs<<"Dog "<<idnum<<" removed from the line!"<<endl<<endl;
  319.        p = Head->Right;
  320.        count = 0;
  321.        while(p !=Head)
  322.        {
  323.            count++;
  324.            p->position = count;
  325.            p = p->Right;
  326.        }
  327.      }
  328.     }
  329.     outs.close();
  330.  return;
  331. }
  332.  
  333.  
  334. /***************************************************************************
  335. This function replaces a node(dog) in the list with another existing node (dog).
  336. ***********************************************************************/
  337.  
  338. void Replacenode(Nodeptr Head)
  339. {
  340.    long idnum;
  341.    Nodeptr p, q, r;
  342.    bool Empty(Nodeptr List);
  343.    Nodeptr Search(Nodeptr List, long idnum);
  344.    int temp = 0;
  345.    ofstream outs;
  346.    
  347.    outs.open("dogs.txt", ios::app);
  348.    
  349.    if (Empty(Head))
  350.       cout<<"List is empty."<<endl;
  351.    else
  352.    {
  353.       cout<<"Enter id number of the dog you wish to move."<<endl;
  354.       cin>>idnum;
  355.       cin.ignore();
  356.       p = Search(Head, idnum);
  357.       if (p==NULL)
  358.       cout<<"The dog is not in the line!"<<endl;
  359.       else
  360.        {
  361.           q = p;
  362.           cout<<"Enter the id number of the dog you wish to replace."<<endl;
  363.           cin>>idnum;
  364.           cin.ignore();
  365.           p = Search(Head, idnum);
  366.           if (p==NULL)
  367.           cout<<"The dog is not in the line!"<<endl;
  368.           else
  369.             {
  370.              cout<<"Replacing dog..."<<endl;
  371.              outs<<"Replacing dog..."<<endl;
  372.             q->Left->Right = q->Left->Right->Right;
  373.             q->Right->Left = q->Right->Left->Left;
  374.             q->Right = NULL;
  375.             q->Left = NULL;
  376.             q->Left = p->Left;
  377.             q->Right = p->Right;
  378.             p->Left->Right = q;
  379.             p->Right->Left = q;
  380.             p->Left = NULL;
  381.             p->Right = NULL;
  382.             delete p;
  383.            
  384.             r = Head->Right;
  385.             temp = 1;
  386.            
  387.              do
  388.             {
  389.              r->position = temp;
  390.              r = r->Right;
  391.              temp++;
  392.             }while(r != Head);
  393.            
  394.             cout<<"Dog successfully replaced."<<endl<<endl;
  395.             outs<<"Dog successfully replaced."<<endl<<endl;
  396.             }
  397.        }
  398.    }
  399.    
  400.    outs.close();
  401.    return;
  402. }
  403.  
  404.  
  405. /*********************************************************************
  406. This function is to reposition a dog in the line.
  407. *****************************************************************************/
  408. void Reposition(Nodeptr Head)
  409. {
  410.    
  411.    bool Empty(Nodeptr List);
  412.    Nodeptr p, q, r;
  413.    Nodeptr Search2(Nodeptr List, int pos);
  414.    int pos, temp;
  415.    temp = 0;
  416.    outs.open("dogs.txt", ios::app);
  417.      
  418.    if (Empty(Head))
  419.       cout<<"List is empty."<<endl;
  420.    else
  421.    {
  422.     cout<<"Enter position of the dog you wish to move."<<endl;
  423.       cin>>pos;
  424.       cin.ignore();
  425.       p = Search2(Head, pos);
  426.       if (p==NULL)
  427.       cout<<"That position does not exist!"<<endl;
  428.       else
  429.        {
  430.        q = p;
  431.        cout<<"Which position do you want to move it to?"<<endl;
  432.        cin>>pos;
  433.        cin.ignore();
  434.        p = Search2(Head, pos);
  435.        if (p==NULL)
  436.        cout<<"That position does not exist!"<<endl;
  437.         else
  438.          {
  439.           if((p->position) < (q->position))
  440.           { //Move node from right to left
  441.             cout<<"Moving dog..."<<endl;
  442.             outs<<"Moving dog..."<<endl;
  443.             q->Left->Right = q->Left->Right->Right;
  444.             q->Right->Left = q->Right->Left->Left;
  445.             q->Right = NULL;
  446.             q->Left = NULL;
  447.             q->Left = p->Left;
  448.             p->Left->Right = q;
  449.             p->Left = q;
  450.             q->Right = p;
  451.            
  452.             r = Head->Right;
  453.             temp = 1;
  454.            
  455.             do
  456.             {
  457.                 r->position = temp;
  458.                 r = r->Right;
  459.                 temp++;
  460.             }while(r != Head);
  461.             cout<<"Dog successfully moved."<<endl<<endl;
  462.             outs<<"Dog successfully moved."<<endl<<endl;              
  463.             }
  464.           else
  465.              {//Move node from left to right
  466.              cout<<"Moving dog..."<<endl;
  467.              outs<<"Moving dog..."<<endl;
  468.              q->Left->Right = q->Left->Right->Right;
  469.              q->Right->Left = q->Right->Left->Left;
  470.              q->Right = NULL;
  471.              q->Left = NULL;
  472.              q->Right = p->Right;
  473.              p->Right->Left = q;
  474.              p->Right = q;
  475.              q->Left = p;
  476.            
  477.             r = Head->Right;
  478.             temp = 1;
  479.            
  480.             do
  481.             {
  482.                 r->position = temp;
  483.                 r = r->Right;
  484.                 temp++;
  485.             }while(r != Head);
  486.             cout<<"Dog successfully moved."<<endl<<endl;
  487.             outs<<"Dog successfully moved."<<endl<<endl;
  488.             }
  489.          
  490.          }  
  491.        }        
  492.     }
  493.    
  494.    outs.close();
  495.    return; 
  496. }
  497.  
  498. /***********************************************************************
  499. This function is to display the information of a dog in line, identified
  500. by id number.
  501. *************************************************************************/
  502.  
  503. void Printanode(Nodeptr Head)
  504. {
  505.   int idnum;
  506.   Nodeptr p;
  507.   bool Empty(Nodeptr List);
  508.   Nodeptr Search(Nodeptr List, long num);
  509.   outs.open("dogs.txt", ios::app);
  510.  
  511.   if (Empty(Head))
  512.       cout<<"List is empty."<<endl;
  513.    else
  514.   {
  515.        cout<<"Enter the ID number of the dog:"<<endl;
  516.        cin>>idnum;
  517.        cin.ignore();
  518.        p = Search(Head, idnum);
  519.        if (p == NULL)
  520.       cout<<"This dog is not in the line."<<endl;
  521.        else
  522.       {
  523.          cout<<"Locating dog at position: "<<p->position<<endl;
  524.          cout<<"Position #"<<p->position<<": "<<" Id number "<<p->id
  525.              <<" : "<<p->name<<endl<<endl;
  526.          
  527.          outs<<"Locating dog at position: "<<p->position<<endl;    
  528.          outs<<"Position #"<<p->position<<": "<<" Id number "<<p->id
  529.              <<" : "<<p->name<<endl<<endl;
  530.        
  531.       }
  532.      }
  533.      
  534.      outs.close();
  535.   return;
  536. }
  537.  
  538. /***************************************************************************
  539. This function searches for a dog in the list based upon the dog's id number.
  540. ****************************************************************************/
  541.  
  542. Nodeptr Search(Nodeptr Head, long idnum)
  543. {
  544.   int found = 0;
  545.   Nodeptr p;
  546.   bool Empty(Nodeptr List);
  547.  
  548.   if (Empty(Head))
  549.      return NULL;
  550.   else
  551.      {
  552.        p = Head->Right;
  553.        do
  554.      {
  555.        if (idnum == p->id)
  556.           found = 1;
  557.        else
  558.           p = p->Right;
  559.        
  560.       }while (!found &&(p != Head));
  561.        if (p != Head)
  562.       return p;
  563.        else
  564.       return NULL;
  565.      }
  566. }
  567.  
  568. /****************************************************************************
  569. This function searches for a dog in the list based upon the dog's position
  570. in the list.
  571. ****************************************************************************/
  572.  
  573. Nodeptr Search2(Nodeptr Head, int pos)
  574. {
  575.   int found = 0;
  576.   Nodeptr p;
  577.   bool Empty(Nodeptr List);
  578.  
  579.   if (Empty(Head))
  580.      return NULL;
  581.   else
  582.      {
  583.        p = Head->Right;
  584.        do
  585.      {
  586.        if (pos == p->position)
  587.           found = 1;
  588.        else
  589.           p = p->Right;
  590.        
  591.       }while (!found &&(p != Head));
  592.        if (p != Head)
  593.       return p;
  594.        else
  595.       return NULL;
  596.      }
  597. }
  598.  
  599. /*********************************************************************
  600. This function checks for an empty list.
  601. **********************************************************************/
  602.  
  603. bool Empty(Nodeptr Head)
  604. {
  605.    if ((Head->Right == Head) && (Head->Left == Head))
  606.       return true;
  607.    else
  608.       return false;
  609. }
  610.  
  611. /**************************************************************************
  612. This function announces the winner of "Best in Show" both at the screen
  613. and at the external text file.
  614. ****************************************************************************/
  615.  
  616. void Announce_Winner(Nodeptr Head)
  617. {
  618.   bool Empty(Nodeptr List);
  619.   ofstream outs;
  620.  
  621.   outs.open("dogs.txt", ios::app);
  622.  
  623.   if (Empty(Head))
  624.       cout<<"List is empty."<<endl;
  625.    else
  626.    {
  627.        cout<<"The winner of Best in Show is: "<<Head->Right->name<<"!!"<<endl;
  628.        outs<<"The winner of Best in Show is: "<<Head->Right->name<<"!!"<<endl;
  629.    }
  630.  
  631.   outs.close();
  632.  
  633.  
  634.   return;
  635. }
Add Comment
Please, Sign In to add comment