zoukankan      html  css  js  c++  java
  • 链表结构_C语言实现

    随着逐渐地学习,我慢慢体会到了程序=数据结构+算法这句话的内涵了。

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #define ANODE struct node
      4 typedef struct node *Node;
      5 typedef int DataType;
      6 struct node
      7 {
      8     DataType info;
      9     Node node;
     10     int index;
     11 };
     12 
     13 // new an empty LinkList
     14 Node creEmList()
     15 {
     16     Node node = (Node) malloc(sizeof(ANODE));
     17     if (node != NULL)
     18     {
     19         node->node = NULL;
     20         node -> index = -1;
     21     }
     22     else
     23         printf("fail to create an empty LinkList");
     24     return node;
     25 }
     26 
     27 // judge whether the LinkList is empty
     28 int isEmList(Node list)
     29 {
     30     return list->node == NULL;
     31 }
     32 
     33 // insert an element into the last index of LinkList
     34 void insertToLast(Node list, DataType x)
     35 {
     36     Node aNode = (Node) malloc(sizeof(ANODE));
     37     aNode->node = NULL;
     38     aNode->info = x;
     39     while (list->node != NULL)
     40         list = list->node;
     41     aNode->index = list->index + 1;
     42     list->node = aNode;
     43 }
     44 
     45 //insert an element after the node of the index
     46 void insertAfter(Node list, DataType x, int index)
     47 {
     48     void createIndex(Node);
     49     Node aNode = (Node) malloc(sizeof(ANODE));
     50     aNode->index = index + 1;
     51     aNode -> info = x;
     52     Node first = NULL, second = NULL;
     53     while(list -> node != NULL)
     54     {
     55         if(list -> index == index)
     56         {
     57             first = list;
     58             second = list -> node;
     59             break;
     60         }
     61         list = list -> node;
     62     }
     63     if(second != NULL)
     64     {
     65         aNode -> node = second;
     66         first -> node = aNode;
     67         createIndex(second);
     68     }
     69     else if(list->index == index)
     70     {
     71         first = list;
     72         first->node = aNode;
     73     }
     74     else 
     75     {
     76     printf("out of bounds");
     77     return;
     78     }
     79         
     80 }
     81 
     82 //create the index of nodes after the node given by the index
     83 void createIndex(Node node)
     84 {
     85     while(node->node != NULL)
     86     {
     87         node -> index++;
     88         node = node -> node;
     89     }
     90     node -> index++;
     91 }
     92 
     93 //delete the node which value is x from the LinkList
     94 void deleteNode(Node list, DataType x)
     95 {
     96     void downIndex(Node);
     97     Node find(Node, DataType);
     98     Node node = NULL, first = NULL, second = NULL,third = NULL;
     99     node = find(list, x);
    100     if(node != NULL)
    101     {
    102         if(node -> node != NULL)
    103         {
    104             first = node;
    105             second = node -> node;
    106             if(second -> node != NULL)
    107             {
    108                 third = second -> node;
    109                 first -> node = third;
    110                 downIndex(second);    
    111                 free(second);
    112             }
    113             else
    114             {
    115                 first -> node = NULL;
    116                 free(second);
    117             }
    118         }
    119         else
    120         free(node);    
    121     }    
    122 }
    123 
    124 //find the note whie value is equal to x
    125 Node find(Node list, DataType x)
    126 {
    127     while(list -> node != NULL)
    128     {
    129         if(list -> node ->info == x)
    130                 return list;
    131         list = list -> node;
    132     }
    133     if(list -> info == x)
    134         return list;
    135     else
    136         printf("no this value in the LinkList\n");
    137     return NULL;    
    138 }
    139 //Auto downgrade node's index after the given node
    140 void downIndex(Node node)
    141 {
    142     while(node -> node != NULL)
    143     {
    144         node->index--;
    145         node = node -> node;;
    146     }
    147     node -> index--;
    148 }
    149 
    150 //get the index of the Node
    151 int getIndex(Node list, DataType x)
    152 {
    153     while(list -> node != NULL)
    154     {
    155         if(list->info == x)
    156             return list->index;
    157         list = list->node;
    158     }
    159     if(list -> info == x)
    160         return list->index;
    161     else 
    162     {
    163         printf("value %d is not exist\n", x);
    164         return -1;
    165     }    
    166 }
    167 
    168 int main()
    169 {
    170     Node list = creEmList();
    171     insertToLast(list, 55);
    172     insertToLast(list,66);
    173     insertAfter(list, 77, 0);
    174     deleteNode(list, 77);
    175     int index = getIndex(list, 66);
    176     printf("value's index is: %d\n", index);
    177     while (list->node != NULL)
    178     {
    179         printf("index:%d value:%d\n", list->index, list->info);
    180         list = list->node;
    181     }
    182     printf("index:%d value:%d\n", list->index, list->info);
    183 }
  • 相关阅读:
    了解java注解
    使用java泛型设计通用方法
    dbutils基本使用
    jquery+ajax+struts2
    c3p0连接数据库的3种方式
    ASP单步调试工具
    设置网页图片不能被用户下载或者另存为
    简单树形菜单
    GBK,GB3212 Unicode编码问题详解
    html页面乱码问题解决方法编码批量转换
  • 原文地址:https://www.cnblogs.com/hanyuan/p/linklist.html
Copyright © 2011-2022 走看看