Question
Objectives: Having more practice working with and understanding linked lists. Implementing a function to search and insert nodes throughout a singly linked list. Implementing a
Objectives:
Having more practice working with and understanding linked lists.
Implementing a function to search and insert nodes throughout a singly linked list.
Implementing a function to delete a node at the and of a singly linked list.
Implementing functions to sort items and count occurrences of items in a singly linked list.
Question: A incomplete C++ code was provided. Your test is to complete this code. Remember only fill out the missing part noted in comments and don't change other existing code. An input text file is also provided on which is used to populate the linked list with its original values. Much of the structure is given for a menu-driven C++ program to implement and perform operations on a singly linked list.
1. Read and understand the given code.
2. Understand each functions purpose. Take note of the parameter types and return types.
3. Implement the following functions:
a. bool isEmpty(node *current_head)
b. void insertPos(node *current, int val, int toSearch)
c. void printList(node *current)
d. void deleteLast(node *current, node *trailCurrent)
e. void deleteAll(node *current)
f. int frequency(node *current, int toSearch)
g. void sort(node *head)
4. Test your implementation. Ensure each menu choice works correctly. Handling invalid input is not required.
5. Submit a single completed .cpp file.
Incomplete Linklist code:
#include#include using namespace std; struct node { int data; node *next; }; node* initializeList(node*, int); void showMenu(); void insertPos(node*, int, int); void deleteLast(node*, node*); void deleteAll(node*); int frequency(node*, int); void sort(node*); void printList(node*); bool isEmpty(node*); // Read and understand the main function. No coding is // required in main, but please write your functions to // conform to the function calls originating in main. int main() { int input; int val; int toSearch; ifstream inData; inData.open("input.txt"); node *head = NULL; while (inData >> val) head = initializeList(head, val); inData.close(); while (true) { showMenu(); cin >> input; switch (input) { case 1: if (!isEmpty(head)) { cout > val; cout > toSearch; insertPos(head, val, toSearch); } else cout > toSearch; cout data = val; newNode->next = NULL; if (current == NULL) { current = newNode; return current; } while (current->next != NULL) current = current->next; current->next = newNode; return head; } // The showMenu function is also complete. No coding is required here. void showMenu() { cout data. // Consider these temporary pointers for traversal. node *temp1 = new node; node *temp2 = new node; // And somewhere to hold the value to be swapped. int tempValue = 0; // TODO: Complete this sort function here using any sort // algorithm. // And don't forget to delete dynamically allocated nodes. delete temp1; delete temp2; }
Content in Input.txt:
10 20 30 40 50 15 25 35 45 55
Sample Output:
C:Users jlatelSource ReposLab22Debug>Lab22 1. Insert a number at a particular position. 2. Delete the last element. 3. Count a particular value 4. Sort the numbers. 5. Print list. 6. Quit Please enter a choice: 5 10 20 30 40 50 15 25 35 45 55 1. Insert a number at a particular position. 2. Delete the last element. 3. Count a particular value 4. Sort the numbers. 5. Print list. 6. Quit Please enter a choice: 1 Please enter a value to insert: 5 Please enter a current list value after which the new value will be inserted: 20 Number inserted. 1. Insert a number at a particular position. 2. Delete the last element. 3. Count a particular value 4. Sort the numbers. 5. Print list
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started