链接列表(在 C 中)为什么这不起作用

Linked list ( in C ) why does this not work

我正在尝试构建一个程序来创建自动机状态(他的符号 + 下一个状态)并显示状态

所以这是我的代码:

#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:现在它在将 scanf("%d",j) 更改为 &j

后在第二次 printf 后停止

EDIT2:在更正所有 scanfs 后它工作正常!

EDIT3:我在代码中添加了更正,现在我在显示器中有一个循环,它不停地显示状态我猜这是一个 link 问题

EDIT4:显示中的循环是由于未为其他状态分配 space;我会将更正添加到代码

感谢帮助

您的代码几乎没有错误。您还没有阅读转换、状态编号和其他变量。只需在整个程序中更正 scanf 的语法,其他一切都会正常工作。在scanf.

中的变量前加一个&

在 add() 函数中更正此问题:

neww = malloc(sizeof (struct State)); // alloc
if(neww!=NULL){ // then test

解释:

  • 您首先必须尝试使用​​ malloc() 分配新状态:

    • 成功时,malloc() returns 指向已分配内存的指针,
    • 失败时 returns NULL;
  • 于是就有了if(neww!=NULL){ }来测试这块内存是否分配成功。
  • 仅在成功时写入该块。

注意:使用 scanf() 时 char 可能会引起头痛!

当您将 struct 中的成员地址传递给 scanf 时,请考虑用方括号括起该成员,如下所示:

scanf("%d",&(neww->state));

代码:

#include<stdio.h>
#include<stdlib.h>


typedef struct State State;
struct State
{
    int     state;
    char    symb; // <- char is problematic with scanf
    int     newState;
    State   *next;
};

State *head=NULL, // !MPORTANT: Initialized to NULL 
      *p;
/*_______________________________________________________________
*/
void Add(void){
    int i,j;
    State *neww;

    printf("\nHow many transitions in your automata? ");
    scanf("%d",&j);

    for(i=0;i<j;i++){
        //use calloc to initialize memory to 0
        neww = calloc(1,sizeof (struct State));
        if(neww!=NULL){ // only if calloc() succeded

            printf("State number: ");
            scanf("%d",&(neww->state)); // the addres of state not neww

            printf("With which symbole to the next state: ");
            scanf(" %c",&(neww->symb)); // idem @ of (symb)


            printf("To which state: ");
            scanf("%d",&(neww->newState)); // and idem @ of (newState)

            //neww->next=NULL; already been initialized by calloc

            if(head==NULL){
                head=neww;
            }else{
                p = head;
                while(p->next !=NULL){
                    p=p->next;
                }
                p->next = neww;
            }
        }else{
            perror("no enough memory");
            exit(1);
        }
    }
}
/*_______________________________________________________________
*/
void Display(void){

    p=head;
    printf("\n\nLIST\n");
    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){
    Add();
    Display();
    return 0;
}