C++ 处理链表时出现分段错误(核心转储)
好的,我的程序从一副牌中随机抽取牌,并将它们存储在一个链表数组中。每件西装都有一张单子,单子上的名次不变C++ 处理链表时出现分段错误(核心转储),c++,linked-list,segmentation-fault,C++,Linked List,Segmentation Fault,好的,我的程序从一副牌中随机抽取牌,并将它们存储在一个链表数组中。每件西装都有一张单子,单子上的名次不变 struct node{ node(int value = 0) {data=value; next = NULL; } int data; node *next; }; class list{ public: list (); ~list();
struct node{
node(int value = 0) {data=value; next = NULL; }
int data;
node *next;
};
class list{
public:
list ();
~list();
bool empty() const {return N == 0;}
bool full() const {return false;}
int size() const {return N;}
void resize(int);
void clear();
void insert(int);
void remove(int);
void pop_back() { remove(N-1);}
const int & back();
int & operator[](int);
int findNodeRank(int);
friend ostream& operator<<(ostream &out, list);
private:
int N;
node *head;
node *findnode(int);
};
list::list() {
cout << "making list" << endl;
head = new node;
cout << "made list" <<endl;
}
正如您所看到的,它永远不会离开列表数组,因为从不调用nothere。我不明白出了什么问题
好的,我把范围缩小到这个函数。我会尽力发布所有相关代码
if (i == 0){
node *p = new node(rankIndex);
cout << "before find node" << endl;
node *pp = findnode(N);
cout << "after find node" << endl;
p->next = pp->next;
pp->next = p;
N++;
}else {
prev->next = match->next;
match->next = head->next; .
head->next = match;
}
}
int list::findNodeRank(int rankIndex){ //Function similar to findnode that checks for rank match. Returns the index of the match.
if (head->next == 0)
return 0;
node *p = head->next;
int i = 1;
cout << "find node rank function" << endl;
while(p->data != 0){
if (p->data == rankIndex)
return i;
p = p->next;
i++;
}
return 0; //Returns 0 if there is no match
}
inline
node *list::findnode(int i) {
if (i == -1)
return head;
node *p = head->next;
while(i--)
p = p->next;
return p;
}
如果(i==0){
node*p=新节点(rankIndex);
cout next=匹配->下一步;
匹配->下一步=头部->下一步。
头部->下一步=匹配;
}
}
int list::findNodeRank(int rankIndex){//函数类似于检查秩匹配的findnode。返回匹配的索引。
如果(头->下一步==0)
返回0;
节点*p=头部->下一步;
int i=1;
cout data==rankIndex)
返回i;
p=p->next;
i++;
}
返回0//如果没有匹配项,则返回0
}
内联
节点*列表::findnode(int i){
如果(i==-1)
回流头;
节点*p=头部->下一步;
而(我--)
p=p->next;
返回p;
}
我可能引用了一个尚未创建的节点或其他东西。我不喜欢这些链表。好的,基本上就像我说的,程序会随机抽取卡片,并将结果存储在我创建的列表中。如果卡片从未绘制,则插入函数应将卡片添加到列表的末尾。如果抽到牌,它应该把牌移到名单的前面。有什么想法吗?在输出
“not here 1”
后,您不会输出std::endl
,因此假设SEGFULT是由创建列表引起的是不安全的。在下一次刷新std::cout
之前,可能是其他原因导致了它。通过调试器运行它将立即告诉您是什么导致了SEGFULT。似乎所有四个列表都已列出。如果你把你的代码放在正确的位置,它甚至在我的机器上运行Fedora19和GCC 4.8.1 20130603,会发生什么?程序的其余部分在哪里?主要功能?好吧,我不知道endl需要在那里。我进一步缩小了范围有什么想法吗?我编辑了我原来的帖子。
here 1
making list
made list
making list
made list
making list
made list
making list
made list
not here 1
before loopHere;
insert
after find node rank
before find node
after find node
[1] 11472 segmentation fault (core dumped) ./Prog2b
if (i == 0){
node *p = new node(rankIndex);
cout << "before find node" << endl;
node *pp = findnode(N);
cout << "after find node" << endl;
p->next = pp->next;
pp->next = p;
N++;
}else {
prev->next = match->next;
match->next = head->next; .
head->next = match;
}
}
int list::findNodeRank(int rankIndex){ //Function similar to findnode that checks for rank match. Returns the index of the match.
if (head->next == 0)
return 0;
node *p = head->next;
int i = 1;
cout << "find node rank function" << endl;
while(p->data != 0){
if (p->data == rankIndex)
return i;
p = p->next;
i++;
}
return 0; //Returns 0 if there is no match
}
inline
node *list::findnode(int i) {
if (i == -1)
return head;
node *p = head->next;
while(i--)
p = p->next;
return p;
}