Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using your knowledge of Circular and Doubly-Linked list algorithms, create a new

ID: 3858586 • Letter: U

Question

Using your knowledge of Circular and Doubly-Linked list algorithms, create a new class combining the two into a new data structure: a Circular, Doubly-Linked List. Call the class DCList and place it in it's own header file, DCList.h. Your class must implement the methods as described in the above UML diagram. The following methods should be included in your class: constructor- initializes pointers in the class to NULL. destructor- destroys the linked list. clear- destroys the linked list, resetting pointers to NULL. insert-inserts it's argument into the list, maintaining an ascending order. Returns 0 on success, -1 on failure. append- attached it's argument to the end of the list. remove- removes the first node containing it's argument from the list. Returns 0 on success, -1 on failure. isFull-returns true if the list is full, false otherwise. isEmpty - returns true if the list is empty, false otherwise. print - displays the content of the list to the screen on a single line. Each value is separated by a space reverse- displays the content of the list to the screen on a single line, but in reverse order. next-displays the "next" value in the list. It does this by advancing the cur pointer and then assigning the node's value to next's reference parameter. Returns 0 on success, -1 on failure. length-returns the number of nodes in the list.

Explanation / Answer

The code for the implementation of the Doubly Linked List is as follows. I tried to keep it simple as far as I could. Hope you'll like it.

DoublyLinkedList.cpp

#include<iostream>

#include<cstdio>

#include<cstdlib>

using namespace std;

struct Nde

{

int inform;

struct Nde *nxt;

struct Nde *previous;

}*strt;

class DoublyList

{

public:

void newList(int val);

void addStrt(int val);

void addEnd(int val, int posit);

void delEl(int val);

void searchEl(int val);

void dispList();

void cnt();

void rev();

DoublyList()

{

strt = NULL;

}

};

int main()

{

int ch, ele, posit;

DoublyList db;

while (1)

{

cout<<endl<<"----------------------------"<<endl;

cout<<endl<<"Operations on Doubly linked list"<<endl;

cout<<endl<<"----------------------------"<<endl;   

cout<<"1.Create Node"<<endl;

cout<<"2.Add at begining"<<endl;

cout<<"3.Add after posit"<<endl;

cout<<"4.Delete"<<endl;

cout<<"5.Display"<<endl;

cout<<"6.Count"<<endl;

cout<<"7.Reverse"<<endl;

cout<<"8.Quit"<<endl;

cout<<"Enter your ch : ";

cin>>ch;

switch ( ch )

{

case 1:

cout<<"Enter the ele: ";

cin>>ele;

db.newList(ele);

cout<<endl;

break;

case 2:

cout<<"Enter the ele: ";

cin>>ele;

db.addStrt(ele);

cout<<endl;

break;

case 3:

cout<<"Enter the ele: ";

cin>>ele;

cout<<"Insert Element after postion: ";

cin>>posit;

db.addEnd(ele, posit);

cout<<endl;

break;

case 4:

if (strt == NULL)

{

cout<<"List empty,nothing to delete"<<endl;   

break;

}

cout<<"Enter the ele for deletion: ";

cin>>ele;

db.delEl(ele);

cout<<endl;

break;

case 5:

db.dispList();

cout<<endl;

break;

case 6:

db.cnt();

break;

case 7:

if (strt == NULL)

{

cout<<"List empty,nothing to rev"<<endl;

break;

}

db.rev();

cout<<endl;

break;

case 8:

exit(1);

default:

cout<<"Wrong ch"<<endl;

}

}

return 0;

}

void DoublyList::newList(int val)

{

struct Nde *sin, *temporary;

temporary = new(struct Nde);

temporary->inform = val;

temporary->nxt = NULL;

if (strt == NULL)

{

temporary->previous = NULL;

strt = temporary;

}

else

{

sin = strt;

while (sin->nxt != NULL)

sin = sin->nxt;

sin->nxt = temporary;

temporary->previous = sin;

}

}

void DoublyList::addStrt(int val)

{

if (strt == NULL)

{

cout<<"First Create the list."<<endl;

return;

}

struct Nde *temporary;

temporary = new(struct Nde);

temporary->previous = NULL;

temporary->inform = val;

temporary->nxt = strt;

strt->previous = temporary;

strt = temporary;

cout<<"Element Inserted"<<endl;

}

void DoublyList::addEnd(int val, int pos)

{

if (strt == NULL)

{

cout<<"First Create the list."<<endl;

return;

}

struct Nde *tempo, *tt;

int uu;

tt = strt;

for (uu = 0;uu < pos - 1;uu++)

{

tt = tt->nxt;

if (tt == NULL)

{

cout<<"There are less than ";

cout<<pos<<" elements."<<endl;

return;

}

}

tempo = new(struct Nde);

tempo->inform = val;

if (tt->nxt == NULL)

{

tt->nxt = tempo;

tempo->nxt = NULL;

tempo->previous = tt;

}

else

{

tempo->nxt = tt->nxt;

tempo->nxt->previous = tempo;

tt->nxt = tempo;

tempo->previous = tt;

}

cout<<"Element Inserted"<<endl;

}

void DoublyList::delEl(int val)

{

struct Nde *tempo, *tt;

if (strt->inform == val)

{

tempo = strt;

strt = strt->nxt;

strt->previous = NULL;

cout<<"Element Deleted"<<endl;

free(tempo);

return;

}

tt = strt;

while (tt->nxt->nxt != NULL)

{   

if (tt->nxt->inform == val)

{

tempo = tt->nxt;

tt->nxt = tempo->nxt;

tempo->nxt->previous = tt;

cout<<"Element Deleted"<<endl;

free(tempo);

return;

}

tt = tt->nxt;

}

if (tt->nxt->inform == val)

{

tempo = tt->nxt;

free(tempo);

tt->nxt = NULL;

cout<<"Element Deleted"<<endl;

return;

}

cout<<"Element "<<val<<" not found"<<endl;

}

void DoublyList::dispList()

{

struct Nde *tt;

if (strt == NULL)

{

cout<<"List empty,nothing to display"<<endl;

return;

}

tt = strt;

cout<<"The Doubly Link List is :"<<endl;

while (tt != NULL)

{

cout<<tt->inform<<" <-> ";

tt = tt->nxt;

}

cout<<"NULL"<<endl;

}

void DoublyList::cnt()

{

struct Nde *tt = strt;

int cnt = 0;

while (tt != NULL)

{

tt = tt->nxt;

cnt++;

}

cout<<"Number of elements are: "<<cnt<<endl;

}

void DoublyList::rev()

{

struct Nde *proc, *proc1;

proc = strt;

proc1 = proc->nxt;

proc->nxt = NULL;

proc->previous = proc1;

while (proc1 != NULL)

{

proc1->previous = proc1->nxt;

proc1->nxt = proc;

proc = proc1;

proc1 = proc1->previous;

}

strt = proc;

cout<<"List Reversed"<<endl;

}

Please rate the answer if it helped.......Thankyou

Hope it helps.......