San Jose City College Void Function List of Integers and Interchanges Data HW

User Generated

irpgbeureb123

Programming

Description

Description

Write a void function that takes a linked list of integers and interchanges data in the first node with that in the last node. The function will take a pointer to the head of the list. After the function is called, it will retrieve data stored in it, then find and retrieve data stored in the last node. The function will then interchange the data of the first node with that of the last node.

Note that your function will neither create nor destroy any nodes. It will simply interchange the data stored between the head and tail nodes. Place your function in a suitable test program.

** Please answer all three questions in the Problems Encountered & More section

Given

You are given the following file:

main.cpp

What to Submit

Submit for this lab:

  1. main.cpp

Requirements

  1. Implement the exchange_data function in main.cpp
  2. You may add more functions that are needed to implement the above
  3. Your code will not cause any runtime errors such as segmentation faults
  4. Answer all three questions in the Problems Encountered & More section

Tips

  1. Use visual aids to help you understand your plan for the function
  2. Consider boundary cases, such as:
    1. When the list is empty
    2. When the list has only one node
  3. You might want to "rewrite" or "copy/type out" other functions that are already implemented to solidify your understanding with linked list first

Test Cases

Suggestion: Change main.cpp to add more ways to test your reverse function implementation.

Demo

Just finished adding nodes
Current linked list:7 5 4 8 9
Just finished exchanging data
Current linked list:9 5 4 8 7

Problems Encountered & More

In the end of your main.cpp file, add a blocked comment section as follows and fill it out:

/* ==========================================================

Q1. Problems I had working on this assignment (and how I overcame these problems)
1.
2.
Q2. When implementing exchange_data, I had passed in head as a parameter:
// your answer: by value or reference
Q3. The reason being:
// your answer: explain your choice
========================================================== */

Unformatted Attachment Preview

Page of 3 ZOOM // File: main.cpp // Lab: 13-1 lab exchange data function. See lab description for more details // Purpose: Test whether the exchange data function described below works // Your task: Define and implement the following func tion // Exchange data function: // Write a void function that takes a linked list o f integers and // interchanges data in the first node with that in t he last node. // The function will take a pointer to the head of t he list. After the // function is called, it will retrieve data stored in it, then find and // retrieve data stored in the last node. The functi on will then interchange // the data of the first node with that of the last node. // Note that your function will neither create nor d estroy any nodes. It will // simply interchange the data stored between the he ad and tail nodes. Place // your function in a suitable test program. // Author: Monza Lui // Date: Nov 26, 2019 #include using namespace std; struct Node { // any type of data int data; Node *next; }; typedef Node* NodePointer; // Purpose: insert a new node at the head of list, with data = data // Precondition: head is pointing to an existing li nked list void head_insert(NodePointer& head, int new_data); // Purpose: remove all the nodes void delete_nodes(NodePointer& head); // Purpose: remove nodes with data = data, returns true if successful void print_nodes(NodePointer head); int main() { NodePointer head = nullptr; head_insert(head, 9); head_insert(head, 8); head_insert(head, 4); head_insert(head, 5); head_insert(head, 7); cout n ext) while (iter != nullptr) { iter2 = iter->next; // iter pointing to the node that we are deleting // iter2 pointing to the node after the one iter is pointing to cout data
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

At...


Anonymous
I was having a hard time with this subject, and this was a great help.

Studypool
4.7
Indeed
4.5
Sitejabber
4.4

Related Tags