reverse link list

来源:互联网 发布:爱奇艺出品的网络剧 编辑:程序博客网 时间:2024/06/08 17:37
#include <stdio.h>


typedef struct _note{
  int data;
  struct _note *next;
} Note;


Note* reverseList(Note *head)

  Note *current, *prev, *next;
  
  current = head;
  prev = NULL;
  
  while(current != NULL)
  {
    next = current->next;
    current->next = prev;
    
    prev = current;
    current = next;
  }
  head = prev;
  
  return head;
}


Note *insert(Note *head, int data)
{
  Note *note = (Note *)malloc(sizeof(Note));
  if(note != NULL)
  {
    note->data = data;
    note->next = head;
    head = note;
  }
  return head;
}


void print(Note *head)
{  
  Note *p = head;
  while(p != NULL)
  {
    printf("%d\n", p->data);
    p = p->next;
  }
}






// To execute C, please define "int main()"


int main() {
  Note *head = NULL;
  head = insert(head, 4);
  head = insert(head, 5);
  head = insert(head, 6);
  head = insert(head, 7);
  head = insert(head, 8);
  
  print(head);
  
  head = reverseList(head);


  print(head);
  return 0;
}
0 0