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
  • 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;...

  • 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...

  • 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...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • 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...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /*...

    In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /* Name: Rabia Saad Al-wethnani ID: 43503535 Section: 2486 */ using namespace std; struct Node { int item; //data Node *next; //pointer to the next node in the list }; Node* createNode(int); int isPresent(Node*, int); void appendNode(Node*&, int); void displayList(Node*); void printLists(Node*, Node*); void swapNodes(Node* &head, int s); void findIntersection(Node* first, Node* P); void findUnion(Node* first, Node* P); int main() { Node* L = 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