链表问题.. [英] Problem with linked list ..

查看:38
本文介绍了链表问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,祝圣诞快乐。我正在尝试创建一个简单的

链接列表,看起来我是在使用DeleteNode函数做错了什么...


#include< malloc.h>

#include< stdio.h>

#include< stdlib.h>

#include< iostream.h>

#include" GenericStringClass2.h"

#include< string.h>

#define默认0


类MACListPart

{

public:

//构造函数

MACListPart();

~MACListPart();

MACListPart * GetNextPointer();

private:

朋友类MACList;

字符串macAdr;

int数据;

MACListPart * next;

} ;

类MACList

{

public:

//构造函数

MACList ();

~MACList();


//访问者

void PrintList(void);

void AddNode(const String& mac);

void DeleteNode(const String& mac);

bool FindNode(const String&);

// void DeleteList();

bool IsListEmpty();

int GetCount(无效);


受保护:

MACListPart * head;

MACListPart * current;


私人:

int itsCount;

};


MACListPart :: MACListPart (无效)

{

next = NULL;

data = 0;


// macAdr = ..

}

MACListPart :: ~MACListPart(无效)

{

cout<< ; 为MACListPart调用默认析构函数...

\ n" ;;

if(this!= NULL)

{


删除此内容;

}

否则删除此内容;

}


MACListPart * MACListPart :: GetNextPointer(void)

{

if(this == NULL)

{

cout<< 你指的是无处,你在哪里

认为你要去?\ n" ;;

返回NULL;

}

其他

{

返回此 - >下一个;

}

}

MACList :: MACList(无效)

{

head = NULL;

current = NULL;

itsCount = 0;

}

MACList :: ~MACList(无效)

{/ *

if(IsListEmpy()== true)

{

//什么都不做..

}

else

{

DeleteList();

} * /

}


bool MACList :: IsListEmpty(void)

{

if(head == NULL)

{

返回true;

}


//其他....

返回false;

}

bool MACList :: FindNode(const String& mac)

{

MACListPart * pMacListPart = head;

while(pMacListPart!= NULL)

{

if(pMacListPart-> macAdr == mac)

{
返回true;

}

}

返回false;

}

int MACList :: GetCount(void)

{

返回itsCount;

}

void MACList :: AddNode(const String& mac)

{

MACListPart * pMACListPart = new MACListPart;

pMACListPart-> macAdr = mac;

pMACListPart-> data = DEFAULT;

pMACListPart-> next = NULL;


if(IsListEmpty()== true)

{

head = pMACListPart;

current = head;

}

else //(如果IsListEmpty()== false)

{

current-> next = pMACListPart;

current = pMACListPart;

}

//增加列表元素

itsCount ++;

}



void MACList :: PrintList()

{

MACListPart * temp = head;

while(temp!= NULL)

{

cout<< temp-> macAdr.GetString()<< endl;

temp = temp-> next;

}

}

void MACList :: DeleteNode( const String& mac)

{

char c;

//如果列表的第一个元素将被删除:

if(head-> macAdr == mac)

{

MACListPart * tempNode;

tempNode = head- >下一个;

删除头;

head = tempNode;

}

其他

{

MACListPart * prevNode,* currNode;

prevNode = head;

currNode = head-> next;

while(currNode!= NULL)

{

if(currNode-> macAdr == mac)

{

MACListPart * tempNode;

tempNode = currNode-> next;

prevNode-> next = tempNode;

printf(YAHOOOO \ n);

this-> PrintList();

c = getchar();

delete currNode;


}

else //你在currNode中找不到字符串

so ...

{
prevNode = currNode;

currNode = currNode-> next;

}

}

}

}

int main(无效)

{

MACList MACListIstance;

MACListIstance.AddNode(" 11-11-11-11-11-11");

MACListIstance.AddNode(" 22-22-22-22-22-22" );

MACListIstance.AddNode(" 33-33-33-33-33-33");

MACListIstance.AddNode(" 44-44-44- 44-44-44");

MACListIstance.PrintList();

bool t = MACListIstance.FindNode(" 11-11-11-11-11-11" ;);

MACListIstance.DeleteNode(" 22-22-22-22-22-22");

MACListIstance.PrintList();

if(t == true)cout<< 这个MAC地址存在于

数据库中...... \ n" ;;

其他cout<< 这个MAC地址不存在于

数据库中.... \ n" ;;

cout<< Hello world ...列表当前有 <<

MACListIstance.GetCount()<< " elements \ n" ;;

返回0;

}

问题是当我试图删除节点时..程序挂起

命令后

删除currNode执行... String类在

中声明另一个文件,这里是

默认析构函数:

String :: ~String()

{

cout<< 调用String的默认析构函数.. \ n;;

delete [] itsString;

itsLen = 0;

cout< ;<" String的默认析构函数... \ n" ;;

}

你能帮帮忙吗?我正在使用Visual C ++


(对不起我的英文...)

解决方案


" Andrew Skouloudis" < AF ******************** @ yahoo.com>在消息中写道

新闻:o8 ******************************** @ 4ax.com ...

大家好,祝圣诞快乐。我正在尝试创建一个简单的链接列表,似乎我在使用DeleteNode函数做错了...

class MACListPart
{
public:
//构造函数
MACListPart();
~MACListPart();
MACListPart * GetNextPointer();

private:
朋友类MACList;
字符串macAdr;
int数据;
MACListPart *下一个;
};

MACListPart :: ~MACListPart(void)
{
cout<< 为MACListPart调用默认析构函数...
\ n;


/ *

if(this!= NULL)
{

删除此内容;
}
否则删除此;


* /

}


您不需要为MACListPart编写析构函数。如果你这样做......你不会想要做自杀事件......


记得在
中减少它的数量

void MACList :: DeleteNode(const String& mac)函数,你会得到......


11-11-11-11- 11-11

22-22-22-22-22-22

33-33-33-33-33-33

44 -44-44-44-44-44

YAHOOOO

11-11-11-11-11-11

33-33- 33-33-33-33

44-44-44-44-44-44

11-11-11-11-11-11

33-33-33-33-33-33

44-44-44-44-44-44

此MAC地址存在于数据库中.. 。

Hello world ...列表目前有3个元素


这就是你想要的吗?

你能帮忙吗? ?我正在使用Visual C ++

(对不起我的英文...)




问候


Brian


On Sun,2003年12月28日12:22:14 -1000,Brian MacBride

< ma *** ***@ix.netcom.com>写道:


Andrew Skouloudis < AF ******************** @ yahoo.com>在消息中写道
新闻:o8 ******************************** @ 4ax.com .. < blockquote class =post_quotes>大家好,圣诞快乐。我正在尝试创建一个简单的链接列表,似乎我在使用DeleteNode函数做错了...

class MACListPart
{
public:
//构造函数
MACListPart();
~MACListPart();
MACListPart * GetNextPointer();

private:
朋友类MACList;
字符串macAdr;
int数据;
MACListPart *下一个;
};

MACListPart :: ~MACListPart(void)
{
cout<< 调用MACListPart的默认析构函数......
\ n;



/ *

if(this! = NULL)
{

删除此内容;
}
否则删除此内容;



* /

}



您不需要为MACListPart编写析构函数。如果你这样做......你不想做自杀事件...

记得在

void MACList ::中减少它的数量:: DeleteNode(const String& mac)功能,你会得到...

11-11-11-11-11-11
22-22-22-22-22 -22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
11-11-11-11-11 -11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11 > 33-33-33-33-33-33
44-44-44-44-44-44这个MAC地址存在于数据库中......
Hello world ...该列表目前有3个元素

这是你想要的吗?


你能帮帮忙吗?我正在使用Visual C ++

(对不起我的英文...)



问候

Brian



首先谢谢你的回答

其次..你是怎么做到的?


我确实把它放了>

if(this!= NULL)

{


删除此内容;

}

否则删除这个;


在评论中仍然程序挂起!!你能给我你的String类的

析构函数????

我甚至删除了MACListPart中的析构函数定义,所以

当删除节点时,编译器应该做任何想做的事。

但是仍然没有! 。


这是我程序的输出:


调用String的默认析构函数..

默认被调用的析构函数...

调用String的默认析构函数..

调用的默认析构函数...

调用默认的析构函数字符串..

被调用的默认析构函数...

调用String的默认析构函数..

默认的析构函数被调用...

11-11-11-11-11-11

22-22-22-22-22-22

33-33-33 -33-33-33

44-44-44-44-44-44

YAHOOOO

调用字符串的默认析构函数..

默认的析构函数...

11-11-11-11-11-11

33-33-33-33- 33-33

44-44-44-44-44-44 //现在挂起!!!




Andrew Skouloudis < AF ******************** @ yahoo.com>在留言中写道

新闻:h7 ******************************** @ 4ax.com ...

On Sun,2003年12月28日12:22:14 -1000,Brian MacBride
< ma ****** @ ix.netcom.com>写道:


Andrew Skouloudis < AF ******************** @ yahoo.com>在消息中写道
新闻:o8 ******************************** @ 4ax.com .. < blockquote class =post_quotes>大家好,圣诞快乐。我正在尝试创建一个简单的链接列表,似乎我在使用DeleteNode函数做错了...

class MACListPart
{
public:
//构造函数
MACListPart();
~MACListPart();
MACListPart * GetNextPointer();

private:
朋友类MACList;
字符串macAdr;
int数据;
MACListPart *下一个;
};

MACListPart :: ~MACListPart(void)
{
cout<< 调用MACListPart的默认析构函数......
\ n;



/ *

if(this! = NULL)
{

删除此内容;
}
否则删除此内容;



* /

}



您不需要为MACListPart编写析构函数。如果你这样做...你b $ b不想做自杀事件......

记得在

void MACList :: DeleteNode中减少它的数量(const String& mac)功能,你会得到......

11-11-11-11-11-11
22-22-22-22-22- 22
33-33-33-33-33-33

YAHOOOO
11-11-11-11-11- 11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44这个MAC地址存在于数据库中......
Hello world ...列表目前有3个元素

那是你想要的吗?


你能帮帮忙吗?我正在使用Visual C ++

(对不起我的英文...)



问候

Brian


首先感谢你的回答
其次..你是怎么做到的?

我确实把

if(this!= NULL)
{

删除这个;
}
否则删除这个;

在评论中仍然程序挂起!!你能给我一个String类的析构函数????
我甚至删除了MACListPart中的析构函数定义,所以当删除节点时,
编译器应该做任何想做的事情。
但仍然没有! 。

这是我程序的输出:

调用String的默认析构函数。
默认的析构函数被调用...
调用String的默认析构函数。
默认的析构函数调用...
调用String的默认析构函数。
调用默认的析构函数...
调用String的默认析构函数..
默认的析构函数...
11-11-11-11-11-11
22-22-22-22-22-22
33-33 -33-33-33-33
44-44-44-44-44-44 YAHOOOO
调用字符串的默认析构函数。
调用的默认析构函数.. 。
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44 //现在挂起!!!




你没有忘记你有这个傻....


c = getchar( );


....这里。失去它...


问候


Brian


Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include "GenericStringClass2.h"
#include <string.h>
#define DEFAULT 0

class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();
private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};
class MACList
{
public:
//constructors
MACList();
~MACList();

//accessors
void PrintList(void);
void AddNode(const String& mac);
void DeleteNode(const String& mac);
bool FindNode(const String&);
// void DeleteList();
bool IsListEmpty();
int GetCount(void);

protected:
MACListPart *head;
MACListPart *current;

private:
int itsCount;
};

MACListPart::MACListPart(void)
{
next=NULL;
data=0;

// macAdr=..
}
MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
\n";
if(this != NULL)
{

delete this;
}
else delete this;
}

MACListPart* MACListPart::GetNextPointer(void)
{
if(this==NULL)
{
cout << "You are pointing to nowhere , where do you
think you are going ??\n";
return NULL;
}
else
{
return this->next;
}
}
MACList::MACList(void)
{
head=NULL;
current=NULL;
itsCount=0;
}
MACList::~MACList(void)
{/*
if(IsListEmpy()==true)
{
//do nothing ..
}
else
{
DeleteList();
}*/
}

bool MACList::IsListEmpty(void)
{
if(head==NULL)
{
return true;
}

//else ....
return false;
}
bool MACList::FindNode(const String& mac)
{
MACListPart *pMacListPart=head;
while(pMacListPart !=NULL)
{
if(pMacListPart->macAdr==mac)
{
return true;
}
}
return false;
}
int MACList::GetCount(void)
{
return itsCount;
}
void MACList::AddNode(const String& mac)
{
MACListPart *pMACListPart = new MACListPart;
pMACListPart->macAdr=mac;
pMACListPart->data=DEFAULT;
pMACListPart->next=NULL;

if(IsListEmpty() == true)
{
head=pMACListPart;
current=head;
}
else // (if IsListEmpty() == false )
{
current->next=pMACListPart;
current=pMACListPart;
}
// increasing the elements of the list
itsCount++;
}


void MACList::PrintList()
{
MACListPart *temp = head;
while( temp != NULL)
{
cout << temp->macAdr.GetString() << endl;
temp=temp->next;
}
}
void MACList::DeleteNode(const String& mac)
{
char c;
// If the first element of the list is going to be deleted:
if(head->macAdr==mac)
{
MACListPart *tempNode;
tempNode=head->next;
delete head;
head=tempNode;
}
else
{
MACListPart *prevNode,*currNode;
prevNode=head;
currNode=head->next;
while(currNode != NULL)
{
if(currNode->macAdr==mac)
{
MACListPart *tempNode;
tempNode=currNode->next;
prevNode->next=tempNode;
printf("YAHOOOO\n");
this->PrintList();
c=getchar();
delete currNode;

}
else // you didn''t find the string in currNode
so ...
{
prevNode=currNode;
currNode=currNode->next;
}
}
}
}
int main(void)
{
MACList MACListIstance;
MACListIstance.AddNode("11-11-11-11-11-11");
MACListIstance.AddNode("22-22-22-22-22-22");
MACListIstance.AddNode("33-33-33-33-33-33");
MACListIstance.AddNode("44-44-44-44-44-44");
MACListIstance.PrintList();
bool t = MACListIstance.FindNode("11-11-11-11-11-11");
MACListIstance.DeleteNode("22-22-22-22-22-22");
MACListIstance.PrintList();
if (t==true) cout << "This MAC address exists into the
database ... \n";
else cout << "This MAC address does not exist in the
database.... \n";
cout << "Hello world ... the list currently has " <<
MACListIstance.GetCount() << " elements \n";
return 0;
}
The problem is when i am trying to delete a node .. The program hangs
after the command
delete currNode is executed ... The String class is declared in
another file and here is the
default destructor :
String::~String()
{
cout << "Calling the default destructor of String ..\n";
delete [] itsString;
itsLen=0;
cout <<"Default destructor of String finished ... \n";
}
Can you help ?? I am using Visual C++

(Sorry for my english ... )

解决方案


"Andrew Skouloudis" <af********************@yahoo.com> wrote in message
news:o8********************************@4ax.com...

Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...
class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();
private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};

MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
\n";
/*
if(this != NULL)
{

delete this;
}
else delete this;
*/
}

You don''t need to write destructor for MACListPart. If you do... you don''t
want to do the suicide thing...

Remember to decrement itsCount in the

void MACList::DeleteNode (const String &mac) function and you''ll get...

11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
This MAC address exists into the database ...
Hello world ... the list currently has 3 elements

Is that what you wanted?

Can you help ?? I am using Visual C++

(Sorry for my english ... )



Regards

Brian


On Sun, 28 Dec 2003 12:22:14 -1000, "Brian MacBride"
<ma******@ix.netcom.com> wrote:


"Andrew Skouloudis" <af********************@yahoo.com> wrote in message
news:o8********************************@4ax.com.. .

Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...
class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();
private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};

MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
\n";



/*

if(this != NULL)
{

delete this;
}
else delete this;



*/

}



You don''t need to write destructor for MACListPart. If you do... you don''t
want to do the suicide thing...

Remember to decrement itsCount in the

void MACList::DeleteNode (const String &mac) function and you''ll get...

11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
This MAC address exists into the database ...
Hello world ... the list currently has 3 elements

Is that what you wanted?


Can you help ?? I am using Visual C++

(Sorry for my english ... )



Regards

Brian


First of all thank you for your answer
Secondly .. how did you do that ??

I did put

if(this != NULL)
{

delete this;
}
else delete this;

in comments and still the program hangs !! Can you give me the
destructor of your String class ????
I even removed the destructor definition in the MACListPart , so the
compiler should do whatever it want to, when deleting the node .
But still nothing !!! .

This is the output of my program :

Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44 // And now hangs !!!



"Andrew Skouloudis" <af********************@yahoo.com> wrote in message
news:h7********************************@4ax.com...

On Sun, 28 Dec 2003 12:22:14 -1000, "Brian MacBride"
<ma******@ix.netcom.com> wrote:


"Andrew Skouloudis" <af********************@yahoo.com> wrote in message
news:o8********************************@4ax.com.. .

Hello people and merry christmas. I am trying to create a simple
linked list and it seems i am
doing something wrong with the DeleteNode function ...
class MACListPart
{
public:
// constructor
MACListPart();
~MACListPart();
MACListPart *GetNextPointer();
private:
friend class MACList;
String macAdr;
int data;
MACListPart *next;
};

MACListPart::~MACListPart(void)
{
cout << "Calling the default destructor for MACListPart ...
\n";



/*

if(this != NULL)
{

delete this;
}
else delete this;



*/

}



You don''t need to write destructor for MACListPart. If you do... you don''twant to do the suicide thing...

Remember to decrement itsCount in the

void MACList::DeleteNode (const String &mac) function and you''ll get...

11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44
This MAC address exists into the database ...
Hello world ... the list currently has 3 elements

Is that what you wanted?


Can you help ?? I am using Visual C++

(Sorry for my english ... )



Regards

Brian


First of all thank you for your answer
Secondly .. how did you do that ??

I did put

if(this != NULL)
{

delete this;
}
else delete this;

in comments and still the program hangs !! Can you give me the
destructor of your String class ????
I even removed the destructor definition in the MACListPart , so the
compiler should do whatever it want to, when deleting the node .
But still nothing !!! .

This is the output of my program :

Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
22-22-22-22-22-22
33-33-33-33-33-33
44-44-44-44-44-44
YAHOOOO
Calling the default destructor of String ..
Default destructor of called ...
11-11-11-11-11-11
33-33-33-33-33-33
44-44-44-44-44-44 // And now hangs !!!



You didn''t forget that you have this silly....

c = getchar ();

....here. Lose it...

Regards

Brian


这篇关于链表问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆