Question

(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 T & d = T()):data(d)

{

next = nullptr;

prev = nullptr;

}

};

public:

class const_iterator

{

public:

// Public constructor for const_iterator.

const_iterator( )

{

current = nullptr;

}

const T & operator* ( ) const

{

return retrieve( );

}

const_iterator & operator++ ( )

{

current = current->next;

return *this;

}

const_iterator & operator-- ( )

{

current = current->prev;

return *this;

}

bool operator== ( const const_iterator & rhs ) const

{

return current == rhs.current;

}

bool operator!= ( const const_iterator & rhs ) const

{

return !( *this == rhs );

}

protected:

NodeType* current;

T & retrieve( ) const

{

return current->data;

}

const_iterator( NodeType* p )

{

current = p;

}

friend class SortedList<T>;

};

class iterator : public const_iterator

{

public:

iterator( )

{ }

T & operator* ( )

{

return const_iterator::retrieve( );

}

const T & operator* ( ) const

{

return const_iterator::operator*( );

}

iterator & operator++ ( )

{

this->current = this->current->next;

return *this;

}

iterator & operator-- ( )

{

this->current = this->current->prev;

return *this;

}

protected:

iterator( NodeType* p ) : const_iterator{ p }

{ }

friend class SortedList<T>;

};

public:

SortedList()

{

init( );

}

~SortedList()

{

clear( );

delete head;

delete tail;

}

iterator begin( )

{

return iterator( head->next );

}

const_iterator begin( ) const

{

return const_iterator( head->next );

}

// Return iterator representing endmarker of list.

// Mutator version is first, then accessor version.

iterator end( )

{

return iterator( tail );

}

const_iterator end( ) const

{

return const_iterator( tail );

}

// Return number of elements currently in the list.

int size( ) const

{

return theSize;

}

// Return true if the list is empty, false otherwise.

bool empty( ) const

{

return size( ) == 0;

}

void clear( )

{

while( !empty( ) )

iterator temp(erase(tail->prev));

}

// Find x in the list

// If x is found, return x position;

// If x is not found, return the position that x should be located.

iterator find(const T & x )

{

NodeType* ptr = head->next;

// add your code

}

// Insert x before itr.

iterator insert( iterator itr, const T & x )

{

NodeType* newptr = new NodeType(x);

NodeType* p = itr.current;

newptr->prev = p->prev;

newptr->next = p;

p->prev->next = newptr;

p->prev = newptr;

++theSize;

return iterator( newptr );

}

// Erase item at itr.

iterator erase( iterator itr )

{

NodeType* p = itr.current;

iterator retVal( p->next );

p->prev->next = p->next;

p->next->prev = p->prev;

delete p;

--theSize;

return retVal;

}

// add x in the sorted list, find the position that x should be

inserted, and then insert x

iterator add_item(const T & x )

{

// add your code

}

// remove x from the sorted list.

// If x is in the sorted list, find the position of x, and then

remove x.

// If x is not in the sorted list, return the position that x should

be located

iterator remove_item(const T & x )

{

//add your code

}

private:

int theSize;

NodeType* head;

NodeType* tail;

void init( )

{

theSize = 0;

head = new NodeType();

tail = new NodeType();

head->next = tail;

tail->prev = head;

}

};

#endif

1) Read the code carefully, and understand the nested classes const_iterator and

iterator.

2) Implement the member functions find, add_item, and remove_item based on

the corresponding comments. The three functions all return an iterator class.

3) Write an application program that

• creates an int type sorted list using SortedLinkedList class template.

• prompt the user to enter int values, and add these values to the sorted list, stop

adding the values when the user enter 0

• print the sorted list using iterator.

• prompt the user to enter int values to be removed, remove the values from the sorted

list, stop removing the values when the user enter 0.

• print the sorted list using iterator.

Three files should be submitted for this program question.

1) the file SortedLinkedList.h which contains the definition and

implementation of SortedLinkedList class template,

2) the application file a2q1.cpp containing main() function,

3) a script file a2q1result containing result.

Here are the sample runs:

Enter values in the sorted list:

3 6 2 9 5 1 8 7 4 0

The sorted list is:

1 2 3 4 5 6 7 8 9

The values to be removed:

4 6 2 8 0

The sorted list is:

1 3 5 7 9

...

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

As per the given details the program should be as follows :

iterated find(const T &x)

{

NodeType* ptr = head->next;

while (ptr != NULL)

    {

        if (ptr ->d== x)

            return ptr;

        current = ptr ->next;

    }

    return NULL;

}

=======================add_item

iterator add_item(const T &x)

{

struct Node** head_ref;

struct Node* newNode;

newNode->d = x;

struct Node* current;  

    // if list is empty

    if (*head_ref == NULL)

        *head_ref = newNode;

    // if the node is to be inserted at the beginning

    // of the doubly linked list

    else if ((*head_ref)->d >= newNode->d) {

        newNode->next = *head_ref;

        newNode->next->prev = newNode;

        *head_ref = newNode;

    }

    else {

        current = *head_ref;

        // locate the node after which the new node

        // is to be inserted

        while (current->next != NULL &&

               current->next->d < newNode->d)

            current = current->next;

        /* Make the appropriate links */

        newNode->next = current->next;

        // if the new node is not inserted

        // at the end of the list

        if (current->next != NULL)

            newNode->next->prev = newNode;

        current->next = newNode;

        newNode->prev = current;

    }

}

===================remove============

iterator remove_item(const T& x)

{

struct Node* current = head;

struct Node* next_next;

while (current->next != NULL)

    {

       /* Compare current node with next node */

       if (current->data == x)

       {

           /* The sequence of steps is important*/              

           next_next = current->next->next;

           free(current->next);

           current->next = next_next;

       }

       else /* This is tricky: only advance if no deletion */

       {

          current = current->next;

       }

    }

}

HOPE THIS MAY HELP YOU---

THANK YOU ……. PLEASE LIKE,IT WILL INCREASE MY SCORE,

HOPE YOU WILL ENCOURAGE ME…..

Add a comment
Know the answer?
Add Answer to:
(The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...
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...

  • 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++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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

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

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

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...

    Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Starter testDeque.cpp which contains: Timer class holder (you need to go through the LearnCpp Ch15 and import it in) Node class Deque class specification (you need to fill out the definition) here is the testDeque.cpp code: // C++ implementation of doubly linked list Deque doubly linked list...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

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