Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The class list was implemented on singular linked list. Re-implement it on doubly linked list take in account the set-position method and the operator overload

The class list was implemented on singular linked list. Re-implement it on doubly linked list take in account the set-position method and the operator overload and copy constructor and destructor.

i want the solve of this qoustion reffered to this code

#include #define MAX 100 using namespace std; template struct node { T data; node*back, * next; node(){ next=back=0; } node(T item){ data = item; next=back=0; } }; template class list { protected: node * head; int count; node* setpos(int pos){ node * temp =head; for(int i=0;inext; return temp; } public: list(){ head=NULL; count=0;} bool empty(){ return (count==0); } bool insert(int pos,T item){ if (pos < 0 || pos > count) return 0; node *temp, *prev, *follow; if (pos > 0){ prev = setpos((pos-1)); follow = prev->next; } //if else follow = head; temp = new node(item); temp->next=follow; if (temp == NULL) return 0; if (pos == 0) head = temp; else prev->next = temp; count++; return 1; } bool retrieve(int pos,T &item){ if (pos < 0 || pos >= count) return 0; if(empty()) return 0; node *temp=setpos(pos); item = temp->data; return 1; } bool remove(int pos){ if(empty()) return 0; if (pos < 0 || pos >= count) return 0; node *temp, *prev, *follow; if (pos > 0){ prev = setpos((pos-1)); follow = prev->next; }//if else follow = head; if (pos == 0) head = head->next; else prev->next = follow->next; delete follow; count--; return 1; } bool replace(int pos,T item){ if (pos < 0 || pos >= count) return 0; if(empty()) return 0; node *temp=setpos(pos); temp->data=item; return 1; } int size(){ return count; } ~list(){ while(!empty()) remove(size()-1); } void operator=(list &original){ node *new_top, *new_copy, *original_node = original.head; if (original_node == NULL) new_top = NULL; else{ new_copy = new_top = new node(original_node->data); while (original_node->next != NULL){ original_node = original_node->next; new_copy->next = new node(original_node->data); new_copy = new_copy->next; } //while }//else while (!empty()) remove(0); head = new_top; count=original.count; } list(const list &original){ node *new_copy, *original_node = original.head; if (original_node == NULL) head = NULL; else{ head = new_copy = new node(original_node->data); while (original_node->next != NULL){ original_node = original_node->next; new_copy->next = new node(original_node->data); new_copy = new_copy->next; }//while }//else count=original.count; }//copy constractor }; template void print(list &L){ T item; for (int i=0;i void fill(list&L, int n){ T item; for (int i=1;i<=n;i++){ item=rand()%100; L.insert(0,item); } } int main() { list L,ll; L.insert(0,100); L.insert(0,200); L.insert(0,300); int item=-1; fill(ll,5); ll=L; cout<

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

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

2. To identify examples of each.

Answered: 1 week ago

Question

Whats involved in listening?

Answered: 1 week ago

Question

=+j Explain IHRMs role in global HR research.

Answered: 1 week ago

Question

=+j Describe an effective crisis management program.

Answered: 1 week ago