Please ! Update the DLinkedList class deletePos( ) BELOW method to account for a
ID: 3596081 • Letter: P
Question
Please !
Update the DLinkedList class deletePos( ) BELOW method to account for a single node list and an empty list. Your code should run with the driver (main) included in the code.
//COMMENT WHAT YOU ADD
//C++
#include <iostream>
using namespace std;
class Node {
public:
char letter;
Node *next, *prev;
};
class DLinkedList {
private:
Node *head, *tail; //Node pointers to head and tail
public:
DLinkedList()
{
head = NULL;
tail = NULL;
}
DLinkedList(char item) //overloaded constructor
{
Node *temp;
temp = new Node;
temp->letter = item;
temp->next = NULL;
temp->prev = NULL;
head = temp;
tail = temp;
}
void addAfter(char item, int pos);
void deletePos(int pos); //homework
void printAll();
void insertFront(char item);
void deleteFront();
void insertBack(char item);
void deleteBack();
};
void DLinkedList::deletePos(int pos)
{
Node *before, *p, *after;
p = head;
for (int i = 0; i < pos; i++) //make p point to pos
{
p = p->next;
}
before = p->prev;
after = p->next;
if (after == NULL) //last in list
{
before->next = NULL;
tail = before;
}
else if (p->prev == NULL) //if first in list
{
after->prev = NULL;
head = after;
}
//add conditions for for one item list and empty list
// else if (one item in list) //change conditions in parenthesis
//else if (empty list) //change conditions in parenthesis
else
{
before->next = after;
after->prev = before;
}
}
void DLinkedList::addAfter(char item, int pos)
{
Node *temp, *p;
p = head;
for (int i = 0; i < pos; i++) //make p point to pos
{
p = p->next;
}
//insert the node behind the current position
temp = new Node;
temp->letter = item;
//check to see if we are at the end
if (p->next == NULL)
{
//temp.insertBack(item);
p->next = temp;
temp->next = NULL;
}
else
{
temp->next = p->next; //sets temp next to the old next
(temp->next)->prev = temp; //sets the next node's previos to temp
p->next = temp;
}
temp->prev = p;
}
//----------copied and edited from SLinkedList----------
void DLinkedList::printAll() //function to print all the values in the list
{
Node *p; //creates a pointer to a node
p = head;
int i = 0;
if (head == NULL && tail == NULL) //empty list
exit;
else
{
while (p->next != NULL) //while the next value is not pointing to null
{
cout << "[" << i << "]" << p->letter << " "; //print out the nodes letter
p = p->next;
i++;
}
cout << "[" << i << "]" << p->letter << endl; //print the last node
}
}
void DLinkedList::insertFront(char item)
{
Node* temp;
temp = new Node;
temp->letter = item; //in python temp.letter = item
if (head == NULL) { //first item added to the list
temp->next = NULL;
tail = temp;
}
else
{
temp->next = head;
}
temp->prev = NULL;
head = temp;
}
void DLinkedList::deleteFront()
{
Node *p;
p = head;
head = p->next;
}
int main()
{
DLinkedList abc; //create a linked list called abc
abc.insertFront('D');
abc.addAfter('C', 0);
abc.addAfter('B', 0);
abc.printAll();
abc.deletePos(0);
abc.printAll();
abc.deletePos(0);
abc.printAll();
abc.deletePos(0);
abc.printAll();
return 0;
}
Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node{
public:
char letter;
Node *next,*prev;
};
class DLinkedList{
private:
Node *head,*tail;//Nodepointerstoheadandtail
public:
DLinkedList()
{
head=NULL;
tail=NULL;
}
DLinkedList(char item)//overloadedconstructor
{
Node *temp;
temp=new Node;
temp->letter=item;
temp->next=NULL;
temp->prev=NULL;
head=temp;
tail=temp;
}
void addAfter(char item,int pos);
void deletePos(int pos);//homework
void printAll();
void insertFront(char item);
void deleteFront();
void insertBack(char item);
void deleteBack();
};
void DLinkedList::deletePos(int pos)
{
Node *before,*p,*after;
p=head;
if (pos == 0){
head = head->next;
if (head == NULL){
tail = NULL;
}
return;
}
for(int i=0;i<pos;i++)//makeppointtopos
{
p=p->next;
}
before=p->prev;
after=p->next;
if(after==NULL)//lastinlist
{
before->next=NULL;
tail=before;
}
else
{
before->next=after;
after->prev=before;
}
}
void DLinkedList::addAfter(char item,int pos)
{
Node *temp,*p;
p=head;
for(int i=0;i<pos;i++)//makeppointtopos
{
p=p->next;
}
//insertthenodebehindthecurrentposition
temp=new Node;
temp->letter=item;
//checktoseeifweareattheend
if(p->next==NULL)
{
//temp.insertBack(item);
p->next=temp;
temp->next=NULL;
}
else
{
temp->next=p->next;//setstempnexttotheoldnext
(temp->next)->prev=temp;//setsthenextnode'spreviostotemp
p->next=temp;
}
temp->prev=p;
}
//----------copiedandeditedfromSLinkedList----------
void DLinkedList::printAll()//functiontoprintallthevaluesinthelist
{
Node *p;//createsapointertoanode
p=head;
int i=0;
if(head==NULL&&tail==NULL)//emptylist
exit;
else
{
while(p->next!=NULL)//whilethenextvalueisnotpoint ingtonull
{
cout<<"["<<i<<"]"<<p->letter<<"";//printoutthenodesletter
p=p->next;
i++;
}
cout<<"["<<i<<"]"<<p->letter<<endl;//printthelastnode
}
}
void DLinkedList::insertFront(char item)
{
Node *temp;
temp=new Node;
temp->letter=item;//inpythontemp.letter=item
if(head==NULL){//firstitemaddedtothelist
temp->next=NULL;
tail=temp;
}
else
{
temp->next=head;
}
temp->prev=NULL;
head=temp;
}
void DLinkedList::deleteFront()
{
Node *p;
p=head;
head=p->next;
}
int main()
{
DLinkedList abc;//createalinkedlistcalledabc
abc.insertFront('D');
abc.addAfter('C',0);
abc.addAfter('B',0);
abc.printAll();
abc.deletePos(0);
abc.printAll();
abc.deletePos(0);
abc.printAll();
abc.deletePos(0);
abc.printAll();
return 0;
}