好的,我的程序从一副牌中随机抽取卡片并将它们存储在一系列链表中。每个西装都有一个列表,该列表包含排名。
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
正如你所看到的,它永远不会退出列表数组,因为 not here 永远不会被调用。我不明白出了什么问题。
好的,我将其缩小到此功能。我会尝试发布所有相关代码
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;
}
我可能引用了一个尚未创建的节点或其他东西。我对这些链接列表不好。好的,基本上像我说的程序会随机抽取卡片并将结果存储在我创建的这些列表中。如果从未绘制过卡片,则插入函数应该将卡片添加到列表的末尾。如果牌被抽出来,它应该把它移到列表的前面。有任何想法吗??