【发布时间】:2018-08-13 10:21:10
【问题描述】:
我有两个包含行、列和值的链接列表,我试图为此编写一个函数来检查条目是否也存在于下一行中。 所以例如给定第 5 行 col2,检查第 6 行是否存在,然后检查第 6 行中是否存在第 2 行。不知何故,该功能没有运行,我希望有人可以帮助我。如果您需要更多代码或解释,请联系我。
谢谢提前
大卫
/*
row: 1
column: 3 value: 0
column: 7 value: 0
row: 5
column: 2 value: 0
column: 6 value: 0
row: 6
column: 2 value: 0
*/
// Row list
struct row_list
{
int row_number;
struct row_list *link_down;
struct value_list *link_right;
};
// Column list + value
struct value_list
{
int column_index;
int value;
struct value_list *next;
};
// col list
void create_value_node(unsigned int data, unsigned int j, struct row_list **z)
{
struct value_list *temp, *d;
temp = (struct value_list*)malloc(sizeof(struct value_list));
temp->column_index = j;
temp->value = data;
temp->next = NULL;
if ((*z)->link_right==NULL)
(*z)->link_right = temp;
else
{
d = (*z)->link_right;
while(d->next != NULL)
d = d->next;
d->next = temp;
}
}
// row list
void create_row_list(struct row_list **start, unsigned int row, unsigned int column, struct array *p)
{
for (unsigned int i = 0; i < row; i++)
{
struct row_list *z, *r;
unsigned int rx = p[i].x;
z = (struct row_list*)malloc(sizeof(struct row_list));
z->row_number = rx;
z->link_down = NULL;
z->link_right = NULL;
if (i==0)
*start = z;
else
{
r = *start;
while (r->link_down != NULL)
r = r->link_down;
r->link_down = z;
}
while(p[i].x==rx)
{
create_value_node(0, p[i].y, &z);
i++;
}
i--;
}
}
bool function(struct row_list *start,int x, int y){
struct row_list *tmpr;
struct value_list *tmpc;
tmpr=start;
int currow = x;
while((tmpr->row_number)<currow){
tmpr=tmpr->link_down;
}
if (tmpr->link_right != NULL)
{ tmpc = tmpr->link_right;}
int curcol = y;
tmpr=tmpr->link_down;
if((tmpr->row_number)==(currow+1)){
while (tmpc != NULL) {
if((tmpc->column_index)==curcol) {
return true;
}
if((tmpc->column_index)>curcol) {
return false;
}
tmpc=tmpc->next;
}
}
return false;
}
int main(){
if(function(start,x,y)){
printf("*%u*%u*%u***\n\n",x,y,v);
}
【问题讨论】:
-
你能否比“不知何故函数没有运行”更具体。有关您的问题的更详细信息将使您更容易为您提供帮助。你试过调试代码吗?
-
你能说明你是如何调用 void create_value_node 以及你是如何声明结构数组的吗?
标签: c struct linked-list