Question

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 std::cout;
using std::endl;
using std::string;

int main() {
list li;

int n;
cin >> n;

for (int i = 0, data, pos; i < n; i++) {
cin >> pos >> data;
li.insert(pos, data);
}

cout << li.toString() << " size: " << li.size() << endl;

list li2(li);
list li3;

li = li3 = li2 = li;

cout << li.toString() << " size: " << li.size() << endl;
cout << li2.toString() << " size: " << li2.size() << endl;
cout << li3.toString() << " size: " << li3.size() << endl;

int m;
cin >> m;

for (int i = 0, pos; i < m; i++) {
cin >> pos;
li.erase(pos);
}

cout << li.toString() << endl;

cout << li.sort().toString() << endl;
cout << li2.sort().toString() << endl;
cout << li3.sort().toString() << endl;

return 0;
}
//SimpleList.hpp

#ifndef LIST
#define LIST

#include <string>
#include <iostream>

typedef struct node {
int data;
struct node* next;
node(int data = 0, struct node* next = NULL) : data(data), next(next) {}
} node;

class list {
private:
node* head;
int _size;

public:
list();
list(const list&);
list& operator=(const list&);
~list();

// Capacity
bool empty(void) const;
int size(void) const;

public:
// output
// list: [1,2,3,4,5]
// output: 1->2->3->4->5->NULL
std::string toString(void) const;

void insert(int position, const int& data);
void erase(int position);
void clear(void) {
if (this->head != NULL) {
node* p = this->head;
while (p != NULL) {
node* temp = p;
p = p->next;
delete temp;
}
this->head = NULL;
}
this->_size = 0;
}
list& sort(void);
};

#endif

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

// SimpleList.hpp

#ifndef LIST
#define LIST

#include <string>

#include <iostream>

typedef struct node {

int data;

struct node* next;

node(int data = 0, struct node* next = NULL) : data(data), next(next) {}

} node;

class list {

private:

node* head;

int _size;

public:

list()

{

               head=NULL;

               _size=0;

}

list(const list&l)

{

               head =NULL;

                              _size = l._size;

                              node *curr_l = l.head;

                              node *curr = NULL;

                              while(curr_l != NULL)

                              {

                                             if(head == NULL)

                                             {

                                                            head = new node;

                                                            head->data = curr_l->data;

                                                            head->next = NULL;

                                                            curr = head;

                                             }else

                                             {

                                                            node *nextNode = new node;

                                                            nextNode->data = curr_l->data;

                                                            nextNode->next = NULL;

                                                            curr->next = nextNode;

                                                            curr = nextNode;

                                             }

                                             curr_l = curr_l->next;

                              }

}

list& operator=(const list&l)

{

               if(this != &l)

                              {

                                             while(head != NULL)

                                             {

                                                            node *temp = head;

                                                            head = head->next;

                                                            delete(temp);

                                             }

                                             _size = l._size;

                                             node *curr_l = l.head;

                                             node *curr = NULL;

                                             while(curr_l != NULL)

                                             {

                                                            if(head == NULL)

                                                            {

                                                                           head = new node;

                                                                           head->data = curr_l->data;

                                                                           head->next = NULL;

                                                                           curr = head;

                                                            }else

                                                            {

                                                                           node *nextNode = new node;

                                                                           nextNode->data = curr_l->data;

                                                                           nextNode->next = NULL;

                                                                           curr->next = nextNode;

                                                                           curr = nextNode;

                                                            }

                                                            curr_l = curr_l->next;

                                             }

                              }

                              return *this;

}

~list()

{

               clear();

}

// Capacity

bool empty(void) const

{

               return(head == NULL);

}

int size(void) const

{

               return _size;

}

public:

// output

// list: [1,2,3,4,5]

// output: 1->2->3->4->5->NULL

std::string toString(void) const

{

               if(empty())

                              return "Empty List";

               else

               {

                              std::string str = "";

                              node *curr = head;

                              while(curr != NULL)

                              {

                                             str = str + std::to_string(curr->data) + "->";

                                             curr = curr->next;

                              }

                              str = str + "NULL\n";

                              return str;

               }

}

void insert(int position, const int& data)

{

               if(position >= 0 && position <= _size)

               {

                              if(head == NULL)

                              {

                                             head = new node;

                                             head->data = data;

                                             head->next = NULL;

                              }else

                              {

                                             node *temp = new node;

                                             temp->data = data;

                                             temp->next = NULL;

                                             if(position == 0)

                                             {

                                                            temp->next = head;

                                                            head = temp;

                                             }else

                                             {

                                                            node *curr = head;

                                                            int i=0;

                                                            while(i<position-1)

                                                            {

                                                                           curr = curr->next;

                                                                           i++;

                                                            }

                                                            temp->next = curr->next;

                                                            curr->next = temp;

                                             }

                              }

                              _size++;

               }

}

void erase(int position)

{

               if(position >= 0 && position < _size)

               {

                              if(position == 0)

                              {

                                             node *temp = head;

                                             head = head->next;

                                             delete(temp);

                                             _size--;

                              }else{

                                             int i=0;

                                             node *curr = head;

                                             while(i < position-1)

                                             {

                                                            curr = curr->next;

                                                            i++;

                                             }

                                             node *temp = curr->next;

                                             curr->next = temp->next;

                                             delete(temp);

                                             _size--;

                              }

               }

}

void clear(void) {

               if (this->head != NULL) {

                              node* p = this->head;

                              while (p != NULL) {

                                             node* temp = p;

                                             p = p->next;

                                             delete temp;

               }

                              this->head = NULL;

               }

               this->_size = 0;

}

list& sort(void)

{

               if(head == NULL)

                              return *this;

               list l1;

               node *temp = head;

               while(temp != NULL)

               {

                              if(l1.head == NULL)

                              {

                                             l1.head = new node;

                                             l1.head->data = temp->data;

                                             l1.head->next = NULL;

                              }else{

                                             node *iNode = new node;

                                             iNode->data = temp->data;

                                             iNode->next = NULL;

                                             if(l1.head->data > temp->data)

                                             {

                                                            iNode->next = l1.head;

                                                            l1.head = iNode;

                                             }else{

                                                            node *curr = l1.head;

                                                            node *prev = NULL;

                                                            while((curr != NULL) && (curr->data < iNode->data))

                                                            {

                                                                           prev = curr;

                                                                           curr = curr->next;

                                                            }

                                                            iNode->next = curr;

                                                            prev->next = iNode;

                                             }

                              }

                              temp = temp->next;

               }

               l1._size = _size;

               *this = l1;

               return *this;

}

};

#endif

#end of SimpleList.hpp

// main.cpp

#include <iostream>

#include <string>

#include "SimpleList.hpp"

using std::cin;

using std::cout;

using std::endl;

using std::string;

int main() {

list li;

int n;

cin >> n;

for (int i = 0, data, pos; i < n; i++) {

               cin >> pos >> data;

               li.insert(pos, data);

}

cout << li.toString() << " size: " << li.size() << endl;

list li2(li);

list li3;

li = li3 = li2 = li;

cout << li.toString() << " size: " << li.size() << endl;

cout << li2.toString() << " size: " << li2.size() << endl;

cout << li3.toString() << " size: " << li3.size() << endl;

int m;

cin >> m;

for (int i = 0, pos; i < m; i++) {

               cin >> pos;

               li.erase(pos);

}

cout << li.toString() << endl;

cout << li.toString() << endl;

cout << li.sort().toString() << endl;

cout << li2.sort().toString() << endl;

cout << li3.sort().toString() << endl;

return 0;

}

//end of main.cpp

Output:

4 0 4 2 1 3 2 4->3->1->2->NULL 4->3->1->2->NULL 4->3->1->2->NULL 4->3->1->2->NULL size: 4 size: 4 size: 4 size: 4 4->3->2- >N

4 0 4 2 1 3 2 4->3->1->2->NULL 4->3->1->2->NULL 4->3->1->2->NULL 4->3->1->2->NULL size: 4 size: 4 size: 4 size: 4 4->3->2- >NULL 4->3->2- >NULL 2->3-4-NULL 1->2->3->4->NULL 1->2->3->4->NULL

Add a comment
Know the answer?
Add Answer to:
Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...
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
  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

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

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

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

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

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

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

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

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