• C 链表实现


    #include<stdio.h>
    #include<stdlib.h>
    #define  show_func_info()  printf ("------------%s----------\n", __func__);
    typedef struct node
    {
      int data;
      struct node *next;
    } Node;
     
    int delete_node (Node * head, int data);
    void show_linklist (Node * const head);
    void clear_linklist (Node * head);
    Node *init_linklist ();
    int insertafter_linklist (Node * const *head, int data);
    int insert_linklist (Node * const *head, int data);
    //反转链表
    Node **reverse_linklist (Node * *head);
    //获取链表程度
    int getlen_linklist (Node * const head);
    int
    main (int argc, char *argv[])
    {
     
      Node *const head = init_linklist ();
      printf ("head(%p)\n", head);
      int data = 0;
      while (scanf ("%d", &data) == 1)
        {
          printf ("current data=%d\n", data);
          insertafter_linklist (&head, data);
        }
      show_linklist (head);
      //清除缓冲区
      //方法1
      while ((getchar ()) == '\n');
     
      printf ("insert node\n");
     
      while (scanf ("%d", &data) == 1)
        {
          printf ("insert data=%d\n", data);
          insert_linklist (&head, data);
     
        }
      show_linklist (head);
      Node *const newhead = *reverse_linklist (&head);
      show_linklist (newhead);
      clear_linklist (head);
      clear_linklist (newhead);
      //show_linklist(head);
     
      return 0;
    }
     
    int
    delete_node (Node * head, int data)
    {
      return 0;
    }
     
    void
    clear_linklist (Node * const head)
    {
      Node *current = head;
      printf ("------------%s----------\n", __func__);
      while (current->next != NULL)
        {
     
     
          printf ("free(%p)\n", current);
          current = current->next;
          free (current);
        }
     
    }
     
    void
    show_linklist (Node * const head)
    {
      Node *current = head;
      printf ("------------%s----------\n", __func__);
      printf ("head(%p)\n", head);
      while (current->next != NULL)
        {
     
          current = current->next;
          printf ("address:%p  data:%d\n", current, current->data);
        }
     
    }
     
    Node *
    init_linklist ()
    {
      printf ("------------%s----------\n", __func__);
      //head 指向的内容不能修改
      Node *head = (Node *) malloc (sizeof (Node));
      printf ("head(%p)\n", head);
      return head;
     
    }
     
    int
    insertafter_linklist (Node * const *head, int data)
    {
      printf ("------------%s----------\n", __func__);
      Node *current = *head;
      while (current->next != NULL)
        {
          current = current->next;
        }
      Node *next = (Node *) malloc (sizeof (Node));
      next->data = data;
      current->next = next;
    }
     
    int
    insert_linklist (Node * const *head, int data)
    {
      int result = 0;
      printf ("------------%s----------\n", __func__);
      //当前节点
      Node *current = *head;
      //当前节点的前一个节点
      Node *before = NULL;
      while (current->next != NULL)
        {
          before = current;
          current = current->next;
          if (current->data >= data)
        {
          result = 1;
          break;
        }
        }
      Node *newnode = (Node *) malloc (sizeof (Node));
      newnode->data = data;
      if (result == 1)
        {
          printf ("current address:%p  data:%d\n", current, current->data);
          printf ("before address:%p  data:%d\n", before, before->data);
     
          before->next = newnode;
          newnode->next = current;
        }
      else
        {
          current->next = newnode;
        }
      return result;
    }
     
    Node **
    reverse_linklist (Node * *head)
    {
      printf ("------------%s----------\n", __func__);
      //当前节点
      Node *current = *head;
      Node *before = NULL;
      Node *const oldhead = *head;
      int len = getlen_linklist (*head);
      printf ("len(linklist)=%d\n", len);
      Node **arrayPtr = malloc (sizeof (Node **) * len);
      int i = 0;
      printf ("fill arrayPtr\n");
      while (current->next != NULL)
        {
          before = current;
          current = current->next;
          *(arrayPtr + i) = current;
          printf ("arrayPtr+%d:%p  current:%p\n", i, arrayPtr + i, current);
          i++;
     
        }
      int currentIndex = 0;
      current = oldhead;
      Node *reverseHead = malloc (sizeof (Node));
      reverseHead->next = NULL;
      reverseHead->data = -1000;
      current = reverseHead;
      while ((currentIndex = --len) >= 0)
        {
          (*(arrayPtr + currentIndex))->next = NULL;
          current->next = *(arrayPtr + currentIndex);
          current = current->next;
     
          printf ("*current:%p  *(arrayPtr+%d):%p\n", current, currentIndex,
              *(arrayPtr + currentIndex));
     
        }
     
      return &reverseHead;
    }
     
    int
    getlen_linklist (Node * const head)
    {
      printf ("------------%s----------\n", __func__);
      Node *current = head;
      int i = 0;
      while (current->next != NULL)
        {
          current = current->next;
          ++i;
        }
      return i;
     
    }
  • 相关阅读:
    编译器优化 → C关键字volatile → memory破坏描述符zz
    Mybatis 控制台打出SqlLog的设置
    J2EE ssm框架服务启动项内存加载数据及读取。
    Oracle 同步表权限分配(同义词)
    iOS开发雕虫小技之傻瓜式定位神器超简单方式解决iOS后台定时定位
    Oracle分页查询SQL实现
    iOS新建项目基本配置
    DP之子序列问题
    图论之最小生成树
    基础数论算法
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/2172815.html
Copyright © 2020-2023  润新知