• 线性链表的代码实现


    总结了一下线性链表的实现方式,贴在了博客里,欢迎大家批阅指正。

      1 #include "stdio.h"
      2 #include "stdlib.h"
      3 
      4 #define LIST_OK    0
      5 #define LIST_ERROR 1
      6 
      7 typedef unsigned int UINT32;
      8 typedef int ElemType;
      9 
     10 
     11 typedef struct LNode{
     12     ElemType _data;
     13     struct LNode *next;
     14 }LNode,*LinkList;
     15 void print_list(LNode *link_list);
     16 
     17 UINT32 create_reverse_list(LNode** link_list, UINT32 list_size){
     18     *link_list = (LNode*)malloc(sizeof(LNode));
     19     (*link_list)->next = NULL;
     20     UINT32 i=0;
     21     printf("Will create %d size link list...
    ",list_size);
     22     for(i=0; i< list_size; i++){
     23         LinkList p = (LNode*)malloc(sizeof(LNode));
     24         p->next = NULL;
     25         p->next = (*link_list)->next;
     26         printf("Please input %d th data:",i+1);
     27         ElemType data;
     28         scanf("%d",&data);
     29         p->_data = data;
     30         (*link_list)->next = p;
     31     }
     32     printf("Create list success!!!
    ");
     33     print_list(*link_list);
     34     return LIST_OK;
     35 }
     36 UINT32 create_order_list(LNode** link_list, UINT32 list_size){
     37     *link_list = (LNode*)malloc(sizeof(LNode));
     38     (*link_list)->next = NULL;
     39     LNode* last_node = (*link_list);
     40     UINT32 i=0;
     41     printf("Will create %d size link list...
    ",list_size);
     42     for(i=0; i< list_size; i++){
     43         LinkList p = (LNode*)malloc(sizeof(LNode));
     44         p->next = NULL;
     45         printf("Please input %d th data:",i+1);
     46         ElemType data;
     47         scanf("%d",&data);
     48         p->_data = data;
     49         last_node->next = p;
     50         last_node = p;
     51     }
     52     printf("Create list success!!!
    ");
     53     print_list(*link_list);
     54     return LIST_OK;
     55 }
     56 
     57 UINT32 destory_list(LNode** link_list){
     58     while((*link_list)->next){
     59         LNode* p = (*link_list)->next;
     60         (*link_list)->next = p->next;
     61         printf("free data:%d
    ",p->_data);
     62         free(p);
     63     }
     64     free(*link_list);
     65     *link_list = NULL;
     66     return LIST_OK;
     67 }
     68 LNode* locate_list(LNode* link_list, UINT32 i){
     69     LNode* p = link_list->next;
     70     UINT32 j = 1;
     71     while(p && j<i){
     72         p = p->next; j++;
     73     } 
     74     return p;
     75 }
     76 void print_list(LNode* link_list){
     77     if(NULL == link_list){
     78         printf("Link list is empty
    ");
     79         return;
     80     }
     81     LNode* p = link_list->next;
     82     UINT32 i = 1;
     83     while(p){
     84        printf("%-6d %d
    ",i,p->_data);
     85        p = p->next;
     86        i++;
     87     }
     88 }
     89 
     90 int main(){
     91     printf("Please input the size of creating list:");
     92     UINT32 size;
     93     scanf("%d",&size);
     94     LNode* link_list = NULL;
     95     printf("address %p
    ",link_list);
     96     create_order_list(&link_list, size);
     97     printf("address %p after creating
    ",link_list);
     98     print_list(link_list);
     99     UINT32 locate_num;
    100     printf("Please input locate number which you want:");
    101     scanf("%d",&locate_num);
    102     LNode* tmp_node = locate_list(link_list, locate_num);
    103     if(tmp_node){
    104         printf("Locating data is %d
    ", tmp_node->_data);
    105     }else{
    106         printf("Locating data not found!
    ");
    107     }
    108     destory_list(&link_list);
    109     printf("address %p after destory
    ",link_list);
    110     print_list(link_list);
    111     return 0;
    112 }

    程序的执行结果如下:

    Please input the size of creating list:3
    address (nil)
    Will create 3 size link list...
    Please input 1 th data:5
    Please input 2 th data:6
    Please input 3 th data:7
    Create list success!!!
    1      5
    2      6
    3      7
    address 0x1eea010 after creating
    1      5
    2      6
    3      7
    Please input locate number which you want:2
    Locating data is 6
    free data:5
    free data:6
    free data:7
    address (nil) after destory
    Link list is empty

    欢迎大家提出问题,一起交流

  • 相关阅读:
    linux使用命令记录
    (转)如何连接远程桌面
    PLSQL将查询结果复制为insert语句
    用foxPro打开dbf文件
    (转)PLSQL新手使用教程
    CPN TOOL使用
    运行mongoDB
    PLSQL连接数据库
    剑指 Offer 10- II. 青蛙跳台阶问题(简单)
    剑指 Offer 10- I. 斐波那契数列(简单)
  • 原文地址:https://www.cnblogs.com/r-yan/p/13452834.html
Copyright © 2020-2023  润新知