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

I have a program that has a couple quick Errors that need fixed. Involves a doub

ID: 3576950 • Letter: I

Question

I have a program that has a couple quick Errors that need fixed. Involves a double linked list. Make sure it runs as shown thanks. I have posted this Twice and the people answer it without no fixes. MAKE SURE IT RUNS BEFORE ANSWERING PLEASE!

its pissing me off.im over here getting robbed questions even though i talked to support and their in the process of laying the pipe down on the person.

dlist.h

{

front = NULL;

current = NULL;

}

void dlist::insert(char ch)

{

dptr newNode = new dlist_node();

newNode->contents = ch;

newNode->next = NULL;

newNode->back = NULL;

if(front == NULL)

{

front = newNode;

current = newNode;

}

else

if(current != NULL)

{

current -> next = newNode;

newNode->back = current;

newNode->next->back = newNode;

newNode->next = current->next;

}

}

bool dlist::remove()

{

dptr delete_node;

char ch;

if(current == NULL && front == NULL)

return false;

else

if (current -> contents == ch)

{

front = front->next;

current = delete_node;

delete current;

return true;

}

else

current ->back;

return true;

}

void dlist::Move_Right(int distance)

{

dptr new_node, current;

int count;

if(new_node != NULL)

for(count = 1; count <= distance; count++)

{

current = current->next;

}

else

if(current->next == NULL)

current->next = current->back;

}

void dlist::Move_Left(int distance)

{

dptr new_node, current;

int count;

if(new_node != NULL)

{

for(count = 1; count <= distance; count++)

{

current = current->back;

}

}

else

if(current->back == NULL)

current->back = current->next;

}

void dlist::print()

{

dptr pNode = front;

if(pNode == current)

cout<<"{"<<pNode->contents<<"}";

else

if(pNode != NULL)

do

{

cout << pNode->contents;

pNode = pNode->next;

}while(pNode != NULL && pNode != current);

}

dlist.cpp

Sample Run

The list is:

hell{o}

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): p

The list is:

hel{l}

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

   l (left) or q (quit): l

Enter the distance to move left: 20

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

   l (left) or q (quit): p

The list is:

{h}ell

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): d

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): p

The list is:

{l}

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): p

The list is:

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

   l (left) or q (quit): i

Enter the character to insert: a

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

   l (left) or q (quit): i

Enter the character to insert: b

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

   l (left) or q (quit): i

Enter the character to insert: c

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

   l (left) or q (quit): i

Enter the character to insert: d

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

   l (left) or q (quit): i

Enter the character to insert: e

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

   l (left) or q (quit): p

The list is:

abcd{e}

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

   l (left) or q (quit): l

Enter the distance to move left: 3

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

   l (left) or q (quit): r

Enter the distance to move right: 1

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

   l (left) or q (quit): p

The list is:

ab{c}de

Directions I was given along with outline code

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.

Explanation / Answer

// dlist.h

#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
bool 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 newNode = new dlist_node();
   newNode->contents = ch;
   newNode->next = NULL;
   newNode->back = NULL;
   if(front == NULL)
   {
       front = newNode;
       current = newNode;
   }
   else
       if(current != NULL)
       {
           current -> next = newNode;
           newNode->back = current;
           current = newNode;
       }
}
bool dlist::remove()
{
   dptr delete_node;
   if(current == NULL && front == NULL)
       return false;
   else{
       delete_node = current;
       if(current == front){
           front = NULL;
           current = NULL;
       }
       else{
           current = current->back;
           current->next = NULL;
       }
       delete(delete_node);
   }
   return true;
}
void dlist::Move_Right(int distance)
{
   dptr new_node, current;
   int count;
   if(new_node != NULL)
       for(count = 1; count <= distance; count++)
       {
           current = current->next;
       }
   else
       if(current->next == NULL)
           current->next = current->back;
}
void dlist::Move_Left(int distance)
{
   dptr new_node, current;
   int count;
   if(new_node != NULL)
   {
       for(count = 1; count <= distance; count++)
       {
           current = current->back;
       }
   }
   else
       if(current->back == NULL)
           current->back = current->next;
}
void dlist::print()
{
   dptr pNode = front;
   if(pNode != NULL)
       do
       {
           if(pNode == current)
               cout<<"{"<<pNode->contents<<"}";
           else cout << pNode->contents;
           pNode = pNode->next;
       }while(pNode != NULL);
}

// dlist.cpp

#include <iostream>
#include <cstdlib>
#include "dlist.h"

using namespace std;

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

   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: ";
/**** Call the print member function to print the dlist ****/
           dlistVar.print();

           cout << endl;
           break;
           case 'i' : cout << " Enter the character to insert: ";
           cin >> ch;
/**** Call the insert member function to insert ch in the dlist ****/
           dlistVar.insert(ch);
           break;
           case 'r' : cout << " Enter the distance to move right: ";
           cin >> distance;
/**** Call the move right member function to move current to the right distance spots ****/
           dlistVar.Move_Right(distance);
           break;
           case 'l' : cout << " Enter the distance to move left: ";
           cin >> distance;
/**** Call the move left member function to move current to the left distance spots ****/
           dlistVar.Move_Left(distance);
           break;
           case 'd' :
/**** Call the remove member function to remove the node pointed to by current ****/
           dlistVar.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;
}