Question

Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp.

file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; };

class List {

public: List(); //Create an empty list

bool Empty(); // return true if the list is empty, otherwise return false

void InsertAtEnd(ElementType x); //insert a value x on the end of the list

void Delete(ElementType x); //if value x is in the list, remove x

void Display(); //Display the data values in the list in the order inserted​

int Sum(); // Compute and return the sum of the values in the list

int Average(); // Compute and return the average of the values in the list

private:

node * first;

//think of other data members that can potentially make your life easier:

//node* last?

//count ?

}

Your client code should be menu driven using the following menu options:

  1. Insert
  2. Delete
  3. Display
  4. Sum
  5. Average
  6. Exit

Option 1: Insert a new value on the end of the list.

Option 2: Delete a number specified by the user from the list, if the number is in the list.

Option 3: Displayed in the list of numbers.

Option 4: Compute and return the sum of the values in the list. If the list is empty return 0.

Option 5: Compute and return the average of the values in the list. If the list is empty return 0.

Option 6: Exits the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

List.h


//file: List.h
typedef int ElementType;
const int MAX = 100;
class List
{
public:
List(); //Create an empty list
bool Empty(); // return true if the list is empty, otherwise return false
void InsertAtEnd(ElementType x); //insert a value x on the end of the list
void Delete(ElementType x); //if value x is in the list, remove x
void Display(); //Display the data values in the list in the order inserted
int Sum(); // Compute and return the sum of the values in the list
int Average(); // Compute and return the average of the values in the list
  
private:
int N; //number of values in list
ElementType listArray[MAX]; //the array to hold the list values
};

main.cpp

#include <iostream>
#include "List.h"

int main() {
  
List myList;
  
std::cout<<"Main Menu \n 1. Insert \n 2. Delete \n 3.Display \n 4. Sum \n 5. Average \n 6. Exit \n";
std::cout<<"Enter the number of the menu item you would like to select \n";
  
int menuSelection;
  
std::cin>>menuSelection;
  
ElementType inValue;
  
  
if (menuSelection == 1) {
std::cout<<"\nEnter the value you would like to add: \n";
  
std::cin>>inValue;
myList.InsertAtEnd(inValue);
}else if (menuSelection == 2){
std::cout<<"\nEnter the value you would like to delete: \n";
  
ElementType delValue;
std::cin>>delValue;
  
myList.Delete(delValue);
}else if (menuSelection == 3){
myList.Display();
}else if (menuSelection == 4){
myList.Sum();
}else if (menuSelection == 5){
myList.Average();
}else{
exit(0);
}
  
}

List.cpp


#include <iostream>
#include "List.h"

List::List(){
ElementType listArray[MAX];
N = 0;
}

bool List::Empty(){
if (N == 0){
return true;
}else{
return false;
}
}

void List::InsertAtEnd(ElementType x){
listArray[N] = x;
}

void List::Delete(ElementType x){
//TODO create delete method
//if value x is in the list, remove x
for(int i = 0; i < MAX; i++){
if(listArray[i] == x){
listArray[i] = 0;
N--;
}
}
}

void List::Display(){
for (int i = 0; i < MAX; i++) {
std::string s = std::to_string(listArray[i]);
std::cout<<"\n" + s;
}
}

int List::Sum(){
int sum = 0;
for(int i = 0; i < MAX; i++){
sum += listArray[i];
}
return sum;
}

int List::Average(){
int sum = 0;
for(int i = 0; i < MAX; i++){
sum += listArray[i];
}
return sum/N;
}

Add a comment
Know the answer?
Add Answer to:
Using the following definition (List.h file) for a list, implement the member functions (methods) for the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due...

    Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due Friday, February 9th, 2017 @ 11:59PM EST Directions Create a List object. Using the following definition (List.h file is also in the repository for your convenience) for a list, implement the member functions (methods) for the List class and store the implementation in a file called List.cpp. Use an array to implement the list. Write the client code (the main method and other non-class...

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • Using C++ • Implement the following in both string linked list and in the template version...

    Using C++ • Implement the following in both string linked list and in the template version of the class: • Add to the back and remove from back of the list : - void addBack(….); - string removeBack(); E removeBack(…); • Insert elements in a sorted list i.e., find the position where an element fits and add it to the list. Note you must call the addBack and addFront functions if you insert in the front or rare of the...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT