c中的链表(如何在另一个结构中调用struct的成员) [英] linked list in c (how to call a member of struct in another struct)

查看:66
本文介绍了c中的链表(如何在另一个结构中调用struct的成员)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i我正在尝试制作一个链表代码库,这是我到目前为止所做的,



< pre lang =c ++> struct 节点
{
int32 data; // store data informatino
struct node * next; // 对下一个节点的引用
};

typedef struct
{
struct node * head;
int count;
} llist;

void list_add(llist * list,int32 element)
{
unsigned int32 count;
struct node * temp;
struct node * new = null;
new = malloc( sizeof struct node));

new - > data = element;
new - > next = null;
if (list - > head == null)
list - > head = new ;
else
{
temp = list - >头;
while (temp!= null)
{
count ++;
temp = temp - >下一个;
}
temp - > next = new ;
}
}

int32 list_sum(llist * list)
{
int32 total;
struct node * temp;

temp = list - >头;
while (temp!= null)
{
temp = temp - >下一个;
总计+ = temp - >数据;
}
返回总计;
}





i有2个问题,当我有一个结构与另一个结构的成员,比如a(b,x, y),b(x,y)

如何调用它(例如)(abx)



我也想得到评论到我的编码,



提前致谢,

z3ngew

解决方案

你的代码包含一些明显的故障。

  struct  node *  = null; 
new = malloc( sizeof struct node));



这是用安全编程过度的。如果要在下一个语句中分配它,则不需要初始化 new 指针。



此外,名称 new 不是一个好选择,因为它是C ++中的关键字。一段时间后你想要升级到C ++并发现你必须编辑很多地方。

 temp = list  - >头; 
while (temp!= null)
{
count ++;
temp = temp - >下一个;
}
temp - > next = new ;



count在其他任何地方都没有使用,所以你可以扔掉它。而且你实际上从不使用你的llist结构的count成员。



while循环运行一次迭代太多了。它运行直到temp为NULL然后你尝试分配给temp-> next。这是行不通的。



最后:

 temp = list  - >头; 
while (temp!= null)
{
temp = temp - >下一个;
总计+ = temp - >数据;
}



您需要在循环内交换两个语句。您想首先访问temp->数据,然后转到下一个节点。



您的第一个问题:



如果你有

  struct  A 
{
< span class =code-keyword> int x;
int y;
};

struct B
{
struct A aaa ;
int z;
};



pB是指向B的指针,然后您将通过以下方式访问x:pB-> aaa.x。


Hello everyone,
i am trying to make a linked list code library, here is what i have done so far,

struct node
{
   int32 data;   //store data informatino
   struct node *next;   //reference to next node
};

typedef struct
{
   struct node *head;
   int count;
}llist;

void list_add(llist *list, int32 element)
{
   unsigned int32 count;
   struct node* temp;
   struct node *new = null;
   new = malloc(sizeof(struct node));
   
   new -> data = element;
   new -> next = null;
   if(list -> head == null)
      list -> head = new;
   else
   {
      temp = list -> head;
      while(temp != null)
      {
         count++;
         temp = temp -> next;
      }
      temp -> next = new;
   }
}

int32 list_sum(llist *list)
{
   int32 total;
   struct node* temp;
   
   temp = list -> head;
   while(temp != null)
   {
      temp = temp -> next;
      total += temp -> data;
   }
   return total;
}



i have 2 question when i have a struct with a member of another struct, say a(b,x,y), b(x,y)
how to call it for example (a.b.x)

and also i would like to get review to my coding,

Thanks in advance,
z3ngew

解决方案

Your code contains some obvious glitches.

struct node *new = null;
new = malloc(sizeof(struct node));


That is overdoing it with "safe" programming. You don't need to initialize the new pointer if you are going to assign to it in the very next statement.

Besides, the name new is not a good choice, because it is a keyword in C++. Some time later you want to upgrade to C++ and find that you have to edit a lot of places.

temp = list -> head;
 while(temp != null)
 {
    count++;
    temp = temp -> next;
 }
 temp -> next = new;


count is not used anywhere else, so you can throw it out. And you actually never use the count member of your llist structure.

The while loop runs one iteration too much. It runs until temp is NULL and then you try to assign to temp->next. That won't work.

And finally:

temp = list -> head;
 while(temp != null)
 {
    temp = temp -> next;
    total += temp -> data;
 }


You need to exchange the two statements inside the loop. You want to first access temp->data and then move on to the next node.

Your first question:

If you have

struct A
{
   int x;
   int y;
};

struct B
{
   struct A aaa;
   int z;
};


And pB is a pointer to a B, then you would access x by: pB->aaa.x.


这篇关于c中的链表(如何在另一个结构中调用struct的成员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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