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 );
}
1 6569
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Martin Magnusson |
last post by:
I'm getting segmentation faults when trying to fix a memory leak in my
program. The problem is related to lists of pointers which get passed
around between objects.
Here is a description of how the code works:
I have a stack consisting of nodes and arguments, much like a function
execution stack. In this example there are only three nodes on the
stack. For each run through the loop, a copy of the current sensation is
written to the...
|
by: MJ |
last post by:
Hi
I have created a prog for link list
I am addig a node, displaying the LL contents, reversing the LL and
deleting the LL
In the delete() if I use free I am getting memory exception
I have used malloc in the add()at the same time
If I used new and delete its working fine
Can any one tell me the reason
|
by: Sumika |
last post by:
Hello,
I'm a newbie here, so don't know much friends.
I've problem deleting my node at the tail, so could you all help me to
solve my error,I worked on it for quite sometime but it just can't work.
Thanks
The following is my code:
private void btnDeleteRear_Click(object sender, System.EventArgs e)
|
by: Raj |
last post by:
Is there any way to delete a particular node from a singly linked list
where the header of the list is unknown.Only pointer available is the
one which points to the node to be deleted
|
by: mathon |
last post by:
hello,
i thought i create a new topic for that because i deal with a certain
problem. I have implemented all methods for a LinkdeList (for my
sequence class) and tested all correct. Now i only have problems with
the remove method to delete a certain node from the list. my remove
method looks like this:
void sequence::remove_current( )
| |
by: sangram |
last post by:
how to delete last node of a Linked list if you only know the address
of last node.
thanks
sangram
|
by: rohits123 |
last post by:
I have an overload delete operator as below
//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetMemoryHead(head_type);
mmFree(&local_Head,(char *)mem);
CPRMemory::SetMemoryHeadAs(local_Head,head_type);
}
/////////////////////
void* operator new(size_t sz, int head_Type) {
|
by: hotice |
last post by:
How to write code to delete a specific node in a single link list that
takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the
time deleting a node is the same (independent from the length of the
list. Show your c/c++ source code.
|
by: moon24 |
last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7
then the result should be 2 7 1 12
here is what I have:
#include <iostream>
using namespace std;
class NumberList
{
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |