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....
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
Hi I would like to see an example in c++ of Stack As Linked List I...
Hi I would like to see an example in c++ of Stack As Linked List I need a seek() function which receives a double number X and which returns in which position in the stack is X. If the value X is not in the stack, the function should return -1
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
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 =...
Develop a program to maintain a Linked List of homework assignments name and due date. When...
Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method: Add a new assignment. Remove an assignment. Provide a list of the assignments in the order they were assigned. Find the assignment(s) with the earliest due date...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT