• C基础--单链表的构造


    #include <stdio.h>
    #include "link.h"
    
    void print_item(link p)
    {
        printf("%d
    ", p->item);
    }
    int main(void)
    {
        link head, p;        //struct node *head;
        link_init(&head);        //head => NULL
    
        p = make_node(3);
        link_insert(&head, p);    //头插法
        p = make_node(5);
        link_insert(&head, p);    //头插法
        p = make_node(1);
        link_insert(&head, p);    //头插法
        p = make_node(8);
        link_insert(&head, p);    //头插法
    
        link_travel(&head, print_item);    //遍历打印链表数值域
        printf("***************
    ");
    
        p = link_search(&head, 1);
        if (p != NULL) {
            link_delete(&head, p);
            free_node(p);
        }
        link_travel(&head, print_item);    //遍历打印链表数值域
        link_destory(&head);
    
        return 0;
    }
    #include <stdio.h>
    #include <stdlib.h>
    #include "link.h"
    
    void link_init(link *head)        //struct node **head = &head
    {
        //head = NULL;
        *head = NULL;
    }
    link make_node(int item)
    {
        //link p = (link *)malloc(sizeof(struct node));
        link p = (link)malloc(sizeof(*p));
        p->item = item;                //(*p).itme = item;
        p->next = NULL;                //#define NULL (void *)0
        return p;
    }
    void link_insert(link *head, link p)        //头插法
    {
        p->next = *head;
        *head = p;
    }
    link link_search(link *head, int key)
    {
        link p;
        for (p = *head; p != NULL; p = p->next)
            if (p->item == key)
                return p;
        return NULL;
    }
    void link_delete(link *head, link q)
    {
        link p;
        if (q == *head) {
            *head = q->next;
            return;
        }
        for (p = *head; p != NULL; p = p->next)
            if (p->next == q) {
                p->next = q->next;
                return;
            }
    
    }
    void free_node(link p)
    {
        free(p);
    }
    void link_modfie(link p, int key)
    {
        p->item = key;
    }
    void link_destory(link *head)
    {
        link p= *head, q;
        while (p != NULL) {
            q = p->next;
            free(p);
            p = q;
        }
        *head = NULL;
    }
    void link_travel(link *head, void (*vist)(link))
    {
        link p;
        for (p = *head; p != NULL; p = p->next)
            vist(p);
    }
    #ifndef _LINK_H_
    #define _LINK_H_
    typedef struct node *link;
    struct node {
        int item;
        link next;        //struct node *next;
    };
    
    void link_init(link *head);
    link make_node(int item);
    void link_insert(link *head, link p);
    link link_search(link *head, int key);
    void link_delete(link *head, link p);
    void free_node(link p);
    void link_modfie(link p, int key);
    void link_destory(link *head);
    void link_travel(link *head, void (*vist)(link));
    
    #endif
  • 相关阅读:
    poj3625
    Directx10,11的SwapChain,RenderTarget和DepthBuffer解释
    Everything you need to know about Authenticode Code Signing
    Best Render Settings for After Effects CS5
    javascript 中cookie的存储,获取cookie,删除cookie的方法
    白话数字签名(番外篇)——签名EXE文件(下)
    在php中使用CKEDITOR在线编辑器
    HTML5 VIDEO标签播放事件流水
    白话数字签名(番外篇)——签名EXE文件(上)
    自定义hint框
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4840944.html
Copyright © 2020-2023  润新知