我试图构建创建一个自动机状态的程序(他的符号+下一个状态)&显示状态链表(C语言)为什么这不工作
因此,这里是我的代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct State
{
int state;
char symb;
int newState;
struct State *next;
}State;
State *head,*neww,*p;
void Add()
{
int i,j;
printf("How many transitions in your automata\n");
scanf("%d",&j);
for(i=0;i<j;i++)
{
if(neww!=NULL)
{
neww = (struct State *)malloc(sizeof (struct State));
printf("State number:");
scanf("%d",&neww->state);
printf("With which symbole to the next state:");
scanf(" %c",&neww->symb);
printf("To which state :");
scanf("%d",&neww->newState);
neww->next=NULL;
if(head==NULL)
{
head=neww;
}
else{
p = head;
while(p->next !=NULL)
{
p=p->next;
}
p->next = neww;
}
}
}
}
void Display()
{
p=head;
while(p != NULL)
{
printf("State : %d to state : %d with symbole : %c \n\n",p->state,p->newState,p->symb);
p = p->next;
}
printf("END\n");
}
int main(void)
{
head = NULL;
Add();
Display();
return 0;
}
你能帮我弄清楚为什么在第一次printf后停止工作吗?
EDIT1:纠正所有scanfs后,它工作得很好:现在改变的scanf( “%d”,j)至&Ĵ
EDIT2后第printf的后停止!
EDIT3:我加入了更正代码,现在我有它持续显示状态显示循环不停止我想这是一个链接问题
EDIT4:显示屏上的循环是由于非 - 为其他国家分配空间;我会更正添加到代码
感谢您的帮助
scanf的,应该是'的scanf( “%d”,&j)'。这调用了UB。您的其他scanf电话有同样的问题 – amdixon
感谢您的快速回复,我纠正了一个! 现在它停止工作后,第二个printf – Silversprint
我仍然无法找到显示为什么有一个循环,添加功能的链接似乎是好的,所以在显示器中的条件,所以我不明白谢谢你帮助 – Silversprint