【问题标题】:Linkeds List using strings使用字符串的链表
【发布时间】:2017-04-05 15:08:53
【问题描述】:

当我插入到我的链接列表时,我得到一个段错误,我就是找不到它。在某一时刻,它最多允许我插入三个节点,但现在它在第一次插入后会出现段错误,并且不会遍历 while 循环和 for 语句。我是否错误地将信息扫描到阵列中?我必须能够接受多个输入值,因为最终我需要能够根据用户给出的字符串和计数值来删除和打印节点。

来自用户的输入如下所示:

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");
    }
}

【问题讨论】:

  • val 未分配 int 指针,但您正在尝试将 scanf 插入其中一些字符串...

标签: c struct linked-list


【解决方案1】:
if ((temp=(struct node *)malloc(sizeof(struct node)))==NULL)
{
    printf("Memory allocation or node failed.\n");
    exit(1);
}
strcpy(temp->symbol,str);

您需要为temp-&gt;symbol 分配内存。由于您要复制现有字符串,因此最简单的方法是像这样使用 strdup 而不是这样做 strcpy

temp->symbol=strdup(str);

【讨论】:

    【解决方案2】:

    您还需要为变量符号分配内存。 使您的插入功能如下:

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

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2016-07-23
      • 2011-05-19
      • 1970-01-01
      相关资源
      最近更新 更多