• 线性表


    /*线性表的顺序表示和实现*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #define LIST_INIT_SIZE 100
    #define LISTINCREMENT 10
    #define TRUE 1
    #define ERROR 0
    #define FALSE 0
    #define OK 1
    #define OVERFLOW -2
    typedef int Status;
    typedef int ElemType;
    typedef struct {
        ElemType *elem;
        int length;
        int listsize;
    }SqList;
    Status InitList_Sq(SqList &L){
        L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
        if(!L.elem)
            exit(OVERFLOW);
        L.length=0;
        L.listsize=LIST_INIT_SIZE;
        return OK;
    }
    Status ListLength(SqList l){
        return l.length;
    }
    Status GetElem(SqList l,int i,ElemType &e){
        if(i<1||i>l.length)
            exit(ERROR);
        e=*(l.elem+i-1);
        return OK;
    }
    Status ListInsert_Sq(SqList &L,int i,ElemType e){
        ElemType *newbase,*q,*p;
        if(i<1||i>L.length+1)
            return ERROR;
        if(L.length>=L.listsize){
            newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
            if(!newbase)  exit(OVERFLOW);
            L.elem=newbase;
            L.listsize+=LISTINCREMENT;
        }
        q=&(L.elem[i-1]);
        for(p=&(L.elem[L.length-1]);p>=q;--p)
            *(p+1)=*p;
        *q=e;
        ++L.length;
        return OK;
    }
    
    Status Equal(ElemType a,ElemType b){
        if(a==b)
            return TRUE;
        else
            return FALSE;
    }
    Status LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType)){
        ElemType *p;
        int i=1;
        p=L.elem;
        while(i<=L.length&&!(*compare)(*p++,e))
            ++i;
        if(i<=L.length)
            return i;
        else
            return 0;
    }
    void print(ElemType c){
        printf("%d ",c);
    }
    Status ListTraverse(SqList l,void(*vi)(ElemType)){
        ElemType *p;
        int i;
        p=l.elem;
        for(i=1;i<=l.length;i++)
            vi(*p++);
        printf("
    ");
        return OK;
    }
    Status DestroyList(SqList  &l){
        if (l.elem) free(l.elem);
        return OK;
    }
    Status Clearlist(SqList &l){
        l.length=0;
        return OK;
    }
    Status ListDelete(SqList &l,int i,ElemType &e){
        ElemType *q,*p;
        if(i<1||i>l.length)
            return ERROR;
        q=&(l.elem[i]);
        for(p=q;p<=&(l.elem[l.length-1]);p++)
            *(p-1)=*p;
        e=*q;
        --l.length;
        return OK;
    }
    /*void unio(SqList &La,SqList Lb){
        ElemType e;
        int i;
        int La_len=ListLength(La);
        int Lb_len=ListLength(Lb);
        for(i=1;i<=Lb_len;i++){
            GetElem(Lb,i,e);
            if(!LocateElem(La,e,Equal))
                ListInsert_Sq(La,++La_len,e);
        }
    }
    
    void jiao(SqList La,SqList Lb,SqList &Lc){
        ElemType e;
        int i;
        int La_len=ListLength(La);
        int Lb_len=ListLength(Lb);
        int Lc_len=ListLength(Lc);
        for(i=1;i<=La_len;i++){
            GetElem(La,i,e);
            if(LocateElem(Lb,e,Equal))
                ListInsert_Sq(Lc,++Lc_len,e);
        }
    }
    */
    void jianfa(SqList La,SqList Lb,SqList &Lc){
        ElemType e;
        int i;
        int La_len=ListLength(La);
        int Lb_len=ListLength(Lb);
        int Lc_len=ListLength(Lc);
        for(i=1;i<=La_len;i++){
            GetElem(La,i,e);
            if(!LocateElem(Lb,e,Equal))
                ListInsert_Sq(Lc,++Lc_len,e);
        }
    }
    
    #define MAXN 1000
    int i,j,k;
    int a[MAXN]={3,5,8,11},b[MAXN]={2,6,8,9,11,15,20};
    int main(){
        SqList la,lb,lc,ld;
        InitList_Sq(la);
        for(i=1;i<=4;i++){
            ListInsert_Sq(la,i,a[i-1]);
        }
        ListTraverse(la,print);
        InitList_Sq(lb);
        for(i=1;i<=7;i++){
            ListInsert_Sq(lb,i,b[i-1]);
        }
        ListTraverse(lb,print);
    
        /*unio(la,lb);//合并AB
        ListTraverse(la,print);
    
        InitList_Sq(lc);
        jiao(la,lb,lc);//求AB的交集
        ListTraverse(lc,print);*/
    
        InitList_Sq(ld);
        jianfa(la,lb,ld);
        ListTraverse(ld,print);
        return 0;
    }
  • 相关阅读:
    递归函数 二分查找
    内置函数
    迭代器 生成器 推导式
    装饰器函数的有用信息
    函数名 闭包 装饰器
    动态参数 命名空间
    C#_LINQ(LINQ to Entities)
    C#_MVC 自定义AuthorizeAttribute实现权限管理
    C#_MVC3之使用Authorize简单的验证登录(一)
    C#_MVC_分页update
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205292.html
Copyright © 2020-2023  润新知