简体   繁体   English

通报链表

[英]Circular LinkedList

I've little problem with circular linkedlist : I want to insert an element (integer) at the end. 我对循环链表没有什么问题:我想在最后插入一个元素(整数)。 My function add the element at the end but my linkedlist isn't really circular (I have this impression). 我的函数在最后添加了元素,但是我的链表并不是真正的循环(我有这种印象)。 this is .h with the structure I use: 这是.h,我使用的结构是:

struct liste_circulaire {
  int val;
  struct liste_circulaire *suivant; /* suivant = next element */
};

typedef struct liste_circulaire liste; typedef struct liste_circulaire liste;

    int main(void) {
  liste *l, *deb;
  deb = (liste *)malloc(sizeof(liste));
  l = deb;
  l -> suivant = deb;  /* suivant = next element */
  ajouter_element(l,0);
  ajouter_element(l,1);
  ajouter_element(l,2);
  ajouter_element(l,3);
  affiche(l,l->suivant);
  printf("%d\n",l->suivant->suivant->suivant->suivant->suivant->val);
  return 0;
}
void ajouter_element(liste *l,int x) {  
  liste *deb = l;  /* with this line, I have a pointeur on the first element of my list */
    while(l->suivant != deb) {    
      l = l -> suivant;
    }
  l -> suivant = (liste *)malloc(sizeof(liste));
  l = l -> suivant;
  l -> val = x;  
  l -> suivant = deb;
}

void affiche(liste *l,liste *deb) {
  if(l == deb) {
    printf(" Fin\n");
    return;
  }
  printf(" %d -->",deb->val);
  affiche(l,deb->suivant);
}

In my main, I have random number when I ask to print the first element of my list but in my example, the first value of my list in 0. So if anyone can to help me (I hope to be clear, I'm french student) Thank's ! 在我的主体中,当我要求打印列表的第一个元素时,我有一个随机数,但在我的示例中,列表的第一个值是0。所以,如果有人可以帮助我(我希望我很清楚,法国学生)谢谢!

Your program is printing a random number because of this: 由于以下原因,您的程序正在打印一个随机数:

liste *l, *deb;
deb = (liste *)malloc(sizeof(liste));
l = deb;
l -> suivant = deb; 

The first element in your list, is a node that you are allocating correctly but you are not giving any number. 列表中的第一个元素是您已正确分配但未提供任何数字的节点。 You should try adding: 您应该尝试添加:

l -> val = 0; 

And getting rid of: 并摆脱:

ajouter_element(l,0);

This should fix your problem. 这应该可以解决您的问题。

you can use the predefined header file list.h for circular linked list: 您可以将预定义的头文件list.h用于循环链表:

the following link contains an example of how to do it. 以下链接包含有关操作方法的示例

the list.h contains all functions related to the management of a circular linked list like definition, add in the head , add in the tail , remove, foreach function to browse the circular linked list... list.h包含与管理循环链表有关的所有功能,例如定义,在头部添加,在尾部添加,删除,foreach功能浏览循环链表...

Note that you never set the value of the first element. 请注意,您永远不会设置第一个元素的值。 (Hernan just beat me here...) (河南只是在这里打败我...)

When you call ajouter_element() it not only adds the new element to the end of the list, it sets the beginning of the list to be the new element: 当您调用ajouter_element()时,它不仅将新元素添加到列表的末尾,还将列表的开头设置为新元素:

l = l -> suivant;

affiche() prints the second element first and the first element last. affiche()打印第二个元素,最后打印第一个元素。 The first to be printed is deb->val which in main would have been l->suivant->val . 第一个要打印的是deb->val ,而它主要是l->suivant->val


If you want affiche() to print the first item... print it! 如果要让affiche()打印第一项,请打印! :] :]

void affiche(liste *l,liste *deb) {
  if (deb == 0)
  {
    deb = l;
  }

  printf(" %d -->",l->val);

  if(l->suivant == deb) {
    printf(" Fin\n");
    return;
  }

  affiche(l->suivant,deb);
}

Call this with: 通过以下方式调用:

affiche(l,0);

or 要么

affiche(l,l);

they'll both do the same thing. 他们都会做同样的事情。 You could also overload it to take only the single value. 您也可以重载它以仅使用单个值。

I haven't run this (or even compiled), but hopefully it's reasonably close. 我没有运行过(甚至编译过),但希望它已经相当接近了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM