Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<fstream>
- #include <iomanip>
- #include<cstring>
- #include<cctype>
- #include<cstdlib>
- using namespace std;
- struct Nodetype;
- typedef Nodetype* Nodeptr;
- struct Nodetype //struct to hold information about each dog
- {
- long id;
- char name[20];
- char breed[50];
- int position; //Keeps track of the position of the dog in the line
- Nodeptr Left, Right;
- int numnodes; //Field is used only in the header node to keep track of # nodes
- };
- ofstream outs;
- int main()
- {
- char choice[3]; //to hold choice made by user
- char ch1,ch2; //to separate out the characters choices.
- Nodeptr Head;
- void menu();
- Nodeptr InitializeHeader();
- void Createlist(Nodeptr Head);
- void PrintList(Nodeptr Head);
- void PrintFirst(Nodeptr Head);
- void PrintLast(Nodeptr Head);
- void Deletenode(Nodeptr Head);
- void Replacenode(Nodeptr Head);
- void Reposition(Nodeptr Head);
- void Printanode(Nodeptr Head);
- Nodeptr Search(Nodeptr Head, long idnum);
- Nodeptr Search2(Nodeptr Head, int pos);
- bool Empty(Nodeptr Head);
- void Announce_Winner(Nodeptr Head);
- Head = InitializeHeader();
- menu();
- cout<<endl;
- for(int i=0;i<3;i++)
- choice[i] = ' ';
- cout<<"Enter your choice."<<endl;
- cin.getline(choice,3);
- ch1 = choice[0];
- ch1 = toupper(ch1);
- ch2 = choice[1];
- ch2 = toupper(ch2);
- while (ch1 != 'Q')
- {
- switch(ch1)
- {
- case 'C':Createlist(Head);
- break;
- case 'P':PrintList(Head);
- break;
- case 'F': PrintFirst(Head);
- break;
- case 'L':PrintLast(Head);
- break;
- case 'D':switch(ch2)
- {
- case 'R':Replacenode(Head);
- break;
- case '\0': Deletenode(Head);
- break;
- }
- break;
- case 'R': Reposition(Head);
- break;
- case 'S': Printanode(Head);
- break;
- default : cout<<"Invalid choice. Try again!"<<endl;
- }
- cout<<endl;
- menu();
- for(int i=0;i<3;i++)
- choice[i] = ' ';
- cout<<"Please enter your choice."<<endl;
- cin.getline(choice,3);
- ch1 = choice[0];
- ch1 = toupper(ch1);
- ch2 = choice[1];
- ch2 = toupper(ch2);
- }
- Announce_Winner(Head);
- system("PAUSE");
- return 0;
- }
- /**************************************************************************
- The following function displays the menu of option to the screen.
- ****************************************************************************/
- void menu()
- {
- cout<<"C or c --- Create list of dogs competing in Best of Show."<<endl;
- cout<<"P or p --- Print the list of dogs by position in line."<<endl;
- cout<<"F or f --- Print the id, name, and breed of first place dog."<<endl;
- cout<<"L or l --- Print the id, name, and breed of last place dog."<<endl;
- cout<<"D or d --- Deletes a dog to be identified by id number."<<endl;
- cout<<"DR or dr --- Replace a dog whose id number you specify."<<endl;
- cout<<"R or r --- Allows the judge to reposition a dog in the line."<<endl;
- cout<<"S or s --- Show the position,id,name,breed of dog speicifed by id."<<endl;
- cout<<"Q or q --- Quit the program and announce the winner."<<endl;
- cout<<endl;
- return;
- }
- /**************************************************************************
- This function initializes the list to an empty list.
- ****************************************************************************/
- Nodeptr InitializeHeader()
- {
- Nodeptr q;
- q = new Nodetype;
- q->Right = q;
- q->Left = q;
- q->numnodes = 0;
- q->position = 0;
- return q;
- }
- /****************************************************************************
- This function adds a new node to the end of the existing list.
- ******************************************************************************/
- void Createlist(Nodeptr Head)
- {
- Nodeptr p,q;
- int idnum;
- int count;
- bool Empty(Nodeptr List);
- Nodeptr Search(Nodeptr List, long num);
- cout<<"Enter id number of the dog(-1 to stop):";
- cin>>idnum;
- cin.ignore();
- count = 0;
- while(idnum != -1)
- {
- p = Search(Head, idnum);
- if (p != NULL)
- cout<<"This dog is already in line!"<<endl;
- else
- {
- count++;
- q = new Nodetype;
- q->id = idnum;
- cout<<"Enter the dog's name:";
- cin.getline(q->name,20);
- cout<<"Enter the dog's breed:";
- cin.getline(q->breed,50);
- q->position = count;
- p = Head->Left;
- q->Right = Head;
- q->Left = p;
- p->Right = q;
- Head->Left = q;
- Head->numnodes++;
- }
- cout<<endl;
- cout<<"Enter id number of the dog(-1 to stop):";
- cin>>idnum;
- cin.ignore();
- }
- return;
- }
- /********************************************************************
- This function will display the contents of each node in the list
- both at the screen and in the external text file.
- **************************************************************************/
- void PrintList(Nodeptr Head)
- {
- Nodeptr p;
- bool Empty(Nodeptr List);
- ofstream outs;
- outs.open("dogs.txt", ios::app);
- p = Head;
- if (Empty(Head))
- cout<<"List is empty."<<endl;
- else
- {
- cout<<"List of all dogs:"<<endl<<endl;
- outs<<"List of all dogs:"<<endl<<endl;
- cout<<"Position"<<setw(15)<<"ID Number"<<setw(19)<<"Breed"<<setw(15)<<"Name"
- <<endl;
- outs<<"Position"<<setw(15)<<"ID Number"<<setw(19)<<"Breed"<<setw(15)<<"Name"
- <<endl;
- do
- {
- p = p->Right;
- cout<<right<<setw(4)<<p->position<<setw(15)<<p->id<<setw(26)
- <<p->breed<<" "<<left<<setw(10)<<p->name<<endl;
- outs<<right<<setw(4)<<p->position<<setw(15)<<p->id<<setw(26)
- <<p->breed<<" "<<left<<setw(10)<<p->name<<endl;
- }
- while (p->Right !=Head);
- }
- cout<<right;
- outs.close();
- return;
- }
- /******************************************************************
- This function is to display the name, breed, and position of the
- dog who is in first place.
- *********************************************************************/
- void PrintFirst(Nodeptr Head)
- {
- bool Empty(Nodeptr List);
- ofstream outs;
- outs.open("dogs.txt", ios::app);
- if (Empty(Head))
- cout<<"List is empty."<<endl;
- else
- {
- cout<<endl;
- cout<<"The dog first in line is:\n";
- cout<<"#"<<Head->Right->id<<": "<<Head->Right->name<<" "<<Head->Right->breed<<endl;
- outs<<endl;
- outs<<"The dog first in line is:\n";
- outs<<"#"<<Head->Right->id<<": "<<Head->Right->name<<" "<<Head->Right->breed<<endl;
- }
- cout<<endl<<endl;
- outs<<endl<<endl;
- outs.close();
- return;
- }
- /**********************************************************************
- This function is to display the name, breed, and position of the
- dog who is in last place.
- *********************************************************************/
- void PrintLast(Nodeptr Head)
- {
- bool Empty(Nodeptr List);
- ofstream outs;
- outs.open("dogs.txt", ios::app);
- if (Empty(Head))
- cout<<"List is empty."<<endl;
- else
- {
- cout<<endl;
- cout<<"The dog last in line is:\n";
- cout<<"#"<<Head->Left->id<<": "<<Head->Left->name<<" "<<Head->Left->breed<<endl;
- outs<<endl;
- outs<<"The dog last in line is:\n";
- outs<<"#"<<Head->Left->id<<": "<<Head->Left->name<<" "<<Head->Left->breed<<endl;
- }
- cout<<endl;
- outs<<endl;
- outs.close();
- return;
- }
- /**********************************************************************
- This function deletes a node from the list based upon the id number
- of the dog that is specified.
- ***********************************************************************/
- void Deletenode(Nodeptr Head)
- {
- long idnum;
- int count;
- Nodeptr p;
- Nodeptr Search(Nodeptr List, long idnum);
- bool Empty(Nodeptr List);
- outs.open("dogs.txt", ios::app);
- if (Empty(Head))
- cout<<"Empty List!"<<endl;
- else
- {
- cout<<"Enter id number of the dog you wish to delete."<<endl;
- cin>>idnum;
- cin.ignore();
- p = Search(Head, idnum);
- if (p==NULL)
- cout<<"The dog is not in the line!"<<endl;
- else
- {
- p->Left->Right = p->Right;
- p->Right->Left = p->Left;
- delete p;
- Head->numnodes--;
- cout<<"Dog "<<idnum<<" removed from the line!"<<endl<<endl;
- outs<<"Dog "<<idnum<<" removed from the line!"<<endl<<endl;
- p = Head->Right;
- count = 0;
- while(p !=Head)
- {
- count++;
- p->position = count;
- p = p->Right;
- }
- }
- }
- outs.close();
- return;
- }
- /***************************************************************************
- This function replaces a node(dog) in the list with another existing node (dog).
- ***********************************************************************/
- void Replacenode(Nodeptr Head)
- {
- long idnum;
- Nodeptr p, q, r;
- bool Empty(Nodeptr List);
- Nodeptr Search(Nodeptr List, long idnum);
- int temp = 0;
- ofstream outs;
- outs.open("dogs.txt", ios::app);
- if (Empty(Head))
- cout<<"List is empty."<<endl;
- else
- {
- cout<<"Enter id number of the dog you wish to move."<<endl;
- cin>>idnum;
- cin.ignore();
- p = Search(Head, idnum);
- if (p==NULL)
- cout<<"The dog is not in the line!"<<endl;
- else
- {
- q = p;
- cout<<"Enter the id number of the dog you wish to replace."<<endl;
- cin>>idnum;
- cin.ignore();
- p = Search(Head, idnum);
- if (p==NULL)
- cout<<"The dog is not in the line!"<<endl;
- else
- {
- cout<<"Replacing dog..."<<endl;
- outs<<"Replacing dog..."<<endl;
- q->Left->Right = q->Left->Right->Right;
- q->Right->Left = q->Right->Left->Left;
- q->Right = NULL;
- q->Left = NULL;
- q->Left = p->Left;
- q->Right = p->Right;
- p->Left->Right = q;
- p->Right->Left = q;
- p->Left = NULL;
- p->Right = NULL;
- delete p;
- r = Head->Right;
- temp = 1;
- do
- {
- r->position = temp;
- r = r->Right;
- temp++;
- }while(r != Head);
- cout<<"Dog successfully replaced."<<endl<<endl;
- outs<<"Dog successfully replaced."<<endl<<endl;
- }
- }
- }
- outs.close();
- return;
- }
- /*********************************************************************
- This function is to reposition a dog in the line.
- *****************************************************************************/
- void Reposition(Nodeptr Head)
- {
- bool Empty(Nodeptr List);
- Nodeptr p, q, r;
- Nodeptr Search2(Nodeptr List, int pos);
- int pos, temp;
- temp = 0;
- outs.open("dogs.txt", ios::app);
- if (Empty(Head))
- cout<<"List is empty."<<endl;
- else
- {
- cout<<"Enter position of the dog you wish to move."<<endl;
- cin>>pos;
- cin.ignore();
- p = Search2(Head, pos);
- if (p==NULL)
- cout<<"That position does not exist!"<<endl;
- else
- {
- q = p;
- cout<<"Which position do you want to move it to?"<<endl;
- cin>>pos;
- cin.ignore();
- p = Search2(Head, pos);
- if (p==NULL)
- cout<<"That position does not exist!"<<endl;
- else
- {
- if((p->position) < (q->position))
- { //Move node from right to left
- cout<<"Moving dog..."<<endl;
- outs<<"Moving dog..."<<endl;
- q->Left->Right = q->Left->Right->Right;
- q->Right->Left = q->Right->Left->Left;
- q->Right = NULL;
- q->Left = NULL;
- q->Left = p->Left;
- p->Left->Right = q;
- p->Left = q;
- q->Right = p;
- r = Head->Right;
- temp = 1;
- do
- {
- r->position = temp;
- r = r->Right;
- temp++;
- }while(r != Head);
- cout<<"Dog successfully moved."<<endl<<endl;
- outs<<"Dog successfully moved."<<endl<<endl;
- }
- else
- {//Move node from left to right
- cout<<"Moving dog..."<<endl;
- outs<<"Moving dog..."<<endl;
- q->Left->Right = q->Left->Right->Right;
- q->Right->Left = q->Right->Left->Left;
- q->Right = NULL;
- q->Left = NULL;
- q->Right = p->Right;
- p->Right->Left = q;
- p->Right = q;
- q->Left = p;
- r = Head->Right;
- temp = 1;
- do
- {
- r->position = temp;
- r = r->Right;
- temp++;
- }while(r != Head);
- cout<<"Dog successfully moved."<<endl<<endl;
- outs<<"Dog successfully moved."<<endl<<endl;
- }
- }
- }
- }
- outs.close();
- return;
- }
- /***********************************************************************
- This function is to display the information of a dog in line, identified
- by id number.
- *************************************************************************/
- void Printanode(Nodeptr Head)
- {
- int idnum;
- Nodeptr p;
- bool Empty(Nodeptr List);
- Nodeptr Search(Nodeptr List, long num);
- outs.open("dogs.txt", ios::app);
- if (Empty(Head))
- cout<<"List is empty."<<endl;
- else
- {
- cout<<"Enter the ID number of the dog:"<<endl;
- cin>>idnum;
- cin.ignore();
- p = Search(Head, idnum);
- if (p == NULL)
- cout<<"This dog is not in the line."<<endl;
- else
- {
- cout<<"Locating dog at position: "<<p->position<<endl;
- cout<<"Position #"<<p->position<<": "<<" Id number "<<p->id
- <<" : "<<p->name<<endl<<endl;
- outs<<"Locating dog at position: "<<p->position<<endl;
- outs<<"Position #"<<p->position<<": "<<" Id number "<<p->id
- <<" : "<<p->name<<endl<<endl;
- }
- }
- outs.close();
- return;
- }
- /***************************************************************************
- This function searches for a dog in the list based upon the dog's id number.
- ****************************************************************************/
- Nodeptr Search(Nodeptr Head, long idnum)
- {
- int found = 0;
- Nodeptr p;
- bool Empty(Nodeptr List);
- if (Empty(Head))
- return NULL;
- else
- {
- p = Head->Right;
- do
- {
- if (idnum == p->id)
- found = 1;
- else
- p = p->Right;
- }while (!found &&(p != Head));
- if (p != Head)
- return p;
- else
- return NULL;
- }
- }
- /****************************************************************************
- This function searches for a dog in the list based upon the dog's position
- in the list.
- ****************************************************************************/
- Nodeptr Search2(Nodeptr Head, int pos)
- {
- int found = 0;
- Nodeptr p;
- bool Empty(Nodeptr List);
- if (Empty(Head))
- return NULL;
- else
- {
- p = Head->Right;
- do
- {
- if (pos == p->position)
- found = 1;
- else
- p = p->Right;
- }while (!found &&(p != Head));
- if (p != Head)
- return p;
- else
- return NULL;
- }
- }
- /*********************************************************************
- This function checks for an empty list.
- **********************************************************************/
- bool Empty(Nodeptr Head)
- {
- if ((Head->Right == Head) && (Head->Left == Head))
- return true;
- else
- return false;
- }
- /**************************************************************************
- This function announces the winner of "Best in Show" both at the screen
- and at the external text file.
- ****************************************************************************/
- void Announce_Winner(Nodeptr Head)
- {
- bool Empty(Nodeptr List);
- ofstream outs;
- outs.open("dogs.txt", ios::app);
- if (Empty(Head))
- cout<<"List is empty."<<endl;
- else
- {
- cout<<"The winner of Best in Show is: "<<Head->Right->name<<"!!"<<endl;
- outs<<"The winner of Best in Show is: "<<Head->Right->name<<"!!"<<endl;
- }
- outs.close();
- return;
- }
Add Comment
Please, Sign In to add comment