C 数据结构、指针
我使用指针在C中创建了一个队列,我的代码可以工作,但我无法理解指针变量rear1是如何工作的,因为每次调用函数时,rear1都会初始化,并且与front相同,front第一次存储start的地址,然后在front重新初始化后,它仍然保留start地址,这是怎么可能的C 数据结构、指针,c,pointers,queue,structure,C,Pointers,Queue,Structure,我使用指针在C中创建了一个队列,我的代码可以工作,但我无法理解指针变量rear1是如何工作的,因为每次调用函数时,rear1都会初始化,并且与front相同,front第一次存储start的地址,然后在front重新初始化后,它仍然保留start地址,这是怎么可能的 #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own get
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, ("pause") or input loop */
struct node
{
int data;
struct node *next;
};
enqueue(struct node **start)
{
struct node *front,*rear;
if (*start==NULL)
{
*start=(struct node *)malloc(sizeof(struct node));
scanf("%d",&(*start)->data);
(*start)->next=NULL;
printf("%s","hello");
front=(*start);
rear=*start;
}
else
{
printf("%d",front->data);
struct node *temp,*curr;
curr=(struct node *)malloc(sizeof(struct node));
rear->next=curr;
rear=curr;
rear->next=NULL;
scanf("%d",&rear->data);
}
}
dequeue(struct node **front)
{
struct node *temp;
temp=(*front);
(*front)=(*front)->next;
printf("%d",(temp->data));
free(temp);
}
int main(int argc, char *argv[])
{
struct node *start=NULL;
enqueue(&start);
enqueue(&start);
enqueue(&start);
enqueue(&start);
enqueue(&start);
enqueue(&start);
dequeue(&start);
printf("\n");
dequeue(&start);
printf("\n");
dequeue(&start);
printf("\n");
dequeue(&start);
printf("\n");
dequeue(&start);
printf("\n");
dequeue(&start);
return 0;
}
#包括
#包括
/*使用控制台暂停器运行此程序,或添加您自己的getch(“暂停”)或输入循环*/
结构节点
{
int数据;
结构节点*下一步;
};
排队(结构节点**开始)
{
结构节点*前,*后;
如果(*start==NULL)
{
*start=(结构节点*)malloc(sizeof(结构节点));
scanf(“%d”和(*start)->数据);
(*start)->next=NULL;
printf(“%s”、“hello”);
前=(*开始);
后=*启动;
}
其他的
{
printf(“%d”,前->数据);
结构节点*temp,*curr;
curr=(结构节点*)malloc(sizeof(结构节点));
后->下一步=当前;
后=电流;
后->下一步=空;
scanf(“%d”,&后->数据);
}
}
出列(结构节点**前端)
{
结构节点*temp;
温度=(*前);
(*前)=(*前)->下一步;
printf(“%d”,(临时->数据));
免费(临时);
}
int main(int argc,char*argv[])
{
结构节点*start=NULL;
排队(&start);
排队(&start);
排队(&start);
排队(&start);
排队(&start);
排队(&start);
退出队列(&start);
printf(“\n”);
退出队列(&start);
printf(“\n”);
退出队列(&start);
printf(“\n”);
退出队列(&start);
printf(“\n”);
退出队列(&start);
printf(“\n”);
退出队列(&start);
返回0;
}
实际上,此代码不应该工作,或者具有未定义的行为
enqueue(struct node **start)
{
struct node *front,*rear;
if (*start==NULL)
{
*start=(struct node *)malloc(sizeof(struct node));
scanf("%d",&(*start)->data);
(*start)->next=NULL;
printf("%s","hello");
front=(*start);
rear=*start;
}
else
{
printf("%d",front->data);
struct node *temp,*curr;
curr=(struct node *)malloc(sizeof(struct node));
rear->next=curr;
rear=curr;
rear->next=NULL;
scanf("%d",&rear->data);
}
}
这里,当定义了*start时,您将curr
分配给rear->next
,但rear
未定义。您有两种解决方案:
- 使用rear作为静态变量(这显然不是一个好的解决方案,特别是如果您想使用多个队列)
- 在主结构中使用2个struct,
和Start
End
我认为您使用
front
和reast
时,就好像它们是静态的一样,但是默认情况下,变量“消失”在声明它的函数的末尾。在每次调用函数时,它都是一个新的未定义变量。首先,请尝试重新格式化代码,使其可读。如果使用正确的标点符号,或者使用多个句子,您的问题会更清楚。请重新表述您的问题,我(可能还有大多数其他读者)不明白问题出在哪里。要继续你的计划根本不起作用。使用未初始化的局部变量时,enqueue
函数至少包含一种未定义行为。在第一次调用之后,您并没有将任何东西真正链接到队列中。请正确格式化您的代码(例如,在您的C教科书中这样做的方式)。