Question

Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help. list.h:...

Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help.

list.h:

//////////////////////////////////////////////////////////////////////////
#ifndef LIST_H
#define LIST_H
//////////////////////////////////////////////////////////////////////////

namespace CS170
{
struct node
{
int value;
node *next;
};

class list
{
public:
// Constructor for list. Creates an empty list
list();

/* Destructor for list.
Empty the list and release the allocated memory
*/
~list();

// Prints out the values contained in the list
void print_list() const;

// Returns the current size of the list
unsigned size() const;

// Returns true if list is empty, false otherwise
bool empty() const;
  
// Frees (deletes) all of the nodes in the list
void clear();

/* Creates a node with val and
add it to the front of the list */
void push_front(int val);

// Return the first node in the list
node *front();

/* Removes nodes at position pos.
Position count starts from zero.
*/
void erase(int pos);

/* Removes nodes from position first to position last-1.
Position count starts from zero.
*/
void erase(int first, int last);

/* Resizes the list to contain n elements.
If n is smaller than the current size, then keep only
the first n elements, then destroy those beyond.
If n is larger than the current size, the new elements
are initialized as val.
*/
void resize(int n, int val = 0);

// Sorts the list ascendingly
void sort();
  
/* Assume the current list and l2 are both sorted ascendingly,
this function merges them into one, so that the elements
are still in ascending order.
The current list will store the merged elements,
while l2 will become empty.
*/
void merge(list &l2);

private:
unsigned list_size;
node *the_list;

node *make_node(int val);
};
}
#endif // LIST_H

main.cpp:

#include <fstream>
#include <iostream>
#include "list.h"

CS170::list& read_list_push_front(const char *filename,CS170::list &l)
{
int value;
std::ifstream fin(filename);
if (!fin.is_open())
std::cout <<filename<< " is not found!" << std::endl;
else
while (!fin.eof())
{
fin >> value;
l.push_front(value);
}

return l;
}

int main(int argc, char *argv[])
{
if(argc<2)
{   
std::cerr<<"Usage: "<<argv[0]<<" <filename>\n";
return 1;
}
CS170::list l1, l2;

l1 = read_list_push_front(argv[1],l1);
std::cout << "l1:\n";
l1.print_list();
  
std::cout << "\nErase l1 at 100:\n";
l1.erase(100);
l1.print_list();
  
std::cout << "\nErase l1 at 200:\n";
l1.erase(200);
l1.print_list();

std::cout << "\nErase l1 from 100 to before 200:\n";
l1.erase(100, 200);
l1.print_list();

std::cout << "\nResize l1 to 500 elements, fill with 100:\n";
l1.resize(500, 100);
l1.print_list();

std::cout << "\nResize l1 to 200 elements:\n";
l1.resize(200);
l1.print_list();
  
std::cout << "\nSort l1:\n";
l1.sort();
l1.print_list();
  
std::cout << "\nl2:\n";
l2 = read_list_push_front(argv[1],l2);
l2.print_list();
  
std::cout << "\nSort l2:\n";
l2.sort();
l2.print_list();
  
l1.merge(l2);
std::cout << "\nl1 after merging:\n";
l1.print_list();
std::cout << "\nl2 after merging:\n";
l2.print_list();
  
l1.clear();
l2.clear();
  
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help. list.h:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove...

    C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. (i.e. n is greater than 0) Follow up: Could you do this in one pass? Hint: Maintain two pointers and update one with...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

    Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

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