13.9 Lab 13: Linked Lists Step 1: (2 Point) Write a search function called linkedlistSearch() that takes a number, trave
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
13.9 Lab 13: Linked Lists Step 1: (2 Point) Write a search function called linkedlistSearch() that takes a number, trave
starter code:
#include <iostream>using namespace std;
class Node { public: int data; Node *pNext;};
void displayNumberValues( Node *pHead){ while( pHead != NULL) { cout << pHead->data << ""; pHead = pHead->pNext; } cout << endl;}
//Option 1: Search the list// TODO: complete the function below to search for a given value inlinked lsit// return true if value exists in the list, return falseotherwise.
?? linkedlistSearch( ???){
}
//Option 2: get sum of all values// TODO: complete the function below to return the sum of allelements in the linked list.
??? getSumOfAllNumbers( ???){
}
int main(){ int userInput; Node *pHead = NULL; Node *pTemp; cout<<"Enter list numbers separated by space,followed by -1: "; cin >> userInput; // Keep looping until end of input flag of -1 isgiven while( userInput != -1) { // Store this number on the list pTemp = new Node; pTemp->data = userInput; pTemp->pNext = pHead; pHead = pTemp; cin >> userInput; } cout <<" Menu options:\n"; cout <<" 1. Search for a value. \n" <<" 2. Display the sum of allnumbers on the list. \n" <<" 3. Exit program. \n"; cin >> userInput; cout<<"Your choice ->" << userInput<<endl; switch(userInput){ case(1): int num; cout<<"Enter a value toinitiate a search:"<<endl; cin >> num; //Call search functionhere //Display results break; case(2): // Call getSumOfAllNumbershere //Display results break; case(3): exit(0); } return 0;}
13.9 Lab 13: Linked Lists Step 1: (2 Point) Write a search function called linkedlistSearch() that takes a number, traverse through a linked list and return true if the number exists it the list and false otherwise. Running the code should look like: Enter list numbers separated by space, followed by -1: 3 5 7 -1 Menu options: 1. Search for a value. 2. Display the sum of all numbers on the list. 3. Exit program. Your choice -> 1 Enter a value to initiate a search: 4 The number you entered does not exist in this list. Another Example: Enter list numbers separated by space, followed by -1: 3 5 7 -1 Menu options: 1. Search for a alue. 2. Display the sum of all numbers on the list. 3. Exit program. Your choice -> 1 Enter a value to initiate a search: 3 The number you entered exists in this list. Step 2: (1 Point) Write a function called getSumOfAllNumbers(.....) that returns the sum of all numbers in the linked list. Running the code should look like: Enter list numbers separated by space, followed by -1: 3 5 7 -1 Menu options: 1. Search for a value. 2. Display the sum of all numbers on the list. 3. Exit program. Your choice -> 2 The sum of all numbers on the list is: 15