I can't figure out why when I want to delete the first node of my linked list, it doesn't seem to work. I think it might have something to do with the stressing of pointers in this class and that this one seems to be riddled with them, but I just can't figure it out. To me it looks like it should work. Kind of long, but just look first at my deleteNode function, then in main I am testing it. Thanks in advance for the help.
#include <iostream>
//#include <conio.h>
usingnamespace std;
//prototypes here
//void traverseList(node *); // can't do prototype here
//class definition
class node
{
//private data items anf functions
// data items
int* number; // pointer to an int
node* next; // node pointer forward
node* prev; // node pointer back
public:
/*************************************
* public data items anf functions
*************************************/
// constructor
node(int);
// destructor
~node();
// setters
void setNext(node* node) {next = node;}
void setPrev(node* node) {prev = node;}
void setNumber(int x) {*number = x;}
// getters
node* getNext() {return next;}
node* getPrev() {return prev;}
int getNumber() {return *number;}
// display the contents of a node
void display (node* current);
}; //end class node
//member functions
//constructor
node::node(int num)
{
cout << "in constructor for " << num << "\n";
number = newint(num);
setNext(NULL);
setPrev(NULL);
}//end constructor
//destructor
node::~node()
{
cout << "in destructor for " << getNumber() << "\n";
delete number;
}//end destructor
/*****************************************
* display the contents of a node
*****************************************/
void node::display(node* current)
{
cout << "\n";
cout << "number " << getNumber() << "\n";
cout << "prev node " << getPrev() << "\n";
cout << "next node " << getNext() << "\n";
cout << "current node " << current << "\n";
}//end node display
/*****************************************
*
* step through the linked list
* and print each node
*
*****************************************/
void traverseList(node* beginning)
{
cout << "\n\nin traverseList\n";
node* current;
current = beginning;
current->display(current);
while(current->getNext() != NULL)
{
current = current->getNext();
current->display(current);
}
}//ende traverseList
/*****************************************
*
* delete a node from the linked list
*
*****************************************/
void deleteNode(node* beginning, int find)
{
cout << "\n\nin delete node\n";
node* current;
current = beginning;
while(current->getNumber() != find && current->getNext() != NULL)
{
current = current->getNext();
}
current->display(current);
if(current->getNumber() != find)
{
cout << "\n no match \n\n";
}
else
{
if(current->getPrev() == NULL)
{
cout << "\n delete beginning \n\n";
current->getNext()->setPrev(NULL);
}
elseif(current->getNext() == NULL)
{
cout << "\n delete end \n\n";
current->getPrev()->setNext(NULL);
}
else
{
cout << "\n delete in nthe middle\n\n";
current->getNext()->setPrev(current->getPrev());
current->getPrev()->setNext(current->getNext());
}
}
}//ende deleteNode
/*****************************************
*
* insert a node into the linked list
*
*****************************************/
void insertNode(node* beginning, int find)
{
cout << "\n\nin insert node\n";
}//ende insert node
/*********************
* other functions
*********************/
void loadLinkedList(node**);
/*********************
* main
*********************/
int main()
{
node *start; // create a pointer to a node
node* *ppStart; // create a node pointer to a node pointer
ppStart = &start; // put the address of our first node pointer
// into the node pointer pointer
loadLinkedList(ppStart); // load the linked list
//traverseList(start);
//system("pause");
deleteNode(start, 7);
traverseList(start);
system("pause");
deleteNode(start, 2);
traverseList(start);
system("pause");
deleteNode(start, 13);
traverseList(start);
system("pause");
deleteNode(start, 8);
traverseList(start);
system("pause");
//traverseList(start);
cout << "\n";
system("pause");
return 0;
}
/**************************************************
*
* create a linked list of seven nodes
* with the values of 2, 3, 5, 7, 9, 11, and 13
*
**************************************************/
void loadLinkedList(node* *pps)
{
node* head; // start of the linked list
node* temp; // a temporary place for a node pointer
node* savePtr; // save the node pointer
head = new node(2); // create the 1st (start) node
savePtr = head; // save address of CURRENT node in savePtr
temp = new node(3); // create the next NEW node
savePtr->setNext(temp); // set setNext of the PREVIOUS node using address of CURRENT node
temp->setPrev(savePtr); // set setPrevious of CURRENT node using saved pointer
savePtr = temp; // save address of CURRENT node in savePtr to use with next node
temp = new node(5); // create the next NEW node
savePtr->setNext(temp); // set setNext of the PREVIOUS node using address of CURRENT node
temp->setPrev(savePtr); // set setPrevious of CURRENT node using saved pointer
savePtr = temp; // save address of CURRENT node in savePtr to use with next node
temp = new node(7); // create the next NEW node
savePtr->setNext(temp); // set setNext of the PREVIOUS node using address of CURRENT node
temp->setPrev(savePtr); // set setPrevious of CURRENT node using saved pointer
savePtr = temp; // save address of CURRENT node in savePtr to use with next node
temp = new node(9); // create the next NEW node
savePtr->setNext(temp); // set setNext of the PREVIOUS node using address of CURRENT node
temp->setPrev(savePtr); // set setPrevious of CURRENT node using saved pointer
savePtr = temp; // save address of CURRENT node in savePtr to use with next node
temp = new node(11); // create the next NEW node
savePtr->setNext(temp); // set setNext of the PREVIOUS node using address of CURRENT node
temp->setPrev(savePtr); // set setPrevious of CURRENT node using saved pointer
savePtr = temp; // save address of CURRENT node in savePtr to use with next node
temp = new node(13); // create the next NEW node
savePtr->setNext(temp); // set setNext of the PREVIOUS node using address of CURRENT node
temp->setPrev(savePtr); // set setPrevious of CURRENT node using saved pointer
savePtr = temp; // save address of CURRENT node in savePtr to use with next node
traverseList(head); // list the created linked list
*pps = head; // pass the address of the 'start' node back to main()
system("pause");
}
after i try to delete the first one, 2, and i test it by traversing the list, it is still there everytime. All of the other deletions in main work, but that one doesnt ;;
what should I do? I though that if I just set the second node's previous to null then i am essentially removing the first node from the list right? well, obviously not though haha, any ideas?