Question

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

#include

#include

using namespace std;

typedef int Type;

enum Boolean

{

False = 0, True

};

class Item

{

friend class SLList;

public:

Type getVal()

{

return val;

}

private:

Item(Type value, Item *item = 0)

{

val = value;

next = item;

}

Type val;

Item *next;

};

class SLList

{

public:

class Const_Iterator

{

friend class SLList;

public:

Item *current;

Const_Iterator() :

current(NULL)

{

}

const Item & operator*() const

{

return *this->current;

}

Const_Iterator operator++()

{

current = current->next;

return *this;

}

Const_Iterator operator++(int)

{

Const_Iterator old = *this;

++(*this);

return old;

}

bool operator==(const Const_Iterator & rhs)

{

return current == rhs.current;

}

bool operator!=(const Const_Iterator & rhs)

{

return !(*this == rhs);

}

Item retrieve() const

{

return current->val;

}

Const_Iterator(Item *p) :

current(p)

{

}

};

class Iterator

{

friend class SLList;

public:

Item *current;

Iterator() :

current(NULL)

{

}

const Item & operator*() const

{

return *this->current;

}

Iterator operator++()

{

current = current->next;

return *this;

}

Iterator operator++(int)

{

Iterator old = *this;

++(*this);

return old;

}

bool operator==(const Iterator & rhs) const

{

return current == rhs.current;

}

bool operator!=(const Iterator & rhs)

{

return !(*this == rhs);

}

Item retrieve()

{

return current->val;

}

Iterator(Item *p) :

current(p)

{

}

};

SLList()

{

list = 0;

}

~SLList()

{

remove();

}

void insert(Type someItem);

void append(Type someItem);

int remove(Type someItem);

void remove();

Item *atEnd();

Item *head();

Item *tail();

Boolean isPresent(Type someItem);

Boolean isEmpty();

void display();

private:

Iterator iterator;

Item *list;

Item *atEndItem;

};

Boolean SLList::isEmpty()

{

return list == 0 ? True : False;

}

void SLList::insert(Type val)

{

Item *pt = new Item(val, list); // create new item put it in front of list

assert(pt != 0);

if (list == 0)

{

atEndItem = pt;

}

list = pt; // and point next top the list

}

Item *SLList::atEnd()

{

if (list == 0)

{

return 0;

}

Item *prev, *curr;

prev = curr = list;

while (curr)

{

prev = curr;

curr = curr->next;

}

return prev;

}

Item *SLList::tail()

{

if (list == 0)

{

return 0;

}

Item *prev, *curr;

prev = curr = list;

while (curr)

{

prev = curr;

curr = curr->next;

}

return prev;

}

Item *SLList::head()

{

if (list == 0)

{

cout << "List is empty" << endl;

return 0;

}

else

{

return list;

} // end if

return list;

}

void SLList::append(Type val)

{

Item *pt = new Item(val);

assert(pt != 0);

if (list == 0)

{

list = pt;

}

else

{

atEndItem->next = pt;

}

atEndItem = pt;

}

void SLList::display()

{

cout << "(";

for (Item *pt = list; pt; pt = pt->next)

{

cout << pt->val << " ";

} // end for

cout << ")" << endl;

}

void SLList::remove()

{

Item *pt = list;

while (pt)

{

Item *temp = pt;

pt = pt->next;

delete temp;

} // end while

list = atEndItem = 0;

}

Boolean SLList::isPresent(Type item)

{

Boolean rc = False;

if (list != 0)

{

if (list->val == item || atEndItem->val == item)

{

rc = True;

}

else

{

Item *pt = list->next;

for (; pt != atEndItem; pt = pt->next)

{

if (pt->val == item)

{

rc = True;

} // end if

} // end for

} // end if

} // end if

return rc;

}

int SLList::remove(Type val)

{

Item *pt = list;

int cnt = 0;

while (pt && pt->val == val)

{

Item *tmp = pt->next;

delete pt;

cnt++;

pt = tmp;

} // end while

if ((list = pt) == 0)

{

atEndItem = 0;

return cnt;

} // end if

Item *prv = pt;

pt = pt->next;

while (pt)

{

if (pt->val == val)

{

prv->next = pt->next;

if (atEndItem == pt)

{

atEndItem = prv;

} // end if

delete pt;

++cnt;

pt = prv->next;

}

else

{

prv = pt;

pt = pt->next;

} // end if else

} // end while

return cnt;

}

int main()

{

cout << "Single Linked List Example" << endl;

const int size = 12;

const int odd = 1;

int ix = 0;

SLList il;

SLList::Iterator itr;

if (il.isEmpty() != True || il.isPresent(1024) != False)

{

cerr << "List class internal error (1)" << endl;

}

else

{

cout << "Ok, empty list class" << endl;

} // end ifelse

il.remove(1024);

il.display();

for (ix = 0; ix < size; ++ix)

{

il.append(ix % 2 ? odd : ix);

} // end for

il.display();

if (il.isEmpty() == False)

{

cout << " Head " << il.head()->getVal() << endl;

cout << " Tail " << il.tail()->getVal() << endl;

} // end if

if (il.isPresent(odd) != True)

{

cerr << "List class internal error (2)" << endl;

} // end if

int odd_cnt = il.remove(odd);

cout << odd_cnt << " items of value " << odd << " removed." << endl;

il.display();

if (il.isEmpty() == False)

{

cout << " Head " << il.head()->getVal() << endl;

cout << " Tail " << il.tail()->getVal() << endl;

} // end if

for (ix = 0; ix < odd_cnt; ++ix)

{

il.insert(odd);

} // end for

il.display();

if (il.isEmpty() == False)

{

cout << " Head " << il.head()->getVal() << endl;

cout << " Tail " << il.tail()->getVal() << endl;

} // end if

for (itr = il.head(); itr != il.tail(); itr++)

{

cout << "Itr = " << itr.current->getVal()<< endl;

} // end for

il.remove();

il.display();

if (il.isEmpty() == False)

{

cout << " Head " << il.head()->getVal() << endl;

cout << " Tail " << il.tail()->getVal() << endl;

} // end if

return 0;

}

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

Please find the code below.

CODE

void SLList::push_back(Type value)

{

Item *pt = new Item(value);

if (pt == NULL) return;

if (list == 0)

{

list = pt;

}

else

{

atEndItem->next = pt;

}

atEndItem = pt;

}

Add a comment
Know the answer?
Add Answer to:
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...
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
  • 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;...

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

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

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

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