• 初学数据结构——单向循环链表和双向循环链表。


    这几天都还是有好好看链表。觉得还是不错滴。就是有点头疼。学会链表之后,突然觉得结构体真是一个很值得去学习的知识。

    因为它拟补了数组的单调性。最重要的是,存储元素很方便吖,操作起来也不太难。很清晰。所以,这几天我也写了单向循环链表和双向循环链表。

    感觉区别不太大⊙0⊙。明天就要开始看栈和队列了。听说数据结构越学越难。希望我不会晕倒吧。

    (>_<分享代码啦,感觉自己的代码风格好丑。最近都稍微看看林锐博士的代码规范。有努力改进了!)

    单向循环链表

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 typedef struct node
     5 {
     6     int data;
     7     struct node *next;
     8 }Node;
     9 
    10 typedef struct node *linklist;
    11 
    12 linklist create_list(linklist head, int n)
    13 {
    14     linklist pre;
    15     head = (linklist)malloc(sizeof(Node));
    16     pre = (linklist)malloc(sizeof(Node));
    17     scanf("%d",&pre->data);
    18     head->next = pre;
    19     for(int i = 1; i < n; i++)
    20     {
    21         pre->next = (linklist)malloc(sizeof(Node));
    22         pre = pre->next;
    23         scanf("%d",&pre->data);
    24     }
    25     pre->next = head;  //循环出来之后把,最后一个结点指向head.因此就成为了单向循环链表。
    26     return head;
    27 }
    28 
    29 void print_list(linklist head)
    30 {
    31     linklist pre;
    32     if(head == NULL)
    33         printf("No List
    ");
    34     else
    35     {
    36         pre = head->next;  //刚开始的时候,必须是head指向的下一个结点。
    37         while(pre != head) //打印的时候 如果单向循环链表会重新回到head.因此,只要回来head,就结束。
    38         {
    39             printf("%d ",pre->data);
    40             pre = pre->next;
    41         }
    42         printf("
    ");
    43     }
    44 }
    45 
    46 int main()
    47 {
    48     linklist phead;
    49     phead = create_list(phead,10);
    50     print_list(phead);
    51     return 0;
    52 }

    双向循环链表

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 
     5 using namespace std;
     6 
     7 typedef struct node          //    | ------- |   这是我抽象出来的双链表的一块结构体内存空间。
     8 {                            //    |  *prev  |   形状大概就是这个样吧。我觉得抽象成这样比较
     9     struct node *prev;       //    | ------- |   好理解。
    10     int data;                //    |   data  |
    11     struct node *next;       //    | ------- |
    12 }Node;                       //    |  *next  |
    13                              //    | ------- |
    14 
    15 
    16 typedef struct node *linklist;
    17 
    18 linklist create_list(linklist head, int n)
    19 {
    20     linklist pre;
    21     head = (linklist)malloc(sizeof(Node));
    22     pre  = (linklist)malloc(sizeof(Node));
    23     scanf("%d",&pre->data);
    24     head->next = pre;         //双链表和单链表不一样的就是:单链表每个结点都只有一个尾指针
    25     pre->prev = head;         //                            双链表每个结点有头指针和尾指针
    26     for(int i = 1; i < n; i++)
    27     {
    28         pre->next = (linklist)malloc(sizeof(Node));
    29         pre->next->prev = pre;
    30         pre = pre->next;
    31         scanf("%d",&pre->data);
    32 
    33     }
    34     pre->next = head;
    35     head->prev = pre;
    36     return head;
    37 }
    38 
    39 linklist insert_value(linklist head,int value)  //链表增加元素的方式和单链表差不多,就是要多操作一下头和尾的连接。
    40 {
    41     linklist temp;
    42     temp = (linklist)malloc(sizeof(Node));
    43     temp->data = value;
    44     temp->next = head->next;
    45     head->next->prev = temp;
    46     temp->prev = head;
    47     head->next = temp;
    48     return head;
    49 }
    50 
    51 void print_list(linklist head)
    52 {
    53     linklist pre;
    54 
    55    //此段注释的代码为逆序打印链表
    56    // pre = head->prev;
    57    // while(pre != head)
    58    // {
    59    //     printf("%d ",pre->data);
    60    //     pre = pre->prev;
    61    // }
    62    // printf("
    ");
    63 
    64     pre = head->next;
    65     while(pre != head)
    66     {
    67         printf("%d ",pre->data);
    68         pre = pre->next;
    69     }
    70     printf("
    ");
    71 }
    72 
    73 int main()
    74 {
    75     linklist phead;
    76     phead = create_list(phead,5);
    77     print_list(phead);
    78     phead = insert_value(phead,100);
    79     print_list(phead);
    80     return 0;
    81 }
  • 相关阅读:
    设计模式之工厂模式
    面向对象的五大原则
    抽象类和接口、类库
    静态
    面向对象三大特性
    JVM(Java虚拟机)优化大全和案例实战
    Java调用Lua脚本(LuaJava使用、安装及Linux安装编译)
    Java调用.dll文件
    linux yum命令详解
    linux nohup命令
  • 原文地址:https://www.cnblogs.com/hersen/p/3250203.html
Copyright © 2020-2023  润新知