ç通用链表 [英] C generic linked-list

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

问题描述

我有一个通用的链接列表,保存类型为void *我想填充我名单型结构员工数据,最终我想销毁对象结构的员工也是如此。

I have a generic linked-list that holds data of type void* I am trying to populate my list with type struct employee, eventually I would like to destruct the object struct employee as well.

考虑这个通用的链表头文件(我有一个char *类型测试过):

Consider this generic linked-list header file (i have tested it with type char*):

struct accListNode                 //the nodes of a linked-list for any data type
{
  void *data;                     //generic pointer to any data type
  struct accListNode *next;       //the next node in the list
};

struct accList                    //a linked-list consisting of accListNodes
{
  struct accListNode *head;
  struct accListNode *tail;
  int size;
};

void accList_allocate(struct accList *theList);           //allocate the accList and set to NULL
void appendToEnd(void *data, struct accList *theList);    //append data to the end of the accList
void removeData(void *data, struct accList *theList);         //removes data from accList
  --------------------------------------------------------------------------------------

考虑员工结构

struct employee 
{ 
   char name[20]; 
   float wageRate; 
} 

现在考虑这个示例测试用例将从调用的main():

Now consider this sample testcase that will be called from main():

    void test2()
    {
      struct accList secondList;
      struct employee *emp = Malloc(sizeof(struct employee));
      emp->name = "Dan";
      emp->wageRate =.5;

      struct employee *emp2 = Malloc(sizeof(struct employee));
      emp2->name = "Stan";
      emp2->wageRate = .3;

      accList_allocate(&secondList);
      appendToEnd(emp, &secondList);
      appendToEnd(emp2, &secondList);

      printf("Employee: %s\n", ((struct employee*)secondList.head->data)->name);   //cast to type struct employee
      printf("Employee2: %s\n", ((struct employee*)secondList.tail->data)->name);  
    }

为什么我贴在下面的回答解决我的问题?我相信这是与指针和内存分配。该函数malloc(),我用的是一个自定义的malloc来检查返回NULL。

Why does the answer that I posted below solve my problem? I believe it has something to do with pointers and memory allocation. The function Malloc() that i use is a custom malloc that checks for NULL being returned.

下面是我的整个通用链表实现的链接:<一个href=\"http://$c$creview.stackexchange.com/questions/13007/c-linked-list-implementation\">http://$c$creview.stackexchange.com/questions/13007/c-linked-list-implementation

Here is a link to my entire generic linked list implementation: http://codereview.stackexchange.com/questions/13007/c-linked-list-implementation

推荐答案

问题是这样的accList_allocate()和你对它的使用。

The problem is this accList_allocate() and your use of it.

struct accList secondList;
accList_allocate(&secondList);

在原来test2的()secondList是在栈上的内存。 &安培; secondList是一个指向该内存。当你调用accList_allocate()指针的副本在栈内存指着过去了。 malloc()函数,然后返回一个内存块并将其分配给指针的副本,而不是原来的secondList。

In the original test2() secondList is memory on the stack. &secondList is a pointer to that memory. When you call accList_allocate() a copy of the pointer is passed in pointing at the stack memory. Malloc() then returns a chunk of memory and assigns it to the copy of the pointer, not the original secondList.

即将退了出去,secondList仍以未初始化的内存堆栈上指向所以调用appendToEnd()失败。

Coming back out, secondList is still pointing at uninitialised memory on the stack so the call to appendToEnd() fails.

情况与此相同的答案,除了secondList恰好是免费的垃圾。也许是偶然,可能是由编译器的设计。无论哪种方式,是不是你应该依靠。

The same happens with the answer except secondList just happens to be free of junk. Possibly by chance, possibly by design of the compiler. Either way it is not something you should rely on.

或者:

struct accList *secondList = NULL;

accList_allocate(&secondList);

和改变accList_allocate()

And change accList_allocate()

accList_allocate(struct accList **theList) {
    *theList = Malloc(sizeof(struct accList));
    (*theList)->head = NULL;
    (*theList)->tail = NULL;
    (*theList)->size = 0;
}

struct accList secondList;

accList_initialise(secondList);

使用accList_allocate()改为accList_initialise(),因为它没有分配

With accList_allocate() changed to accList_initialise() because it does not allocate

accList_initialise(struct accList *theList) {
    theList->head = NULL;
    theList->tail = NULL;
    theList->size = 0;
}

这篇关于ç通用链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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