I am having problems deleting a node from a link list. I need to delete the node which contains the number six. This is what I have so far.....
Thank you in advance.
#include <iostream>
#include "stdafx.h"
using namespace std;
class IntNode
{
public:
// constructors
IntNode( );
IntNode(int theData, IntNode* theLink) ;
//getters and setters
IntNode* getLink( ) const { return link; }
int getData( ) const { return data; }
void setData(int theData){ data= theData; };
void setLink(IntNode * pointer){ link = pointer; }
void headInsert(IntN ode* & head, int theData);
void insert(IntNode* afterMe, int theData);
;
private:
int data;
IntNode *link;
};
void printList (IntNode* head);
int _tmain(int argc, _TCHAR* argv[])
{
IntNode *headList = new IntNode();
//IntNode *temp;
//Create first node of list
headList->setData(5);
headList->setLink(NULL );
// Add next 4 elements to list
headList->headInsert(hea dList,3);
headList->headInsert(hea dList,6);
headList->headInsert(hea dList,1);
headList->headInsert(hea dList,2);
cout << "\nOriginal List:\n";
printList (headList);
// DELETE THE NODE CONTAINING NUMBER 6 from the list HERE:
IntNode *temp;
temp = headList;
cout << "\nList after deleting node with number 6, by :\n";
printList (headList);
return 0;
}// end main
void printList (IntNode* head)
{
IntNode* temp;
temp = head;
while (temp != NULL)
{
cout << temp->getData()<< "\t";
temp = temp->getLink();
//temp->setLink(temp->getLink());
}
cout << "\nEnd of List!\n";
}
// Constructor Implementations
IntNode::IntNod e( )
{
}
IntNode::IntNod e(int theData, IntNode* theLink)
{
data = theData;
link = theLink;
}
//Function Definitions
//Function to Add a Node at the Head of a Linked List
//Function Declaration
void IntNode::headIn sert(IntNode* & head, int theData)
//Precondition: The pointer variable head points to
//the head of a linked list.
//Postcondition: A new node containing theData
//has been added at the head of the linked list.
{
head = new IntNode(theData , head);
}
//Function to Add a Node in the Middle of a Linked List
//Function Declaration
void IntNode::insert (IntNode* afterMe, int theData)
//Precondition: afterMe points to a node in a linked list.
//Postcondition: A new node containing theData
//has been added after the node pointed to by afterMe.
{
IntNode * temp;
temp = new IntNode(theData , afterMe->link);
afterMe->setLink(temp );
}
Thank you in advance.
#include <iostream>
#include "stdafx.h"
using namespace std;
class IntNode
{
public:
// constructors
IntNode( );
IntNode(int theData, IntNode* theLink) ;
//getters and setters
IntNode* getLink( ) const { return link; }
int getData( ) const { return data; }
void setData(int theData){ data= theData; };
void setLink(IntNode * pointer){ link = pointer; }
void headInsert(IntN ode* & head, int theData);
void insert(IntNode* afterMe, int theData);
;
private:
int data;
IntNode *link;
};
void printList (IntNode* head);
int _tmain(int argc, _TCHAR* argv[])
{
IntNode *headList = new IntNode();
//IntNode *temp;
//Create first node of list
headList->setData(5);
headList->setLink(NULL );
// Add next 4 elements to list
headList->headInsert(hea dList,3);
headList->headInsert(hea dList,6);
headList->headInsert(hea dList,1);
headList->headInsert(hea dList,2);
cout << "\nOriginal List:\n";
printList (headList);
// DELETE THE NODE CONTAINING NUMBER 6 from the list HERE:
IntNode *temp;
temp = headList;
cout << "\nList after deleting node with number 6, by :\n";
printList (headList);
return 0;
}// end main
void printList (IntNode* head)
{
IntNode* temp;
temp = head;
while (temp != NULL)
{
cout << temp->getData()<< "\t";
temp = temp->getLink();
//temp->setLink(temp->getLink());
}
cout << "\nEnd of List!\n";
}
// Constructor Implementations
IntNode::IntNod e( )
{
}
IntNode::IntNod e(int theData, IntNode* theLink)
{
data = theData;
link = theLink;
}
//Function Definitions
//Function to Add a Node at the Head of a Linked List
//Function Declaration
void IntNode::headIn sert(IntNode* & head, int theData)
//Precondition: The pointer variable head points to
//the head of a linked list.
//Postcondition: A new node containing theData
//has been added at the head of the linked list.
{
head = new IntNode(theData , head);
}
//Function to Add a Node in the Middle of a Linked List
//Function Declaration
void IntNode::insert (IntNode* afterMe, int theData)
//Precondition: afterMe points to a node in a linked list.
//Postcondition: A new node containing theData
//has been added after the node pointed to by afterMe.
{
IntNode * temp;
temp = new IntNode(theData , afterMe->link);
afterMe->setLink(temp );
}
Comment