链表(在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 automatan");
  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 nn",p->state,p->newState,p->symb);
        p = p->next;
    }
    printf("ENDn");
}
int main(void)
{
    head = NULL;
    Add();
    Display();
    return 0;
}

你能帮我弄清楚为什么它在第一次打印后就停止工作了吗?

EDIT1:在将scanf("%d",j)更改为&j后,现在在第二次打印后停止

EDIT2:在纠正了所有的扫描后,它工作得很好!

EDIT3:我添加了更正代码,现在我有一个循环在显示它保持显示状态不停止,我猜这是一个链接问题

EDIT4:显示中的循环是由于没有为其他状态分配空间;我将把更正添加到代码

Thanks for the help

您的代码中几乎没有错误。您没有读取转换,状态数和其他变量。只要在整个程序中纠正scanf的语法,其他一切都会正常工作。在scanf变量前添加&

在add()函数中纠正这个错误:

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

解释:

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

      当malloc()方法成功时,返回一个指向已分配内存的指针,
  • 失败时返回NULL;
因此,if(neww!=NULL){}来测试内存块是否已成功分配。
  • 只在成功时写入该块。
  • 注意:char在使用scanf()时可能会引起头痛!

    当将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("nnLISTn");
        while(p != NULL)
        {
            printf("State : %d to state : %d with symbole : %c nn",p->state,p->newState,p->symb);
            p = p->next;
        }
        printf("ENDn");
    }
    /*_______________________________________________________________
    */
    int main(void){
        Add();
        Display();
        return 0;
    }
    

    相关内容

    • 没有找到相关文章

    最新更新