好吧,我的程序随机从卡片中抽出卡片并将它们存储在一系列链接列表中。每个诉讼一个清单,清单保持等级。
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;
}
我的主要功能有这些行
cout << "here 1" << endl;
list deck[4];
cout << "not here 1";
编译并运行我的代码后,我得到以下输出。
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;
}
我可能正在引用一个尚未创建的节点。我对这些链接列表不好。好吧基本上像我说的程序绘制随机卡并将结果存储在我创建的这些列表中。如果从未绘制过插入函数,则应该将卡添加到列表的末尾。如果卡被绘制,它应该将其移动到列表的前面。任何想法??