Question

In: Computer Science

Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...

Objective: Learning linked list.

Problem Specification:

            An employer would like to maintain a linked list for employees, the data stored is

·An employee number (a positive integer)

·A yearly salary (a float).

·Number of dependents (a short positive integer)

The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses a menu driven program with options to handle choices (methods). The methods are:

·Insert: Which inserts elements at the beginning of the list, which is the most recent input is at the beginning of the list.

·Remove: which deletes the last element in the list.

·Display: its purpose is to display the list but needs the assistance of a Print function.

·Print: a recursive function that prints all the elements of the list, first to last.

·Clear: a recursive function that deletes every Node from the list and leaves the list empty.

Requirements:

·Define a class Node containing the employee’s data and a pointer to the next Node.

·Define the necessary functions to access, instantiate, and set the data in the class Node.

·Define a class LinkedList that has only one data member, a pointer to a Node, and the necessary member functions in addition to the member functions above.

Grading criteria:

10 points         Sufficient comments including specifications

5 points         Menu is used to display options and calls methods.

5 points         Guards are used.

10 points         Insert performs it task correctly.

10 points         Remove performs it task correctly.

10 points         Display performs it task correctly.

10 points         print is recursive and performs it task correctly.

10 points         Clear performs it task correctly in a recursive manner.

10 points         UML class diagrams are submitted and each is correct.

15 points         Program runs correctly and performs its task correctly.

5 points         test run is handed-in and demonstrates all activities.

Submission Details:

Submit a print-out of:

·The source program

·Demonstration of all activities.

Solutions

Expert Solution

#include<iostream>
#include<string>
using namespace std;

//Class Node to hold data for single employee.

class Node
{
   private:
       string number;
       int year;
       unsigned short int dependents;
       Node *next;//Pointer to next node 1.e. next Employee data
   public:
       Node()//default constructor to set default values into data members
       {
           number="";
           year=0;
           dependents=0;
       }

//Setters
       void setNumber(string number_)
       {
           number=number_;
       }
       void setYear(int year_)
       {
           year=year_;
       }
       void setDependencies(unsigned short int dependents_)
       {
           dependents=dependents_;
       }
       void setNext(Node* next_)
       {
           next=next_;
       }
//Gettres
       string getNumber()
       {
           return number;
       }
       int getYear()
       {
           return year;
       }
       unsigned short int getDependencies()
       {
           return dependents;
       }
       Node *getNext()
       {
           return next;
       }
      
};

//Class List
class Employees
{
   private:
       Node *head;//To hold the start of the list
   public:
       Employees()//default constructior to set head to NULL
       {
           head=NULL;
       }
       void Insert(string no,int year_,unsigned short int dep)
       {
//Create new node
           Node *newNode=new Node();
           newNode->setNumber(no);
           newNode->setYear(year_);
           newNode->setDependencies(dep);


//Set New node to the front of the node.
           newNode->setNext(head);
           head=newNode;

       }
      
      
       void Remove()
       {
          
           if(head==NULL)
           {
               cout<<"\nNo element in the list\n";
           }
           else
           {
               if(head->getNext()==NULL)
               {
                   delete(head);
                   head=NULL;
               }
               else
               {
                   Node *trev=head;
                   while(trev->getNext()->getNext()!=NULL)//Traverse till the second last node
                   {
                       trev=trev->getNext();
                   }

//Rempove the right neighbour of the second last node, which is the last node
                   Node *temp=trev->getNext();
                   trev->setNext(NULL);
                   delete(temp);
               }
           }
       }
      
       //Print
       void Display()
       {
           if(head==NULL)
           {
               cout<<"\nNo element in the list\n";
           }
           else
           {
               cout<<"\n\t\tEmployee Data\n";
               Print(head);//call assistance function Print
           }
       }
       void Print(Node *node)
       {
           if(node!=NULL)//print the Employee data if given node is not null
           {
               cout<<"\n\t Employee Number="<<node->getNumber()<<" Employee year:"<<node->getYear()<<" Employee number of dependencies="<<node->getDependencies()<<endl;
               Print(node->getNext());//call Recursive function by bassing next node
           }
          
       }
       //Reverse
       void Clear(Node *node)
       {
          
           if(node!=NULL)//call the recursive function Clear till given node is not NULL
           {
               cout<<"\n\t\tDeleting....\n";
               Clear(node->getNext());
               delete(node);//delete the current provided node
           }
           head=NULL;//make head as NULL
       }

//getter for head
       Node *getHead()
       {
           return head;
       }
};
int main()
{
   Employees lst;
   int ch=-1;
   string no;
   int year_;
   unsigned short int dep;
   while(ch!=0)//print menu till user enter 0 which is the exut state
   {
       cout<<"1. Insert\n";
       cout<<"2. Remove\n";
       cout<<"3. Display\n";
       cout<<"4. Clear\n";
       cout<<"0. Exit\n";
       cout<<"\nPlease choose a menu option:\n";
       cin>>ch;
       switch(ch)
       {
           case 1:
//Get data from user to pass the data to Insert function
               cout<<"\nEnter Employee number:";
               cin>>no;
               cout<<"\nEnter year:";
               cin>>year_;
               cout<<"\nEnter Number of Dependicies:";
               cin>>dep;
              
               lst.Insert(no,year_,dep);
               break;
           case 2:
               lst.Remove();
               break;
           case 3:
               lst.Display();
               break;
           case 4:
               lst.Clear(lst.getHead());
               break;
           default:
               if(ch!=0)//if user does not enter 0 then other choices are invalid. So priunt error message
               {
                   cout<<"\nInvalid menu option.\n";
               }
               break;
       }
   }
   return 0;
}


Related Solutions

Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
USING IDLE PYTHON: A greengrocer would like to maintain a linked lists about his products. For...
USING IDLE PYTHON: A greengrocer would like to maintain a linked lists about his products. For each product he saves it name, price and stock amount. Write a program that creates an empty linked list and then prompts to user to do one of the following: 1. Add a product to the list (anywhere) 2. Print all products in the LinkedList 3. Print all products above a certain price 4. Print all low-stock products ( Less than 20 pounds) 5....
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID,...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID, name and salary as data of each employee. Search a particular record on ID and display the previous and next records as well. Whichever ID it give, it should display all the records because of being circular. Code needed in Java.
Learning Objectives: list several objectives that address management knowledge and skill targets…what you would like to...
Learning Objectives: list several objectives that address management knowledge and skill targets…what you would like to know and would like to do as a result of any internship experience? What activities, tasks, project work will you be engaged in that will facilitate reaching your objectives? How will you measure progress toward these goals? How will you measure whether you have achieved these goals at the end of your internship?
E13-19 Identifying advantages and disadvantages of a corporation Learning Objective 1 Following is a list of...
E13-19 Identifying advantages and disadvantages of a corporation Learning Objective 1 Following is a list of advantages and disadvantages of the corporate form of business. Identify each quality as either an advantage or a disadvantage. a. Ownership and management are separated. b. Entity has continuous life. c. Transfer of ownership is easy. d. Stockholders’ liability is limited. e. Exposure to double taxation is evident. f. Entity can raise more money than a partnership or sole proprietorship. g. Government regulation is...
Would federal law be violated if a union would like its employer to require workers to...
Would federal law be violated if a union would like its employer to require workers to join the union after a specified amount of time on the job although the employer does not require its new hires to join the union as a requirement for getting hired? If an employer has 7,000 workers in 7 locations, do its employees have the right to organize?
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic:...
IN C++. Objective: Create a Singly linked list of numbers based upon user input. Program logic: Ask for a number, add that number to the front of the list, print the list. Repeat until they enter -1 for the number. . Sample Input: 10, 15, 5, 2, 4, -1 Output: 4, 2, 5, 15, 10. Next sort all the numbers using selection sort and display them. Next give the user option to search for a specific number in the list....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT