2017-04-05 103 views
0

當我插入到鏈接列表中時,出現段錯誤,我無法找到它。在某一點上,它允許我插入最多三個節點,但現在它在第一次插入後發生段錯誤,並且不會遍歷while循環和語句。我是否將信息錯誤地掃描到數組中?我必須能夠接受多個輸入值,因爲最終我需要能夠根據用戶給出的字符串和計數值來刪除和打印節點。從用戶使用字符串的鏈接列表

輸入應該是這樣的:

ins books 
ins table 
prl // to print list 
del v1 v5 //to delete nodes with count values that fall between 1 and 5 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct node 
{ 
    char *symbol;// each Node contains a array 'symbol' 
    int count; // each Node contains a symbol counter 
    struct node *next; 
    };// end struct 



void insert(struct node**,struct node **, char *str); 
void printL(struct node *); 




int main() 
{ 

    struct node *head; 
    struct node *tail; 

    head = NULL; 
    tail = NULL; 


    //Declare variables needed for input and output 
    char input[15]={0}; 
    char cmd [4]={0}; 
    char info[11] = {0}; 
    int *val={0}; 


    //possible command strings 
    char ins[]= "ins"; 
    char prl[]= "prl"; 
    char end[]= "end"; 


    // Prompt user for command and corresponding input 
    puts("Please enter a command with corresponding value(s) where necessary"); 
    scanf("%s%s%s", cmd,info, val); 


//While command is not 'end': 
while (strcmp(end,cmd) != 0){ 
     // Read value(s) for the command, in necessary 
     if (strcmp(ins,cmd)==0) 
     { 
      insert(&head, &tail, info); 
     } 

     if (strcmp(prl, cmd)==0) 
     { 
      printL(head); 
     } 


     puts("Please enter your next command and value where necessary:"); 

     scanf("%s%s%s", cmd,info, val); 




    } 
    return 0; 
} 
void insert(struct node **h, struct node **t, char * str) 
{ 

    struct node *temp; 

    if ((temp=(struct node *)malloc(sizeof(struct node)))==NULL) 
    { 
     printf("Memory allocation or node failed.\n"); 
     exit(1); 
    } 
    strcpy(temp->symbol,str); 
    temp->count= 1; 
    temp->next=NULL; 

    if(*h == NULL) 
    { 
     *h=*t=temp; 
    } 

    else 
    { 
    (*t)->next = temp; 
    *t = (*t)->next; 
    } 
} 

void printL(struct node *h) 
{ 
    // NodePtr hPtr = NULL; 
    //hPtr=malloc(sizeof(Node)); 
    //hPtr=head; 
    if(h == NULL){ 
     puts("The list is empty"); 
    } 
    else{ 

     while(h != NULL){ 
      printf("%s", h->symbol); 
      printf("\t %d", h->count); 
      h= h->next; 
     } 
     printf("\n"); 
    } 
} 
+2

'val'未分配'int'指針,但你嘗試'scanf'到它的一些串... –

回答

0
if ((temp=(struct node *)malloc(sizeof(struct node)))==NULL) 
{ 
    printf("Memory allocation or node failed.\n"); 
    exit(1); 
} 
strcpy(temp->symbol,str); 

您需要爲temp->symbol分配內存。因爲你複製現有字符串的最簡單的方法是使用strdup這樣,而不是那樣做strcpy

temp->symbol=strdup(str); 
0

您還需要爲變量符號分配內存。 讓你插入功能如下:

void insert(struct node **h, struct node **t, char * str) 
{ 

struct node *temp; 

if ((temp=(struct node *)malloc(sizeof(struct node)))==NULL) 
{ 
    printf("Memory allocation or node failed.\n"); 
    exit(1); 
} 
int count = 0; 
count=strlen(str); 
temp->symbol=malloc(count+1); 
strcpy(temp->symbol,str); 
temp->count= 1; 
temp->next=NULL; 

if(*h == NULL) 
{ 
    *h=*t=temp; 
} 

else 
{ 
(*t)->next = temp; 
*t = (*t)->next; 
} 
}