By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,104 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,104 IT Pros & Developers. It's quick & easy.

Delete Node from link list...

P: 13
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(IntNode* & 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(headList,3);
headList->headInsert(headList,6);
headList->headInsert(headList,1);
headList->headInsert(headList,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::IntNode( )
{

}

IntNode::IntNode(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::headInsert(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);
}
Dec 11 '06 #1
Share this Question
Share on Google+
1 Reply


P: 10
IntNode *temp,*prev;
temp = headList;
prev = temp
while(temp -> setData != 6)
{
prev = temp; //prev points to the previous node.
temp = temp -> setLink;
}

prev -> setLink = temp -> setLink; //set prev next to temp next.
cout <<temp -> setData;
free(temp); // print data of temp and free its mem.

hope it helpd...

FDD
Dec 11 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.