zoukankan      html  css  js  c++  java
  • 单链表实现实例

    /* list.h */
    #ifndef _LINKLIST_H
    #define _LINKLIST_H
    
    struct node {
        int data;
        struct node *next;
    };
    
    typedef struct node *ptr_to_node;
    typedef struct node *position;
    typedef struct node *list;
    
    list create_list();
    void insert(int x, list l, position p);
    void insert_to_head(int x, list l);
    void insert_to_tail(int x, list l);
    position find_previous(int x, list l);
    int is_last(position p, list l);
    void delete(int x, list l);
    void printl(list l);
    void make_empty(list l)
    
    #endif
    /* list.c */
    #include "list.h"
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    
    list 
    create_list()
    {
        list l;
        l = malloc(sizeof(struct node));
        if(l == NULL)
        {
            perror("malloc error");
            exit(1);
        }
        l->next = NULL;
    
        return(l);
    }
    
    void 
    insert(int x, list l, position p)
    {
        position newnode;
        newnode = malloc(sizeof(struct node));
    
        if(newnode == NULL)
        {
            perror("malloc error");
            exit(1);
        }
        newnode->data = x;
        newnode->next = p->next;
        p->next = newnode;
    }
    void
    insert_to_head(int x, list l)
    {
        insert(x, l, l);
    }
    void
    insert_to_tail(int x, list l)
    {
        ptr_to_node nptr;
    
        nptr = l;
        while(nptr->next != NULL)
            nptr = nptr->next;
        insert(x, l, nptr);
    }
    position
    find_previous(int x, list l)
    {
        position p;
        
        p = l;
        while(p->next != NULL && p->next->data != x)
            p = p->next;
    
        return(p);
    }
    int
    is_last(position p, list l)
    {
        return(p->next == NULL);
    }
    void
    delete(int x, list l)
    {
        position p, tmpp;
    
        p = find_previous(x, l);
    
        if( !is_last(p, l))
        {
            tmpp = p->next; 
            p->next = tmpp->next;
            free(tmpp);
        }
    }
    void printl(list l)
    {
        position p;
        
        p = l->next;
        while(p != NULL)
        {
            printf("%d->", p->data);
            p = p->next;
        }
        printf("NULL
    ");
    }

      void make_empty(list l)
      {
          ptr_to_node p, p_next;

          p = l->next;
          while(p != NULL)
         {
             p_next = p->next;
             free(p);
             p = p_next;
         }
         l->next = NULL;
      }

     
    /* test.c */
    #include "list.h"
    #include <stdio.h>
    
    int
    main(void)
    {    
        list l;
        l = create_list();
        printf("insert from list tail:
    ");
        insert_to_tail(1, l);
        insert_to_tail(2, l);
        insert_to_tail(3, l);
        insert_to_tail(4, l);
        insert_to_tail(5, l);
        printl(l);
        printf("after delete 3:
    ");
        delete(3, l);
        printl(l);
    
        putchar('
    ');
        printf("insert from list head:
    ");
        insert_to_head(1, l);
        insert_to_head(2, l);
        insert_to_head(3, l);
        insert_to_head(4, l);
        insert_to_head(5, l);
        printl(l);
        printf("after delete 3:
    ");
        delete(3, l);
        printl(l);
    }

    编译运行:

    image

  • 相关阅读:
    C# 小规模查找集合性能测试
    高级前端开发不可或缺的知识
    移动前端开发-单页应用(spa)模型
    移动开发之用视频做背景
    纯CSS打造忙碌光标
    移动前端开发之数据库操作篇
    如何从源码中学习javascript
    Deffered.js的实现原理
    Codeforces Round #381 (Div. 2)
    2017 ZSTU寒假排位赛 #6
  • 原文地址:https://www.cnblogs.com/nufangrensheng/p/3579993.html
Copyright © 2011-2022 走看看