Question

Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

Working in C++, Complete the code.

// Lab 9 code

/* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */

/* Insert code as described by the comments. */

/* Add comments to each line of code that uses a pointer, describing how it is being used. */

#include <iostream>

using namespace std;

struct Node

{

int data; //the data part of the Node.

Node *next; //Pointer to the next node.

};

Node *head = NULL;

int getData()

{

int data;

cout << "Input an integer as data: ";

cin >> data;

return data;

}

void addData(Node *targetNode, int data)

{

/* Add one line of code here that adds data to the linked list */

}

void createNewNode(Node *prevNode)

{

Node *newNode = new Node;

addData(newNode, getData()); //storing data to the new node.

/* Add two lines of code that properly set the pointers for the new and the previous node. */

}

void createList(int total)

{

Node *Current = head = new Node;

addData(Current, getData());

/* Add a loop that create the required number of nodes (based on the parameter “total”) for the linked list using the function createNewNode.*/

}

void showList()

{

Node *Current = head;

cout << "Showing the linked list:" << endl;

if(Current == NULL)

{

cout << "Empty list!" << endl;

return;

}

while(Current != NULL)

{

cout << Current -> data << " ";

Current = Current -> next; //Now, Current pointer is pointing it next Node.

}

cout << endl;

}

void selectionSort()

/* Complete the code to execute s selection sort using pointers to the linked list. */

{

Node *selectedNode = head;

while(selectedNode -> next != NULL)

{

Node *currentNode = selectedNode;

Node *minimumNode = currentNode;

int minimumData = currentNode -> data;

/* Write code that traverses the linked list and compares the data of the nodes to find the minimum and appropriately set “minimumData” and “minimumNode”. */

/* Write code that sets up the sorted list by setting the data for “selectedNode” to the value of the minimum node, appropriately resetting the minimum node, and moving to the next “selected node” to continue the sort.”

int main()

{

createList(5);

showList();

selectionSort();

showList();

return 0;

}

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

// Lab 9 code

/* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */

/* Insert code as described by the comments. */

/* Add comments to each line of code that uses a pointer, describing how it is being used. */

#include <iostream>

using namespace std;

struct Node

{

int data; //the data part of the Node.

Node *next; //Pointer to the next node.

};

Node *head = NULL;

int getData()

{

int data;

cout << "Input an integer as data: ";

cin >> data;

return data;

}

void addData(Node *targetNode, int data)

{

targetNode -> data = data;

}

void createNewNode(Node *prevNode)

{

Node *newNode = new Node;

addData(newNode, getData()); //storing data to the new node.

newNode -> next = NULL;
prevNode -> next = newNode;

}

void createList(int total)

{

Node *Current = head = new Node;

addData(Current, getData());

for(int i = 1; i < total; i++){
createNewNode(Current);
Current = Current -> next;
}

}

void showList()

{

Node *Current = head;

cout << "Showing the linked list:" << endl;

if(Current == NULL)

{

cout << "Empty list!" << endl;

return;

}

while(Current != NULL)

{

cout << Current -> data << " ";

Current = Current -> next; //Now, Current pointer is pointing it next Node.

}

cout << endl;

}

void selectionSort()

/* Complete the code to execute s selection sort using pointers to the linked list. */

{

Node *selectedNode = head;

while(selectedNode -> next != NULL)

{

Node *currentNode = selectedNode;

Node *minimumNode = currentNode;

int minimumData = currentNode -> data;


/* Write code that traverses the linked list and compares the data of the nodes to find the minimum and appropriately set “minimumData” and “minimumNode”. */
while(currentNode != NULL)
{
if(minimumData > currentNode -> data)
{
minimumData = currentNode -> data;
minimumNode = currentNode;
}

currentNode = currentNode -> next;
}
int tempSwap = minimumNode -> data;
minimumNode -> data = selectedNode -> data;
selectedNode -> data = tempSwap;
selectedNode = selectedNode -> next;
}
}
/* Write code that sets up the sorted list by setting the data for “selectedNode” to the value of the minimum node, appropriately resetting the minimum node, and moving to the next “selected node” to continue the sort.”*/

int main()

{

createList(5);

showList();

selectionSort();

showList();

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...
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
  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying to...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • How would you get this lab to work with the numbers 37 14 68 47, the...

    How would you get this lab to work with the numbers 37 14 68 47, the book we are using is Data structures using C++ by D.S. Malik. Please I need help? Just keeps repeating the same question. Code: #include <iostream> #include <cstdlib> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm ...

    Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm We have seen in class how selection sort algorithm works on arrays data structure. In this lab we will practice how selection sort can be performed on a linked list ADT 1. Convert the following selection sort pseudo-code to perform the sort in ascending order (selectionsort asc function) a. Find the node with the minimum value in the linked list of length rn b. Append...

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