Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// 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

// 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 class SortedList

{ private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType {

T data; NodeType* next; NodeType* prev;

}; 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

NodeType(const T & d = T()):data(d) {

next = nullptr; prev = nullptr;

}

protected: NodeType* current; T & retrieve( ) const {return current->data; } const_iterator( NodeType* p ) {current = p; } friend class SortedList;

}; class iterator : public const_iterator { public: iterator( ) {}

T & operator* ( ) { return const_iterator::retrieve( ); } const T & operator* ( ) const { return const_iterator::operator*( ); } iterator & operator++ ( )

{return current == rhs.current; } bool operator!= ( const const_iterator & rhs ) const {return !( *this == rhs ); }

{ 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; };

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.

iterated find(const T &x) { Node Type* ptr = head->next; while (ptr != NULL)

{

if (ptr ->d== x) return ptr;

current = ptr ->next; }

return NULL; }

// 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) { struct Node** head_ref; struct Node* new Node;

new Node->d = x; struct Node* current;

// if list is empty if (*head_ref == NULL)

*head_ref = new Node; // if the node is to be inserted at the beginning

// of the doubly linked list else if ((*head_ref)->d >= new Node->d) {

new Node->next = *head_ref; new Node->next->prev = new Node; *head_ref = new Node;

}

else { current = *head_ref;

// locate the node after which the new node // is to be inserted while (current->next != NULL &&

current->next->d d) current = current->next;

/* Make the appropriate links */ new Node->next = current->next;

// if the new node is not inserted // at the end of the list

if (current->next != NULL) new Node->next->prev = new Node;

current->next = new Node;

new Node->prev = current; }

}

// 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) { 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;

} }

}

private: int theSize; NodeType* head;

NodeType* tail; void init( ) { theSize = 0;

head->next = tail;

tail->prev = head; }

}; #endif

image text in transcribed

please show me your output !

please write code for the three files that in the question !

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 a2qlresult containing result Here are the sample runs: Enter values in the sorted st: 3 6 29 5 1 8 7 40 The sorted list is: 1 2 3 45 67 89 The values to be removed: The sorted st is: 1 3 5 79

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions

Question

Explain all drawbacks of the application procedure.

Answered: 1 week ago

Question

Determine Leading or Lagging Power Factor in Python.

Answered: 1 week ago