zoukankan      html  css  js  c++  java
  • 数据结构-单链表(Linked List)

    #include <stdio.h>
    #include <stdlib.h>
    #define LIST_INIT_SIZE 10
    #define LISTINCREMENT 100
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
    
    typedef int status;         //函数返回类型
    typedef int ElemType;   //数据类型
    typedef struct Lnode
    {
        ElemType data;
        struct Lnode *next;
    } Lnode, *Linklist;
    
    int i,j;
    /*
        创建
        添加(在数字i 之前)
        删除i 号元素
        打印链表
        合并链表
        链表排序(比较low)
    */
    status CreateList_L(Linklist *L, int n);
    status ListInsert_L(Linklist *L, int i, ElemType e);
    status ListDelete_L(Linklist *L, int i, ElemType *e);
    status ListPrint_L(Linklist L);
    status MergeList_L(Linklist *La, Linklist *Lb, Linklist *Lc);
    status ListSort_L(Linklist *L, int n);
    
    int main()
    {
        Linklist List, L2, L3;
        ElemType e;
        int p;
        printf("Please input 5 number to insert the list_A:
    ");
        CreateList_L(&List, 5);
        printf("Please input the element to insert_A:
    ");
        scanf("%d", &e);
        printf("Please input the position to insert_A:
    ");
        scanf("%d", &p);
        ListInsert_L(&List, p, e);
        printf("After insert,the list is:
    ");
        ListPrint_L(List);
        printf("Please input the position to delete_A:
    ");
        scanf("%d", &p);
        ListDelete_L(&List, p, &e);
        printf("After delete,the list is:
    ");
        ListPrint_L(List);
        printf("Please input 5 number to insert the list_B:
    ");
        CreateList_L(&L2, 5);
        ListSort_L(&List, 5);
        ListSort_L(&L2, 5);
        MergeList_L(&List, &L2, &L3);
        ListPrint_L(L3);
        return 0;
    }
    
    status CreateList_L(Linklist *L,int n)
    {
        Linklist r;
        *L = (Linklist)malloc(sizeof(Lnode));
        (*L)->next = NULL;
        r = *L;
        for (i= 1; i<= n; ++i)
        {
            Linklist p = (Linklist)malloc(sizeof(Lnode));
            scanf("%d", &p->data);
            r->next = p;
            r = p;
        };
        r->next = NULL;
    };
    
    status ListInsert_L(Linklist *L, int i, ElemType e)
    {
        Linklist p = *L;
        j = 0;
        while (p && j< i- 1)
            p = p->next, ++j;
        if (!p || j> i- 1)
            return ERROR;
        Linklist s = (Linklist)malloc(sizeof(Lnode));
        s->data = e;
        s->next = p->next;
        p->next = s;
        return OK;
    }
    
    status ListDelete_L(Linklist *L, int i, ElemType *e)
    {
        Linklist p = *L;
        j = 0;
        while (p->next && j< i- 1)
        {
            p=p->next;
            ++j;
        }
        if (!(p->next) || j> i- 1)
            return ERROR;
        Linklist q = p->next;
        p->next = q->next;
        *e = q->data;
        free(q);
        return OK;
    }
    
    status ListPrint_L(Linklist L)
    {
        Linklist temp = L->next;
        while(temp != NULL)
        {
            printf("%d%c",temp->data,temp->next==NULL?'
    ':' ');
            temp = temp->next;
        }
        return OK;
    }
    /*
        不知道为啥MergeList_L老是(内存越界?)错误.
        结论:free问题,自己free了自己应用的内存真是自己犯的错笑着也要改下去:)
    */
    
    status MergeList_L(Linklist *La, Linklist *Lb, Linklist *Lc)
    {
        Linklist pa = *La;
        Linklist pb = *Lb;
        pa = pa->next, pb = pb->next;
        Linklist pc = *Lc =  *La;
        while(pa&& pb)
        {
            if(pa->data <= pb->data)
                pc->next = pa, pc = pa, pa = pa->next;
            else
                pc->next = pb, pc = pb, pb = pb->next;
        }
        pc->next = pa ? pa : pb;
    //    free(Lb);
    }
    status ListSort_L(Linklist *L, int n)
    {
        ElemType temp;
        ElemType *a= (ElemType*)malloc(n* sizeof(*a));
        Linklist p,q;
        p= *L;
        q= *L;
        i= 0;
        while(q)
            a[i++]= q->data, q= q->next;
        for(i= 1; i<= n; i++)
            for(j= 1; j<= n- i; j++)
                if(a[j]>a[j+1])
                    temp= a[j], a[j]= a[j+1], a[j+1]= temp;
        i= 0;
        while(p)
            p->data= a[i++], p= p->next;
    }
    

  • 相关阅读:
    SpringBoot梳理一(快速上手及常用配置)
    C# .NET写入记事本(规范版)
    PhpStorm安装及汉化
    C# .NET发送邮件方法
    Maven安装教程
    plsql连接Oracle数据库安装及配置
    Mysql常用SQL语句整理
    C#字符串处理和时间格式化整理
    PHP字符串处理和时间格式化整理
    pandas的小技巧
  • 原文地址:https://www.cnblogs.com/chinashenkai/p/9451405.html
Copyright © 2011-2022 走看看