• 线性表


    /*线性表的顺序表示和实现*/
    #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;
    }
  • 相关阅读:
    更新整理本人所属博客文章的示例代码和工具组件(Java 和 C++)
    【端午呈献】通用高性能 Windows Socket 组件 HPSocket v2.2.1 悄然发布
    REST手记(一):对URI隧道技术以及CRUD操作的理解
    Jquery+JSON消费REST WCF4.0 服务(带源码)
    对REST架构 风格下WCF的一点补充
    REST与SOA两种架构下WCF的异同比较(含源码)
    REST笔记(三):一种标准的超媒体格式:Atom
    REST WCF 使用Stream进行Server与Client交互
    REST笔记(六)通过缓存架构可伸缩性与容错性的Service
    深入理解WCF系统体系(之二:WCF客户端如何构建?(上))
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205292.html
Copyright © 2020-2023  润新知