fork download
  1. //Arquivo circDuplList.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct cdLst{
  6. int info;
  7. struct cdLst* ant;
  8. struct cdLst* prox;
  9. } cdLst;
  10.  
  11. /*Insert a node to the front of the circular list*/
  12. cdLst* insert_front(cdLst* cdl, int info) {
  13. cdLst* p = (cdLst*)malloc(sizeof(cdLst));
  14. p->info = info;
  15. if (cdl != NULL) {
  16. p->prox = cdl;
  17. p->ant = cdl->ant;
  18. p->ant->prox = p;
  19. p->prox->ant = p;
  20. } else {
  21. p->prox = p;
  22. p->ant = p;
  23. }
  24. cdl = p;
  25. return p;
  26. }
  27.  
  28. /*Print the list*/
  29. void cdl_print(cdLst* cdl) {
  30. printf("\n%d",cdl->info);
  31. for (cdLst *p = cdl->prox; p != cdl; p = p->prox) {
  32. printf("\n%d",p->info);
  33. }
  34. }
  35.  
  36. int main(void){
  37. cdLst* l = NULL;
  38. l = insert_front(l,3);
  39. l = insert_front(l,2);
  40. l = insert_front(l,3);
  41. l = insert_front(l,8);
  42. l = insert_front(l,65);
  43. l = insert_front(l,3);
  44. cdl_print(l);
  45. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
3
65
8
3
2
3