创建一个在C单向链表 [英] Creating a singly linked list in C

查看:161
本文介绍了创建一个在C单向链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建从一个分配输入文本文件中的单向链表。我试图做一点点在一个时间,所以我知道我的code是不完整的。我试图创建头指针和刚打印出它的价值,我甚至不能得到工作,但我不知道为什么。我包括结构,我创建列表,打印列表等功能。我不包括打开的文件,因为这部分工作。

  typedef结构列表
{
   结构列表*接下来的; / *指针指向下一个链表节点* /
   字符*海峡; / *指针psented字符串重新$ P $ * /
   诠释计数; / *此字符串出现的#* /
}清单;LIST * CreateList(FILE * FP)
{
    字符输入[LINE_LEN]
    LIST *根; / *包含列表的根* /
    为size_t了strsize;
    LIST * newList; / *用于分配新的列表成员* /    而(的fscanf(FP,BUFFMT的s,输入)!= EOF){        了strsize = strlen的(输入)+ 1;        / *创建根节点,如果没有当前根节点* /
        如果(根== NULL){
            如果((newList =(LIST *)malloc的(的sizeof(LIST)))== NULL){
                的printf(内存不足...);
                出口(EXIT_FAILURE);
            }
            如果((的char *)malloc的(的sizeof(了strsize))== NULL){
                的printf(没有足够的内存%S,输入);
                出口(EXIT_FAILURE);
            }
                的memcpy(newList-> STR,输入了strsize); / *复制串* /
                newList->计数= START_COUNT;
                newList->接着= NULL;
                根= newList;
        }
    }
        返回根;
}/ *打印新力链表并返回头指针* /
LIST *的printList(常量LIST *头)
{
    诠释计数;    为(计数= 1;头= NULL;!=头部流浆>接下来,头部++){
        的printf(%s%D,流浆> STR,流浆>计数);
    }
    返回头; / *,这实际上返回头PTR开始,B / C我想
                            返回头PTR的开始。 * /
}


解决方案

有一个未定义的值,所以它不会初始化。第二行 CreateList

  LIST *根= NULL;

另外,还向下有明显的项目的详细信息分配,但一)code未能捕获分配,并保存它的任何地方,以及b)的分配的大小应了strsize ,不是变量本身的长度。有几种方法来解决这个问题,但最直接的是:

  newList-> STR =(字符*)malloc的(了strsize);
如果(newList-> STR == NULL)

I'm trying to create a singly linked list from an input text file for an assignment. I'm trying to do it a little bit at a time so I know my code is not complete. I tried creating the head pointer and just printing out its value and I can't even get that to work, but I'm not sure why. I included the struct, my create list, and print list functions. I didn't include the open file since that part works.

typedef struct List
{
   struct List *next;   /* pointer to the next list node */
   char *str;           /* pointer to the string represented */
   int count;           /* # of occurrences of this string */
} LIST;

LIST *CreateList(FILE *fp) 
{
    char input[LINE_LEN];
    LIST *root;             /* contains root of list             */
    size_t strSize;         
    LIST *newList;          /* used to allocate new list members */

    while (fscanf(fp, BUFFMT"s", input) != EOF) {

        strSize = strlen(input) + 1;

        /* create root node if no current root node */
        if (root == NULL) {
            if ((newList = (LIST *)malloc(sizeof(LIST))) == NULL) {
                printf("Out of memory...");
                exit(EXIT_FAILURE);
            } 
            if ((char *)malloc(sizeof(strSize)) == NULL) {
                printf("Not enough memory for %s", input);
                exit(EXIT_FAILURE);
            }
                memcpy(newList->str, input, strSize);   /*copy string    */
                newList->count = START_COUNT;
                newList->next = NULL;
                root = newList;
        }
    }
        return root;
}

/* Prints sinly linked list and returns head pointer */
LIST *PrintList(const LIST *head) 
{
    int count;

    for (count = 1; head != NULL; head = head->next, head++) {
        printf("%s    %d", head->str, head->count);
    }                       
    return head;     /* does this actually return the start of head ptr, b/c I want to 
                            return the start of the head ptr. */
}

解决方案

root has an undefined value, so it won't initialize. The second line of CreateList should be

LIST *root = NULL;

Also, further down there is allocation apparently for the details of the item, but a) the code fails to capture the allocation and save it anywhere, and b) the size of the allocation should be strSize, not the length of the variable itself. There are several ways to fix it, but the most straightforward would be:

newList->str = (char *)malloc(strSize);
if (newList->str == NULL)

这篇关于创建一个在C单向链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆