fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct node{
  4. int val;
  5. struct node* prev;
  6. struct node* next;
  7. }Node;
  8.  
  9. typedef struct list{
  10. Node* head;
  11. Node* tail;
  12. }List;
  13.  
  14. void pushFront(List* l, Node* node){
  15. if(l->head == NULL){
  16. l->head = node;
  17. l->tail = node;
  18. l->tail->next = NULL;
  19. }else{
  20. l->head->prev = node;
  21. node->next = l->head;
  22. l->head = node;
  23. }
  24. }
  25. void printList(List* list){
  26.  
  27. Node *ptr = list->head;
  28. while(ptr != NULL){
  29. printf("%i ",ptr->val);
  30. ptr = ptr->next;
  31. }
  32. puts("");
  33. }
  34.  
  35. void reverse(List* lista){
  36.  
  37. Node* ptr = lista->head;
  38. Node* temp = NULL;
  39. while(ptr != NULL){
  40. temp = ptr->prev;
  41. ptr->prev = ptr->next;
  42. ptr->next = temp;
  43. ptr = ptr->prev;
  44. }
  45.  
  46. if(temp != NULL)
  47. lista->head = temp->prev;
  48. }
  49.  
  50. int main(void) {
  51. List list = { NULL, NULL };
  52. Node nodeArr[7];
  53. int i;
  54.  
  55. for( i = 0; i < 7; i++ )
  56. {
  57. nodeArr[i].val = 7 - i;
  58. nodeArr[i].prev = NULL;
  59. nodeArr[i].next = NULL;
  60. pushFront(&list, &nodeArr[i]);
  61. }
  62.  
  63. printList(&list);
  64. reverse(&list);
  65. printList(&list);
  66.  
  67. // your code goes here
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 
7 6 5 4 3 2 1