使用类的 C++ 单向链表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9709107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ singly linked list using a classes
提问by Matthew Grossman
I want to create singly linked list (using classes), where in each list there will be: pointer to text,int number, pointer to next list.
我想创建单链表(使用类),在每个列表中将有:指向文本的指针、整数数字、指向下一个列表的指针。
I need to implement 3 functions: inserts(which inserts a list into singly linked list and sorts elements with strcmp according to text which is pointed by pointer) removes(int num) which removes first list in which number occurs. print() which prints the whole singly linked list.
我需要实现 3 个函数: inserts(将列表插入单链表并根据指针指向的文本使用 strcmp 对元素进行排序) removes(int num) 删除出现数字的第一个列表。print() 打印整个单链表。
I have problem with removes function which gives error in runtime and I have a conjecture where is the problem if ( tmp->next == NULL && tmp->number==num ) {
delete tmp;
first = NULL;
}
, but I have no idea why is it so .
我的 removes 函数有问题,它在运行时出错,我猜想问题出在哪里if ( tmp->next == NULL && tmp->number==num ) {
delete tmp;
first = NULL;
}
,但我不知道为什么会这样。
Also I am not sure how should I implement sorting into insert function, so if you have any ideas and if you could explain me where in my removes function the error is I would really appreciate it.
另外我不确定我应该如何将排序实现到插入函数中,所以如果你有任何想法,如果你能解释我的删除函数中的错误是我真的很感激。
Here's the code:
这是代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class list
{
private:
int number;
char* word;
list* next;
public:
void inserts(int num, char* text);
void removes(int num);
void print();
};
list* first;
void list::print() {
cout <<"This is our list:"<<endl;
// Temp pointer
list *tmp = first;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY list" << endl;
return;
}
// One node in the list
if ( tmp->next == NULL ) {
cout <<"NUMBER:\t"<< tmp->number;
cout <<"\tWORD:\t"<< tmp->word << endl;
cout <<"--------------------------------"<<endl;
}
else {
// Parse and print the list
while ( tmp != NULL ){
cout <<"NUMBER:\t"<< tmp->number;
cout <<"\tWORD:\t"<< tmp->word << endl;
cout <<"--------------------------------"<<endl;
tmp = tmp->next;
}
}
}
void list::inserts(int num, char* word){
// Create a new list
list* newlist = new list;
newlist->number=num;
newlist->word=word;
newlist->next=NULL;
// Create a temp pointer
list *tmp = first;
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->next != NULL ) {
tmp = tmp->next;
}
// Point the last node to the new node
tmp->next=newlist;
}
else {
// First node in the list
first = newlist;
}
}
void list::removes(int num){
int k = 0;
list* tmp=first;
if(tmp==NULL)
return;
//Last node of the list
if ( tmp->next == NULL && tmp->number==num ) {
delete tmp;
first = NULL;
}
else {
//Parse thru the nodes
list* prev;
prev = new list;
while ( tmp != NULL )
{
if ( tmp->number == num && k == 0)
first = first->next;
if ( tmp->number == num)
break;
prev = tmp;
tmp = tmp->next;
k++;
}
//Adjust the pointers
prev->next=(tmp->next);
//Delete the current node
delete tmp;
delete prev;
}
}
int main ()
{
first->print();
first->inserts(1200,"endian");
first->print();
/* first->inserts(10,"endianness");
first->inserts(1200,"PEEK");
first->inserts(1200,"POKE");
first->inserts(1200,".MIL");
first->print();*/
first->removes(100);
first->print();
getchar();
}
回答by Kim Kardashian
Get rid of the delete prev;
in the last line of the removes()
function.
去掉函数delete prev;
最后一行的removes()
。
I'm no expert, but by deleting prev you are losing the reference to your first node in the list.
我不是专家,但是通过删除 prev,您将丢失对列表中第一个节点的引用。
By the way, good comments, made it very easy to read!
顺便说一句,很好的评论,让它很容易阅读!
回答by Rob?
In removes
, after the loop while ( tmp != NULL ) { … }
, tmp
is possibly NULL.
在removes
循环之后while ( tmp != NULL ) { … }
,tmp
可能为 NULL。
But, after that loop, you have these lines:
但是,在那个循环之后,你有这些行:
//Adjust the pointers
prev->next=(tmp->next);
You are dereferencing a NULL pointer, which causes your program to crash.
您正在取消引用 NULL 指针,这会导致您的程序崩溃。
Replace those lines with these:
用这些替换这些行:
//Adjust the pointers
if(tmp)
prev->next=(tmp->next);
另请注意:
removes
removes
例程中的内存管理仍然存在错误。您永远不会调用delete
delete
的初始值prev
prev
,并且您删除了在某些情况下应该保留的列表节点。而不是你在做什么,初始化它这样:list* prev=0;
list* prev=0;
,并摆脱delete prev
delete prev
.回答by Chaos
When this loop executes:
当这个循环执行时:
while ( tmp != NULL )
your tmp pointer will reach the end of the list, and will be NULL when the loop breaks.
您的 tmp 指针将到达列表的末尾,并且在循环中断时将为 NULL。
So, when you execute :
所以,当你执行:
prev->next=(tmp->next);
your temp pointer does not have a "next" value. Maybe you should maintain another pointer outside of the loop.
您的临时指针没有“下一个”值。也许您应该在循环之外维护另一个指针。