/******************************************************************************
ID: 3778717 • Letter: #
Question
/******************************************************************************
File name : 2170_10_3b.cc
Purpose : This program demonstrates the use of doubly linked lists
and deletion of a node.
******************************************************************************/
#include <iostream>
#include "2170_10_3b.h"
using namespace std;
// This is the constructor for the DLList object
DLList::DLList()
{
head1 = head2 = NULL;
}// end of constructor
// This is the destructor for the DLList object
DLList::~DLList()
{
NodePtrType q = head1;
while (q != NULL)
{
head1 = head1->next;
q->next = NULL;
delete q;
q = head1;
}
}// end of destructor
// This function inserts newData at the end of the list.
void DLList::InsertAtEnd(float newData)
{
//create a pointer for the new data
NodePtrType p;
//create a node for the new data
p = new NodeType;
//store the new data
p->data = newData;
p->next = NULL;
//if list is empty, set node to head of list
if (head1 == NULL)
{
head1 = p;
head1->prev = NULL;
//set head2 to point to only node
head2 = head1;
}
else
{
//set new node to point to old head2
//and vice-versa
p->prev = head2;
head2->next = p;
//set head2 to new node
head2 = p;
}
}
// This function finds and returns the node denoted by the index.
// Indexes start at 1. If index is larger than the number of nodes
// in the list, the last node is returned. If index is 1 or less,
// the head node is returned. If the list is empty, NULL is returned.
NodePtrType DLList::FindNode(int index)
{
NodePtrType p; //node to return
// start at the beginning of the list
p = head1;
// if the list is not empty
if (p != NULL)
{
//move up nodes until index or end of list reached
for (int i = 1; i < index && p->next != NULL; i++)
{
p = p->next;
}
}
return p;
}
// This function deletes the node pointed to by p, saving
// the data in the variable saveData
// Add code to delete the node pointed to by p.
// Remember these special cases, though:
// 1. The list is empty
// 2. There is only one node
// 3. p is head1 or head2
// Don't forget to free the space occupied by the node,
// and don't forget to collect the deleted data in saveData.
void DLList::DeleteNode(NodePtrType p, float& saveData)
{
}
// This function prints the list in order
void DLList::PrintForward()
{
NodePtrType p;
// start at beginning of list
p = head1;
// continue through whole list
while (p != NULL)
{
// print data
cout << p->data << endl;
// move to next node
p = p->next;
}
cout << endl;
}
void DLList::PrintReverse()
{
NodePtrType p;
// start at end of list
p = head2;
// continue through whole list
while (p != NULL)
{
// print data
cout << p->data << endl;
// move to previous node
p = p->prev;
}
cout << endl;
}
Explanation / Answer
Here goes the function implementation for deletenode
Code:
void DLList::DeleteNode(NodePtrType p,float &saveData){
//save the data of deleting node in saveData
saveData=p->data;
//checking if the list is not empty
if(head1!=NULL){
//if the node to be deleted is the headnode
if(head1==p){
head1=p->next;
}
//change next only if the node to be deleted is not the last node
if(p->next!=NULL){
p->next->prev=p->prev;
}
//change prev only if the node to be deleted is not the first node
if(p->prev!=NULL){
p->prev->next=p->next;
}
}
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.