C中链表的总大小 [英] Total size of a linked list in C

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

问题描述

好的...我的CS数据结构简介太生锈了,我需要在这里问这个问题.

Alright... my Introduction to Data Structures from CS is so rusty I need to ask this here.

我有一个链表,其结构为:

I have a linked list whose structure is:

struct Data_Struct {
    char *Name;
    char *Task;
    char *Pos;
    struct Data_Struct *Next;
};
typedef struct Data_Struct MyData;

现在,在我的应用程序中的某个时候,我用数据填充了列表.

Now, at some point in my application I filled the list with data.

问题是,如何获取存储在其中的数据的总大小?有多少个字符?像

The question is, how do I get the total size of the data stored in there? How many chars are there? Something like

sizeof(MyData);

这将返回列表中存储的信息的大小.

That will return the size of the info stored in the list.

代码受到赞赏.

谢谢!

编辑:不幸的是,这不是家庭作业.我20多年前完成了学业,坦率地说,我从不需要在任何东西上使用链表.我只是不记得了.我正在做的是迭代列表,并获取每个元素的strlen()并将其保持在全局大小,但是我想知道是否有更好的方法.

EDIT: Unfortunately this is NOT homework. I finished school more than 20 years ago and frankly i never ever had to use linked lists on anything. I just don't remember. What I am doing is iterate the list and get the strlen() of each element and keep it on a global size but I wanted to know if there was a better way.

不,我不需要链接的点赞的大小(节点数),我只想知道其中存储了多少个字符.

and NO, I don't need the size of the linked likes (the count of nodes), i just want to know how many characters are stored in there.

谢谢

推荐答案

在计数的同时,您通常会遍历列表,直到到达末尾项目为止,代码应该是这样的:

You usually go through the list until you reach the tail item while counting in the meanwhile, code should be something like that:

int listLength(struct Data_Struct* item)
{
  struct Data_Struct* cur = item;
  int size = 0;

  while (cur != null)
  {
    ++size;
    cur = cur->Next;
  }

  return size;
}

请注意,此操作的复杂度与列表的大小呈线性关系,因此它是 O(n),并且效率很低.您可以将大小存储在某处,并使用列表插入和删除进行更新,以避免任何开销,并能够在恒定时间内 O(1)进行计算.

Mind that complexity of this operation is linear with the size of the list, so it's O(n) and it's quite inefficient. You could store size somewhere and update with list insertions and deletes to avoid any overhead and being able to calculate it in a constant time O(1).

没注意到您想要列表中包括的整个数据的大小.在您的情况下,您可以使用与计算长度相同的方法,但是为每个元素添加 1 则应添加字符串的总长度:

Didn't notice you wanted size of the whole data included into the list. In your case you can keep the same approach used for calculating the length but instead that adding 1 for every element you should add the total length of strings:

size += strlen(Name)+strlen(Task)+strlen(Pos);

请注意,由于如果 char * 类型的列表元素中的数据有效,则 Data_Struct 的有效大小仅为4个指针,这就是为什么需要使用支持功能的原因像 strlen 一样,否则您将无法获得字符串的 real 维度.

Mind that since data inside your list element if of type char* the effective size of the Data_Struct is just 4 pointers, that's why you need to use a support function like strlen, otherwise you can't get real dimension of the strings.

有什么区别?

sizeof(Data_Struct) == 16

因为 Data_Struct 类型包含4个指针,三个用于指向char的指针,一个用于列表中下一个元素的指针

because the Data_Struct type contains 4 pointers, three for pointers to char and one for the next element in the list

sizeof(Name) == sizeof(Task) == sizeof(Pos) == 4

因为这些变量的类型是 char的指针,所以它们是指针,没有具体值,通常为4个字节(我假设是32位体系结构)

because these variables are of type pointer to char, so they are pointer, no concrete value, and it's usually 4 bytes (I'm assuming a 32 bit architecture)

strlen(Name) == length in chars of the string

因为该函数完全可以计算字符串的长度.

because the function works exactly to calculate the length of a string.

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

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