SHARE
TWEET

Untitled

a guest Mar 19th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include"Dog.h"
  2.  
  3. void insertDog(Dog *&head);
  4. void displayList(Dog *&head);
  5. void deleteList(Dog *&head);
  6.  
  7. int main()
  8. {
  9.     Dog *head = NULL;  
  10.     int count = 0;
  11.  
  12.     cout << "Enter dogs into the list: " << endl;
  13.    
  14.     char anotherOne = 'Y';
  15.     while (anotherOne == 'Y' || anotherOne == 'y')
  16.     {
  17.         insertDog(head);
  18.         cout << "Enter another Dog (Y or N)? ";
  19.         cin >> anotherOne;
  20.        
  21.     }
  22.  
  23.     char answer = 'y';
  24.     while (answer == 'y' || answer == 'Y')
  25.     {
  26.         cout << "Display the list (Y/N)?";
  27.         cin >> answer;
  28.         system("cls");
  29.         cout << "Here is the list:\n";
  30.         displayList(head);
  31.     }
  32.    
  33.     cout << "Here is the list after the delete: " << endl;
  34.     deleteList(head);
  35.  
  36.     system("pause");
  37.     return 0;
  38. }
  39.  
  40. void insertDog(Dog *&head)
  41. {
  42.     Dog *temp = new Dog;
  43.     cout << "ID: ";
  44.     cin >> temp->id;
  45.     cin.ignore();
  46.  
  47.     cout << "Name: ";
  48.     getline(cin, temp->name);
  49.  
  50.     temp->next = head;  
  51.     head = temp;
  52. }
  53.  
  54. void displayList(Dog *&head)
  55. {
  56.     Dog *temp = head;
  57.  
  58.     while (temp != NULL)
  59.     {
  60.         cout << "ID:  " << temp->id << endl;
  61.         cout << "Name " << temp->name << endl;
  62.         temp = temp->next;
  63.     }
  64. }
  65.  
  66. void deleteList(Dog *&head)
  67. {
  68.     Dog *lead = head;
  69.     Dog *follow = head;
  70.  
  71.     int idNum;
  72.     cout << "Enter an id of a dog to be deleted: ";
  73.     cin >> idNum;
  74.  
  75.     while ((lead->next != NULL) && (lead->id != idNum))
  76.     {
  77.         follow = lead;
  78.         lead = lead->next;
  79.     }
  80.  
  81.     if (lead == NULL)
  82.         cout << "Id was not found." << endl;
  83.     else
  84.         follow->next = lead->next;
  85.  
  86.     if (lead->next == NULL)
  87.     {
  88.         follow->next = NULL;
  89.         delete lead;
  90.     }
  91.  
  92.     else
  93.         delete lead;
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top