zoukankan      html  css  js  c++  java
  • C 语言 实现双向链表 模拟ArrayList功能 可追加,插入,移除,得到指定index 的元素值

    C 语言 实现双向链表 模拟ArrayList功能 可追加,插入,移除,得到指定index 的元素值.

    实现方法 :

    append,insert,Remove,getNode...

    View Code
      1 #include <stdio.h>
    2 #include <stdlib.h>
    3
    4 //define the node struct
    5 typedef struct tmp
    6 {
    7 int data;
    8 int ishead;
    9 struct tmp *pre;
    10 struct tmp *next;
    11 }intList;
    12 //create a int list
    13 intList * createList(void)
    14 {
    15 intList *p = (intList *)malloc(sizeof(intList));
    16 p->ishead = 1;
    17 return p;
    18 }
    19 //get the last node
    20 intList *getLastNode(intList *list)
    21 {
    22 intList *p = list;
    23 while(1)
    24 {
    25 if(p->next == NULL)
    26 return p;
    27 else
    28 p = p->next;
    29 }
    30 return NULL;
    31 }
    32 //append item
    33 void listAppend(intList *list,intList *n)
    34 {
    35 intList *tmp = NULL;
    36 if(list->ishead != 0)
    37 {
    38 *list = *n;
    39 }
    40 else
    41 {
    42 tmp = getLastNode(list);
    43
    44 if(n == tmp) //if the dizhi is the same
    45 {
    46 intList *p = (intList *)malloc(sizeof(intList));
    47 *p = *n;
    48 p -> pre = tmp;
    49 tmp->next = p;
    50 p->next = NULL;
    51 }
    52 else
    53 {
    54 n->pre = tmp;
    55 tmp->next = n;
    56 n->next = NULL;
    57 }
    58 }
    59 }
    60 // get node at index pos
    61 intList *listGetNode(intList *list,int index)
    62 {
    63 intList *p = list ;
    64 int tmp = 0;
    65 do
    66 {
    67 if(tmp == index)
    68 return p;
    69 tmp ++;
    70 }while((p = p->next) != NULL);
    71 return NULL;
    72 }
    73 //insert a node
    74 void listInsert(intList *list,intList *n,int index)
    75 {
    76 intList *p = listGetNode(list,index),*pr = p->pre;
    77 if(p)
    78 {
    79 if(p->pre != NULL)
    80 {
    81 pr->next = n;
    82 n->pre = pr;
    83 n->next = p;
    84 p->pre = n;
    85 }
    86 else //insert before the first one
    87 {
    88 intList *kao = (intList *)malloc(sizeof(intList));
    89 *kao = *p;
    90 n->next = kao;
    91 kao->pre = n;
    92 kao->next->pre = kao; //因为第一个元素变成了第二个元素. 所以一定要把原来的第二个元素的前一个元素改成 这个新的地址kao
    93 *list = *n;
    94 }
    95 }
    96 }
    97 //remove node at index
    98 void listRemoveNode(intList *list,int index)
    99 {
    100 intList *p = listGetNode(list,index);
    101 if(p)
    102 {
    103 if(p->pre == NULL) //删除列首
    104 {
    105 *list = *(list->next);
    106 list->pre = NULL;
    107 }
    108 else if(p->next == NULL) //删除列尾
    109 {
    110 p->pre->next = NULL;
    111 free(p);
    112 }
    113 else
    114 {
    115 (p->pre)->next = p->next;
    116 (p->next)->pre = p->pre;
    117 free(p);
    118 }
    119 }
    120 }
    121 //out put the list
    122 void putList(intList *list)
    123 {
    124 printf("%3d",list->data);
    125 if(list->next)
    126 putList(list->next);
    127 else
    128 printf("\n");
    129 }
    130
    131 int main()
    132 {
    133 //Create a int list
    134 intList *list = createList();
    135 intList i1 = {2,0,NULL,NULL};
    136 intList i2 = {45,0,NULL,NULL};
    137 intList i3 = {84,0,NULL,NULL};
    138 intList i4 = {24,0,NULL,NULL};
    139 intList i5 = {57,0,NULL,NULL};
    140 intList i7 = {77,0,NULL,NULL};
    141 listAppend(list,&i2);
    142 listAppend(list,&i1);
    143 listAppend(list,&i7);
    144 putList(list);
    145 printf("在第三个元素前增加 84:\n");
    146 listInsert(list,&i3,2);
    147 putList(list);
    148 printf("在列首增加 24:\n");
    149 listInsert(list,&i4,0);
    150 putList(list);
    151 printf("在列尾增加 57:\n");
    152 listAppend(list,&i5);
    153 putList(list);
    154 printf("删除第三个元素:\n");
    155 listRemoveNode(list,2);
    156 putList(list);
    157 printf("删除第一个元素:\n");
    158 listRemoveNode(list,0);
    159 putList(list);
    160 printf("删除最后一个元素:\n");
    161 listRemoveNode(list,3);
    162 putList(list);
    163 getchar();
    164 }

    代码输出结果:

  • 相关阅读:
    Nginx文件下载服务器部署
    Git分支命名规范
    ROS通信介绍
    linux环境设置默认路由的优先级
    Python日志方案
    Python threading Local()函数用法:返回线程局部变量
    Python中websocket的使用示例
    MQTT的Python使用示例
    利用systemback打包个人ISO系统镜像
    Spring Security学习笔记三
  • 原文地址:https://www.cnblogs.com/easyfrog/p/C_arraylist.html
Copyright © 2011-2022 走看看