2015-10-17 65 views
0

我试图构建创建一个自动机状态的程序(他的符号+下一个状态)&显示状态链表(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:显示屏上的循环是由于非 - 为其他国家分配空间;我会更正添加到代码

感谢您的帮助

+0

scanf的,应该是'的scanf( “%d”,&j)'。这调用了UB。您的其他scanf电话有同样的问题 – amdixon

+0

感谢您的快速回复,我纠正了一个! 现在它停止工作后,第二个printf – Silversprint

+0

我仍然无法找到显示为什么有一个循环,添加功能的链接似乎是好的,所以在显示器中的条件,所以我不明白谢谢你帮助 – Silversprint

回答

2

有在你的代码一些错误。你还没有读过渡,州编号和其他变量。只需更正程序中的scanf的语法,其他一切都可以正常工作。在变量scanf之前添加一个&

+0

谢谢,我没有意识到我在所有的scanf中犯了同样的错误!它现在工作得很好:) – Silversprint

+0

你能重新看一下代码吗? – Silversprint

0

中添加更正此()函数:

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

说明:

  • 你首先要尝试分配新的使用状态的malloc():

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

请提供更多关于如何解决此问题的解释。这是一个质量很低的文章 – eliasah

+0

你首先必须尝试使用​​malloc()分配新状态,malloc()成功时返回一个指向已分配内存的指针,失败时返回NULL;因此它来'if(neww!= NULL){'来测试内存块是否被成功分配。并只在成功时写上该块。 – milevyo

0

注意:使用scanf()时字符可能会导致头痛!

当你通过一个构件的地址结构内的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; 
} 
+0

感谢您的建议! 这将是非常有用的下一代码。 – Silversprint