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

Using the header (dlist ) and mainline file ( dlistapp ) below You are going to

ID: 3779665 • Letter: U

Question

Using the header(dlist) and mainline file (dlistapp) below

You are going to write public functions for a doubly linked list class.

The dlist class is declared in the file dlist.h as follows:

struct dlist_node

{

char contents;    // contents in the node

dlist_node *back, // pointer to previous node in the list

             *next; // pointer to the next node in the list

};

typedef dlist_node* dptr;

class dlist

{

    private:

      dptr front,   // pointer to the front of the list

           current; // pointer to current node in the list

    public:

      dlist ();   // constructor creates an empty list

      void insert (char ch); // inserts a new node           

      void remove ();        // removes a node     

      void Move_Right(int distance); // moves current right

      void Move_Left(int distance); // moves current left

      void print ();   // prints the list

};

The public functions that you need to define are:

dlist (): Constructor that initializes the list to be empty.

void insert (char ch): Adds a new node to the right of current containing ch and points current at the new node. Should insert first node correctly.

void remove (): Removes the node from the list pointed to by current. Points current at the node after the deleted node (if present) else points current at the node before the deleted node (if present). Should remove last node correctly and recycle nodes. Should not fail if list is empty.

void Move_Right (int distance): Moves current to the right distance nodes. If the given distance will move current off the end of the list, current should be set to point at the rightmost node. Should not fail if list is empty.

void Move_Left (int distance): Moves current to the left distance nodes. If the given distance will move current off the end of the list, current should be set to point at the leftmost node. Should not fail if list is empty.

void print (): Prints all the nodes in the list. The value pointed to by current should be printed in braces.print does not output any spaces or linefeeds. For example, if the data in the linklist represents CIS 361, the output would be :

CIS 3{6}1

The dlistapp.cpp application file is coded except for the calls to the public functions. Add the calls as directed by the comments in the file.

dlist.h

dlistapp.cpp

Sample run

Select p (print), i (insert), d (delete), r (right),

   l (left) or q (quit): d

Select p (print), i (insert), d (delete), r (right),

   l (left) or q (quit): r

Enter the distance to move right: 10

Select p (print), i (insert), d (delete), r (right),

   l (left) or q (quit): l

Enter the distance to move left: 2

Select p (print), i (insert), d (delete), r (right),

   l (left) or q (quit): p

The list is:

Select p (print), i (insert), d (delete), r (right),

   l (left) or q (quit): i

Enter the character to insert: h

front current

Explanation / Answer

#include<iostream>

using namespace std;

struct dlist_node
{
char contents; // contents in the node
dlist_node *back, // pointer to previous node in the list
*next; // pointer to the next node in the list
};

typedef dlist_node* dptr;

class dlist
{
private:
dptr front, // pointer to the front of the list
current; // pointer to current node in the list
public:
dlist (); // constructor creates an empty list
void insert (char ch); // inserts a new node
void remove (); // removes a node
void Move_Right (int distance); // moves current to right
void Move_Left (int distance); // moves current to left
void print (); // prints the list
};

dlist::dlist()
{
front = NULL;
current = NULL;
}

void dlist::insert(char ch)
{
dptr node = new dlist_node;
node->contents = ch;
node->back = current;
node->next = NULL;
current = node;
  
if (front == NULL)
front = node;
}

void dlist::remove()
{
dptr node = current;
dptr prev = current->back;
if (prev)
prev->next = current->next;
delete current;
if (prev && prev->next == NULL)
current = prev;
else
current = prev->next;
}

void dlist::Move_Right(int distance)
{
int i;
for(i = 0;i < distance;i++)
{
if (current && current->next)
current = current->next;
else
break;
}
}

void dlist::Move_Left(int distance)
{
int i;
for(i = 0;i < distance;i++)
{
if (current && current->back)
current = current->back;
else
break;
}
}

void dlist::print()
{
dptr n = front;
  
while(n != NULL)
{
cout<<n->contents<<" ";
n = n->next;
}
}

int main(int argc, char *argv[])
{
char choice, ch;
int distance;
dlist *lis = new dlist;
/**** Declare a dlist variable ****/

cout << "Select p (print), i (insert), d (delete), r (right),";
cout << " l (left) or q (quit): ";
cin >> choice;

while (choice != 'q')
{
switch (choice)
{
case 'p' :
{
cout << " The list is: ";
lis.print();
cout << endl;
break;
}

case 'i' :
{
cout << " Enter the character to insert: ";
cin >> ch;
lis.insert(ch);
break;
}
  
case 'r' :
{
cout << " Enter the distance to move right: ";
cin >> distance;
lis.Move_Right(distance);
break;
}
  
case 'l' :
{
cout << " Enter the distance to move left: ";
cin >> distance;
lis.Move_Left(distance);
break;
}
  
case 'd' :
{
lis.remove();
cout << endl;
break;
}
  
default : cout << " Must enter p, i, d, r, l, or q! ";
}
cout << " Select p (print), i (insert), d (delete), r (right),";
cout << " l (left) or q (quit): ";
cin >> choice;
}

cout << " Editing Complete ";


return 0;
}