1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
storchak [24]
3 years ago
8

The LList class is (mostly) the same one discussed in lecture, but you must write a sort() function that uses one of the three i

nefficient sorts discussed in lecture. The function should sort the list contents from lowest to highest value using bubble sort, selection sort, or insertion sort. Since the original linked list code produced a sorted list, this version has been modified such that (1) insert() adds each new item to the start of the list, not in order and (2) remove() searches the entire list, since it can't assume the list is ordered. Also, display() has been modified to print all list contents on a single line, not one value per line.
The only file you may modify is LList.cpp, which contains space to write the definition for the sort() function, as well as the definitions of standard LList functions covered previously.
The other two files, LList.h and LList_test_main.cpp, are read-only. LList.h contains the LList class definition, while the other file exists solely to test the code you write.
LList.h
#ifndef LLIST_H
#define LLIST_H
#include
using std::ostream;
class LList {
public:
/***** FUNCTION TO BE WRITTEN *****/
void sort(); // Sort linked list using bubble, insertion, or selection sort
/***** FUNCTIONS BELOW THIS LINE ARE ALREADY WRITTEN--DO NOT MODIFY *****/
LList(); // Default constructor
~LList(); // Destructor
bool empty(); // True if list is empty
void insert(int v); // Add new value to list
void remove(int v); // Remove node with v
void display(ostream &out); // Print contents of list
// (could of course write as
// overloaded operator)
private:
/****************************************************************
Slightly different setup than we've seen before, as Node class
is defined *inside* LList class.
Benefits:
By making "Node" a member of LList, LList functions can
directly refer to Node data members val and next. No
need to write/call accessor functions for Node, so
fewer files in solution + no function call overhead on
simple data accesses. Node data members are "public" to
LList functions but "private" to outside world.
Downside:
This implementation of Node can *only* be used inside LList.
If you want Node objects in other classes, have to redefine.
****************************************************************/
class Node {
public:
int val; // Value in each node--could be generalized
// using templates
Node *next; // Pointer to next node
};
Node *first; // Pointer to first node
};
#endif
LList.cpp
#include "LList.h"
using namespace std;
/***** FUNCTION TO BE WRITTEN *****/
void LList::sort() { // Sort linked list using bubble, insertion, or selection sort
/***** ADD YOUR OWN SOLUTION *****/
}
/***** FUNCTIONS BELOW THIS LINE ARE ALREADY WRITTEN--DO NOT MODIFY *****/
// Default constructor
LList::LList() : first(NULL)
{}
// Destructor
LList::~LList() {
Node *temp;
while (first != NULL) {
temp = first;
first = first->next;
delete temp;
}
}
// True if list is empty
bool LList::empty() {
return (first == NULL);
}
// Add new value to list
/***** MODIFIED TO CREATE UNORDERED LIST *****/
void LList::insert(int v) {
// Allocate new node and place at beginning of list
Node* newNode = new Node;
newNode->val = v;
newNode->next = first;
first = newNode;
}
// Remove node with v
void LList::remove(int v) {
Node *prev; // Predecessor of node to be deleted
Node *cur; // Node to be deleted
// Find node, if it's in list
cur = first;
prev = NULL;
while (cur != NULL) { /***** MODIFIED--NO LONGER ASSUMES LIST ORDERED *****/
prev = cur;
cur = cur->next;
}
// Didn't find node
if (cur == NULL || cur->val > v) {
cout << "Node with value " << v << " not found\n";
return;
}
// Otherwise, remove node
if (prev == NULL) // Special case for first node
first = cur->next;
else
prev->next = cur->next;
delete cur;
}
// Display contents of list
void LList::display(ostream &out) {
Node *ptr = first;
while (ptr != NULL) {
out << ptr->val << ' ';
ptr = ptr->next;
}
out << '\n';
}
LList_test_main.cpp
#include "LList.h"
#include
#include
using namespace std;
int main() {
LList L1, L2; // Linked lists to use for testing
unsigned i; // Loop indexes
int seed; // RNG seed
// RNG seed--ensures you'll get same "random" values across
// multiple runs with same seed
cout << "Seed value: ";
cin >> seed;
srand(seed);
// Fill lists
for (i = 0; i < 10; i++) {
L1.insert(rand() % 50);
L2.insert(rand() % 50);
}
// Print lists before sorting
cout << "Before sorting:\nL1: ";
L1.display(cout);
cout << "\nL2: ";
L2.display(cout);
// Sort and print lists again
L1.sort();
L2.sort();
cout << "After sorting:\nL1: ";
L1.display(cout);
cout << "\nL2: ";
L2.display(cout);
return 0;
}
Computers and Technology
1 answer:
Vladimir79 [104]3 years ago
8 0

Answer:

see explaination

Explanation:

#include <iostream>

#include <string>

using namespace std;

class LinkedList{

class Node{

public :

int data;

Node* next;

Node(int data){

this->data = data;

next = NULL;

}

};

public :

Node *head;

LinkedList(){

this->head = NULL;

}

void insert(int d){

Node* new_node = new Node(d);

new_node->next = head;

head = new_node;

}

// sort the list with selection sort algorithm.

// Pick the smallest element in the unsorted array and place in the first element in the unsorted.

void sort_list(){

if (head == NULL){

return;

}

Node* current = head;

while (current->next != NULL){

Node* min_node = current;

Node* traverse = current->next;

while(traverse != NULL){

if(traverse->data < min_node->data){

min_node = traverse;

}

traverse = traverse->next;

}

int temp = current->data;

current->data = min_node->data;

min_node->data = temp;

current = current->next;

}

}

void print_list(){

Node* current = head;

while(current !=NULL){

cout<<current->data<<" ";

current = current->next;

}

cout<<"\n";

}

};

int main(){

LinkedList ll;

for(int i=0;i<10;i++){

ll.insert(i);

}

ll.print_list();

cout<<"*******************************************\n";

ll.sort_list();

ll.print_list();

cout<<"*******************************************\n";

}

You might be interested in
Technician A says that volatility describes how hard it is for gasoline to evaporate. Technician B says that Reid vapor pressure
Lubov Fominskaja [6]

I’d say both techies are correct.

Volatility, as applied in gasoline, is quantified by the tendency of the liquid to change to vapor at any given temperature (vaporize). This rate of change might be hard or easy, this depends on the temperature or the pressure. Technician B is also correct. RVP is defined and determined experimentally according to the ratio of the vapor volume to the liquid volume at 100 °F.

8 0
2 years ago
Amy uses digital devices to listen to music and to watch movies. Which one of these is Mbps a measure of?
posledela

Answer:

Storage Size

Explanation:

the answer to which is better 8 Mbps or 2 MBps? Is 2 MBps (which is 16 Mbps). Using a less common, but more clear notation: which is better 8 Mbit/s or 2 MBps? Answer: 2 MBps, since that is 16 Mbit/s. (Marketing people use this confusion to their advantage if you're not sure which is intended, ask.)

Source http://cs.sru.edu/~mullins/cpsc100book/module02_introduction/module02-05_introduction.html

(if you want to read the full artical.)

8 0
2 years ago
Write a Python 3 program that prompts the user for 3 postive numbers (or zero) and then adds them together. If the user enters a
Citrus2011 [14]

Answer:

def prompt_number():

   while True:

       number = int(input("Enter a number: "))

       if number >= 0:

           break

       

   return number

   

def compute_sum(n1, n2, n3):

   total = n1 + n2 + n3

   return total

   

n1 = prompt_number()

n2 = prompt_number()

n3 = prompt_number()

result = compute_sum(n1, n2, n3)

print(result)

Explanation:

Create a function named prompt_number that asks the user to enter a number until a positive number or 0 is entered and returns the number

Create a function named compute_sum that takes three numbers, sums them and returns the sum

Ask the user to enter three numbers, call the prompt_number() three times and assign the values

Calculate the the sum, call the compute_sum and pass the numbers as parameters

Print the result

6 0
2 years ago
____ is the study of how computers can potentially improve without human programming.
Advocard [28]

Answer:

Machine Learning and/or Artificial Intelligence (AI)

5 0
1 year ago
What security principle does a firewall implement with traffic when it does not have a rule that explicitly defines an action fo
NemiM [27]

Answer:

Implicit deny

Explanation:

A firewall is a network security protocol that monitors and controls inbound and outbound traffic based on set aside security rules.

When a firewall encounters a traffic it does not have a rule that explicitly defines an action for that communication, it implicit deny.

7 0
3 years ago
Other questions:
  • What would you use to compare two date ranges in a report?
    9·1 answer
  • What weight pencil is recommended for darkening lines and for lettering? *
    7·2 answers
  • Transmits data as pulses of light through tiny tubes of glass Uses standard telephone lines to create a high-speed connection Co
    5·1 answer
  • Write java code that displays all the objects in a stack in the order in which they were pushed onto it. after all the objects a
    5·1 answer
  • (1) Prompt the user to input an integer between 32 and 126, a float, a character, and a string, storing each into separate varia
    7·1 answer
  • Ryan is working on a document with many secotions. For each section,he wantes to have the title bolded,underlined,an blue. Which
    9·2 answers
  • Which of the following is true about advertisements?
    15·1 answer
  • Discuss five domains of Instructional technology​
    7·1 answer
  • A non-profit organization decides to use an accounting software solution designed for non-profits. The solution is hosted on a c
    11·1 answer
  • You have received an update package for a critical application you are running on a virtual machine. You have been told to insta
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!