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

时间:2015-10-17 10:57:16

标签: c linked-list

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

所以这是我的代码:

#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;
}

请你帮我弄清楚为什么它会在第一次打印后停止工作?

EDIT1:现在在将scanf(&#34;%d&#34;,j)更改为&amp; j

后,在第二次printf后停止

EDIT2:纠正所有scanfs后,它运行正常!

EDIT3:我添加了对代码的更正,现在我在显示器中有一个循环,它一直显示状态而不停止我猜它是一个链接问题

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

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

您的代码中几乎没有错误。您尚未阅读转换,状态编号和其他变量。只需通过程序更正scanf的语法,其他一切都可以正常工作。在&中的变量前添加scanf

答案 1 :(得分:0)

在add()函数中更正此:

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

说明:

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

    • 成功时,malloc()返回指向已分配内存的指针
    • 失败时返回NULL;
  • 因此,如果(neww!= NULL){}来测试是否已成功分配内存块。
  • 仅在成功时写在该块上。

答案 2 :(得分:0)

Remarque:使用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;
}