• c语言,链表


    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Node{
        struct Node* next;
        int data;
    };
    typedef struct Node  NODE;
    typedef struct Node  LIST;
    
    LIST *creat_link_list(LIST *L, int n)
    {
        int i;
        NODE *p = 0;
        L = (NODE*) malloc(sizeof(NODE)); L->next =0;
        for(i = 0; i < n; i++)
        {
            p = (NODE*) malloc(sizeof(NODE)); 
            printf("input a data to be inserted:");
            scanf("%d", &p->data);
    
            p->next = L->next;
            L->next = p;
        }
        return L;
    }
    
    int delete_node(NODE* p)
    {
    //     static int s_i =1;
    //     printf("delete %d
    ", s_i++);
        p->next = 0;
        free(p);
        p = 0;  
        
        return 0;
    }
    
    int delete_link_list(LIST *L)
    {
        NODE* pd = L->next;
    
        while(L->next != 0){
            pd = L->next;
            L->next = L->next->next;
            delete_node(pd);
        }
        delete_node(L);
    
        return 0;
    }
    
    int show_link_list(LIST *L)
    {
        NODE* ps = L->next;
        int i = 0;
    
        while(ps != 0){
            printf("node%d: %d,", ++i , ps->data);
            ps = ps->next;
        };
        
    //     do{    //错误
    //         printf("node%d: %d,", ++i , ps->data);
    //         ps = ps->next;
    //     }while(ps->next != 0);
    
        return 0;
    }
    int t2()
    {
        NODE *L = 0;
        int n = 3;
    
        L = creat_link_list( L, n);
        printf("
    creat_link_list over
    ");
    
        show_link_list(L);
        printf("
    show_link_list over
    ");
    
        delete_link_list(L);
        printf("
    delete_link_list over
    ");
    
        return 0;
    }
    /*
    input a data to be inserted:1
    input a data to be inserted:2
    input a data to be inserted:3
    
    creat_link_list over
    node1: 3,node2: 2,node3: 1,
    show_link_list over
    
    delete_link_list over
    请按任意键继续. . .
    */
  • 相关阅读:
    基于HTTP和TFTP的PXE批量自动化安装Linux系统
    使用U盘进行Linux系统的安装
    CentOS 7单用户模式修改root密码
    LVS的调度算法分析
    linux LVS DR模式配置
    Nagios的客户端的安装
    Nagios监控系统的安装
    Linux内核升级
    redhat linux使用Centos yum源
    Juniper srx防火墙NAT配置
  • 原文地址:https://www.cnblogs.com/mylinux/p/4640014.html
Copyright © 2020-2023  润新知