zoukankan      html  css  js  c++  java
  • 线性表之一,SEQLIST(顺序表)类及其父类LIST,模板类及C结构体,包装顺序表

    一、C++模板

    1、seqList模板类,顺序表代码

      1 # include "list.h"
      2 
      3 //代码清单2-2 顺序表类的定义和实现
      4 // The Definition of seqList
      5 template <class elemType>
      6 class seqList:public list<elemType>
      7 {
      8 public:
      9     seqList(int initSize);          // 构造函数
     10     ~seqList();                       // 析构函数
     11     void clear();
     12     int length() const;
     13     void insert(int i, const elemType &x);
     14     void remove(int i);
     15     int search(const elemType &x) const;
     16     elemType visit(int i) const;
     17     void traverse() const;
     18     void erase(int i);
     19 
     20 private:
     21     elemType *data;       // 存储线性表元素的数组的起始地址
     22     int currentLength;     // 线性表的表长
     23     int maxSize;            // 数组的规模
     24     void doubleSpace();  // 扩大数组空间
     25 };
     26 
     27 // 代码清单2-3 顺序表类的clear、visit、length和析构函数的实现
     28 // ~seqList
     29 template <class elemType>
     30 seqList<elemType>::~seqList()
     31 {
     32     delete [] data;
     33 }
     34 
     35 // clear
     36 template <class elemType>
     37 void seqList<elemType>::clear()
     38 {
     39     currentLength = 0;
     40 }
     41 
     42 // length
     43 template <class elemType>
     44 int seqList<elemType> ::length() const
     45 {
     46     return currentLength;
     47 }
     48 
     49 // visit
     50 template <class elemType>
     51 elemType seqList<elemType> ::visit(int i) const
     52 {
     53     return data[i];
     54 }
     55 
     56 // traverse
     57 template <class elemType>
     58 void seqList<elemType> ::traverse() const
     59 {
     60     cout<<endl;
     61     for (int i = 0; i<currentLength; ++i)
     62         cout<<data[i]<<"";
     63 }
     64 
     65 // seqList
     66 template <class elemType>
     67 seqList<elemType> ::seqList(int initSize)
     68 {
     69     data = new elemType[initSize];
     70     maxSize = initSize;
     71     currentLength = 0;
     72 }
     73 
     74 // search
     75 template <class elemType>
     76 int seqList<elemType> ::search(const elemType &x) const
     77 {
     78     int i;
     79     for (i = 0; i<currentLength && data[i]!=x; ++i);
     80     
     81     if (i==currentLength) 
     82         return -1;
     83     else 
     84         return i;
     85 }
     86 
     87 // doubleSpace
     88 template <class elemType>
     89 void seqList<elemType> ::doubleSpace()
     90 {
     91     elemType *tmp = data;
     92 
     93     maxSize *=2;
     94     data = new elemType[maxSize];
     95     for (int i = 0; i<currentLength; ++i)
     96         data[i] = tmp[i];
     97     delete [] tmp;
     98 }
     99 
    100 // insert
    101 template <class elemType>
    102 void seqList<elemType> ::insert(int i, const elemType &x)
    103 {
    104     if(i>=maxSize) doubleSpace();//如果插入点大于数组的上限,扩充数组
    105     
    106     if(i>this->currentLength) {
    107         cout<<"插入位置与线性表元素不连续:currentLength="<<currentLength<<endl;
    108         return;
    109     }
    110     
    111     for (int j = currentLength-1; j>=i; --j)//元素后移,给i留出来插入的空间
    112         data[j+1] = data[j];
    113     
    114     this->data[i] = x;//插入元素
    115     this->currentLength++; //所有元素的个数+1
    116 }
    117 
    118 // remove
    119 template <class elemType>
    120 void seqList<elemType> ::remove(int i)
    121 {
    122     if(i>=this->currentLength) {
    123         cout<<"删除位置错误:currentLength="<<currentLength<<endl;
    124         return;
    125     }
    126     for (int j = i; j<currentLength; ++j)
    127         data[j] = data[j+1];
    128     this->currentLength--;
    129 }
    seqList模板类,顺序表(动态数组)

    注:上面代码 doubleSpace() 类成员函数 扩充空间有漏洞,当maxSize==0时,不会扩充空间

    2、seqList父类list,抽象类的代码

     1 // 代码清单2-1 线性表的抽象类
     2 template <class elemType>
     3 class list
     4 {
     5 public:
     6     virtual void clear()=0;
     7     virtual int length() const=0;
     8     virtual void insert(int i, const elemType &x)=0;
     9     virtual void remove(int i)=0;
    10     virtual int search(const elemType &x) const=0;
    11     virtual elemType visit(int i) const=0;
    12     virtual void traverse() const=0;
    13     virtual ~list() {};
    14 };
    list抽象类, 模板类

    二、C结构体,包装顺序表

    1.顺序表(结构体包装)的初始化、查找、插入、删除

     1 #include<stdio.h>
     2 #include <stdlib.h>
     3 #define MAXSIZE 15
     4 #define false 0
     5 #define true 1
     6 typedef int Position,ElementType,bool;
     7 typedef struct LNode *List;
     8 struct LNode {
     9     ElementType Data[MAXSIZE];
    10     Position Last;
    11 };
    12 
    13 /* 初始化 */
    14 List MakeEmpty()
    15 {
    16     List L;
    17     
    18     L = (List)malloc(sizeof(struct LNode));
    19     L->Last = -1;
    20     
    21     return L;
    22 }
    23 
    24 /* 查找 */
    25 #define ERROR -1
    26 
    27 Position Find( List L, ElementType X )
    28 {
    29     Position i = 0;
    30     
    31     while( i <= L->Last && L->Data[i]!= X )
    32         i++;
    33     if ( i > L->Last )  return ERROR; /* 如果没找到,返回错误信息 */
    34     else  return i;  /* 找到后返回的是存储位置 */
    35 }
    36 
    37 /* 插入 */
    38 /*注意:在插入位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),
    39 这里P是存储下标位置(从0开始),两者差1*/
    40 bool Insert( List L, ElementType X, Position P ) 
    41 { /* 在L的指定位置P前插入一个新元素X */
    42     Position i;
    43     
    44     if ( L->Last == MAXSIZE-1) {
    45         /* 表空间已满,不能插入 */
    46         printf("表满"); 
    47         return false; 
    48     }  
    49     if ( P<0 || P>L->Last+1 ) { /* 检查插入位置的合法性 */
    50         printf("位置不合法");
    51         return false; 
    52     } 
    53     for( i=L->Last; i>=P; i-- )
    54         L->Data[i+1] = L->Data[i]; /* 将位置P及以后的元素顺序向后移动 */
    55     L->Data[P] = X;  /* 新元素插入 */
    56     L->Last++;       /* Last仍指向最后元素 */
    57     return true; 
    58 } 
    59 
    60 /* 删除 */
    61 /*注意:在删除位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),
    62 这里P是存储下标位置(从0开始),两者差1*/
    63 bool Delete( List L, Position P )
    64 { /* 从L中删除指定位置P的元素 */
    65     Position i;
    66     
    67     if( P<0 || P>L->Last ) { /* 检查空表及删除位置的合法性 */
    68         printf("位置%d不存在元素", P ); 
    69         return false; 
    70     }
    71     for( i=P+1; i<=L->Last; i++ )
    72         L->Data[i-1] = L->Data[i]; /* 将位置P+1及以后的元素顺序向前移动 */
    73     L->Last--; /* Last仍指向最后元素 */
    74     return true;   
    75 }
    76 int main()
    77 {
    78     List p = MakeEmpty();
    79     Insert(p,9,0);
    80     Insert(p,8,1);
    81     printf("%d
    ",Find(p,9));
    82     printf("%d
    ",Find(p,8));
    83     return 0;
    84 }
    顺序表初始化、查找、插入、删除
    2.顺序表,删除给定值S与T之间的所有元素

    3.顺序表,逆序

     1 #include <stdio.h>
     2 #define MAXSIZE 100
     3 /* 逆序 */
     4 void reverse(int* arr, int n)
     5 {
     6     int* p = arr, *q = arr + n - 1;
     7     while(p<q)
     8     {
     9         int t = *p;
    10         *p = *q;
    11         *q = t;
    12         p++,q--;
    13     }
    14 } 
    15 /* 输入 */
    16 void putin(int* arr, int n)
    17 {
    18     printf("输入%d个整数:",n);
    19     for(int i=0; i<n; ++i){
    20         scanf("%d",&arr[i]);
    21     }
    22 }
    23 /* 打印 */
    24 void print(int* arr, int n)
    25 {
    26     for(int i=0; i<n; ++i){
    27         printf("%5d",arr[i]);
    28     } 
    29     printf("
    ");
    30 }
    31 int main()
    32 {    
    33     /* 输入并打印 */
    34     int arr[MAXSIZE] = {0};
    35     int n = 6;
    36     putin(arr,n);
    37     printf("逆序前如下:
    ");
    38     print(arr,n);
    39     /* 逆序后打印 */
    40     reverse(arr,n);//逆序
    41     printf("逆序后如下:
    ");
    42     print(arr,n);
    43     return 0;
    44 }
    逆序

    4、有序多项式合并

     1 #include<stdio.h>
     2 
     3 int main()
     4 {
     5     typedef struct arr Array;
     6     struct arr{
     7         int a;
     8         int i;
     9     };
    10     Array a[10]={{9,12},{4,8},{3,2}};
    11     Array b[10]={{26,19},{-4,8},{-13,6},{82,0}};
    12     Array tmp[10]={0};
    13     int ai=0,bi=0,ti=0;
    14 
    15     while(a[ai].a && b[bi].a){
    16         if(a[ai].i>b[bi].i)
    17             tmp[ti++] = a[ai++];
    18         else if(a[ai].i<b[bi].i)
    19             tmp[ti++] = b[bi++];
    20         else if(a[ai].i==b[bi].i){
    21             tmp[ti].a=a[ai].a+b[bi].a;
    22             ai++,bi++;
    23             if(tmp[ti].a){
    24                 tmp[ti].i=a[ai].i;
    25                 ti++;
    26             }        
    27         }
    28     }
    29 
    30     while(a[ai].a){
    31         tmp[ti++] = a[ai++];
    32     }
    33 
    34     while(b[bi].a){
    35         tmp[ti++] = b[bi++];
    36     }
    37 
    38     for(ti=0; tmp[ti].a; ++ti)
    39     {
    40         if(ti&&tmp[ti].a>0)
    41             printf("+ ");
    42         
    43         printf("%d",tmp[ti].a);
    44 
    45         if(tmp[ti].i)
    46             printf("x");
    47 
    48         if(tmp[ti].i)
    49             printf("%d ",tmp[ti].i);
    50     }
    51     printf("
    ");
    52 
    53     return 0;
    54 }
    有序多项式合并

    5、二分法查找

     1 #include <stdio.h>
     2 int main()
     3 {
     4     int arr[] = {1,3,9,12,32,41,45,62,75,77,82,95,100};
     5     int x = 82, count = 0; //查找82
     6     int left =0, right = 12, mid;
     7     
     8     while(left<=right)     //  <=
     9     {
    10         mid = (left + right) / 2;
    11         count++;       //对比次数
    12         printf("%d
    ", arr[mid]);
    13         if(arr[mid] < x)
    14             left = mid + 1;
    15         else if(arr[mid] > x)
    16             right = mid -1;
    17         else 
    18             break;        
    19     }
    20     
    21     printf("%d
    ", count);
    22     
    23     return 0;
    24 }
    有序,顺序表,二分查找

     6、顺序表值为X的元素查找、删除所有值为X的元素、查找最大值、次大值

     1 #include <stdio.h> 
     2 #define N 100
     3 int main () 
     4 {     /* 1.数组元素输入(数组下标0作为哨兵) */
     5     int a[N] = {0}, n;
     6     printf("请输入数组元素的个数:");
     7     scanf("%d",&n);
     8     printf("请输入%d个数组元素:",n);
     9     for(int i=1; i<=n; ++i){
    10         scanf("%d",&a[i]);
    11     }
    12     
    13     /* 2.查找K值返回下标 */
    14     int j, k;
    15     printf("请输入要查找的数组元素:");
    16     scanf("%d",&k);
    17     a[0] = k; //设置哨兵
    18     for(j = n; a[j] != k; --j);
    19     if(!j) printf("数组中查找不到%d
    ",k);
    20     else printf("%d在数组中的下标为%d
    ",k,j);
    21     
    22     /* 3.删除所有值为K的元素 */
    23     int count = 0;
    24     for(int i=1; i<=n; ++i){
    25         if(a[i] != k){            
    26             count++;
    27             a[count] = a[i];
    28         }
    29     }    
    30     n = count; 
    31     printf("删除%d后,剩余元素%d
    ", k,n);
    32     
    33     /* 4.查找最大值,次大值 */
    34     int max, submax;
    35     max = submax = a[1];
    36     a[1]>a[2]?submax=a[2]:max = a[2];//最大值次大值初始值
    37     for(int i=3; i<=n; ++i){
    38         if(a[i]>max){
    39             submax = max;
    40             max = a[i];
    41         }
    42         else if(a[i]>submax){
    43             submax = a[i];
    44         }
    45     }
    46     printf("最大值:%d,次大值:%d
    ",max,submax);    
    47     return 0; 
    48 }
    查找删除

    7、seqlist代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> 
     
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW -2
     
    #define LIST_INIT_SIZE 100
    #define LISTINCREMENT 10
     
    typedef int Status;
    typedef int ElemType;
    typedef struct {
        ElemType *elem;
        int length;
        int listsize;
    }SqList;
    //操作结果:构造一个空的线性表L
    Status InitList(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 ResizeList(SqList *L,int increment){
        ElemType *newbase = (ElemType*)realloc(L->elem,(L->listsize+increment)*sizeof(ElemType));
        if(!newbase) exit(OVERFLOW);
     
        L->elem = newbase;
        L->listsize += increment;
        return OK;
    }
     
    //初始条件:线性表L已存在
    //操作结果:销毁线性表L
    Status DestroyList(SqList *L){
        if(0==L->listsize) return ERROR;
        free(L->elem);
        L->elem = NULL;
        L->length = 0;
        L->listsize = 0;
        return OK;
    }
    //初始条件:线性表L已存在
    //操作结果:将L重置为空表
    Status clearList(SqList *L){
        if(0==L->listsize) return ERROR;
        L->length = 0;
        return OK;
    }
    //初始条件:线性表L已存在
    //操作结果:若L为空表,则返回TRUE,否则返回FALSE
    Status ListEmpty(SqList L){
        return (L.length>0) ? FALSE : TRUE;
    }
    //初始条件:线性表L已存在
    //操作结果:返回L中数据元素个数
    Status ListLength(SqList L){
        return L.length;
    }
    //初始条件:线性表L已存在,1<=i<=ListLength(L)
    //操作结果:用e返回L中第i个数据元素的值
    Status GetElem(SqList L,int i,ElemType *e){
        if(i<1 || i>ListLength(L)) return ERROR;
        *e = L.elem[i-1];
        return OK;
    }
    //初始条件:线性表L已存在,1<=i<=ListLength(L)
    //操作结果:用e改写L中第i个数据元素的值
    Status SetElem(SqList L,int i,ElemType e){
        if(i<1 || i>ListLength(L)) return ERROR;
        L.elem[i-1] = e;
        return OK;
    }
    int my_compare(ElemType a,ElemType b){
        return a==b;
    }
    //初始条件:线性表L已存在,compare()就数据元素判定函数
    //操作结果:返回L中第一个给与e满足关系compare()的数据元素的位序。若这样的数据元素不存在,则返回值为0。
    Status LocateElem(SqList L,ElemType e,int (*compare)(ElemType,ElemType)){
        for(int i=1;i<=L.length;i++){
            if(compare(L.elem[i-1],e)) return i;
        }
        return 0;
    }
     
    //初始条件:线性表L已存在
    //操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,否则操作失败,pre_e无定义
    Status PriorElem(SqList L,ElemType cur_e,ElemType *pre_e){
        int i = LocateElem(L,cur_e,my_compare);
        if(i>1){
            *pre_e = L.elem[i-2];
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //初始条件:线性表L已存在
    //操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义
    Status NextElem(SqList L,ElemType cur_e,ElemType *next_e){
        int i = LocateElem(L,cur_e,my_compare);
        if(i>0 && i<L.length){
            *next_e = L.elem[i];
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //初始条件:线性表L已存在,1<=i<=ListLength(L)+1
    //操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1
    Status ListInsert(SqList *L,int i,ElemType e){
        if(i<1 || i>L->length+1) return ERROR;
     
        //若存储空间已满,需开辟新空间
        if(L->length*sizeof(ElemType)>=L->listsize) ResizeList(L,LISTINCREMENT);
     
        ElemType *q=&(L->elem[i-1]);
        for(ElemType *p=&(L->elem[L->length-1]);p>=q;p--){
            *(p+1)=*p;
        }
        *q=e;
        L->length++;
        return OK;
    }
    //初始条件:线性表L已存在
    //操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1
    Status ListDelete(SqList *L,int i,ElemType *e){
        if(i<1 || i>L->length) return ERROR;
     
        ElemType *p =&(L->elem[i-1]);
        *e = *p;
        ElemType *q=&(L->elem[L->length-1]);
     
        while(p<q){
            *p=*(p+1);
            p++;
        }
        L->length--;
        return OK;
    }
    //初始条件:线性表L已存在
    //操作结果:依次对L的每个数据元素调用函数visit()。一旦visit()失败,则操作失败。
    Status ListTraverse(SqList L,void visit(ElemType)){
        for(int i=0;i<L.length;i++) visit(L.elem[i]);
        return OK;
    }
     
    void my_print(ElemType e){
        printf("%d,",e);
    }
     
    void Union(SqList *La,SqList Lb){
        int La_len = ListLength(*La);
        int Lb_len = ListLength(Lb);
        ElemType e;
        for(int i=1;i<=Lb_len;i++){
            GetElem(Lb,i,&e);
            if(!LocateElem(*La,e,my_compare))
                ListInsert(La,++La_len,e);
        }
    }
     
    //将链表按升序进行排序
    void sortAscList(SqList L){
        ElemType e,f,min;
        int min_pos;
        int len = L.length;
        for(int i=1;i<=len;i++){
            GetElem(L,i,&e);
            min=e;
            for(int j=i+1;j<=len;j++){
                GetElem(L,j,&f);
                if(min>f){
                    min=f;
                    min_pos=j;
                }
            }
     
            if(min!=e){
                SetElem(L,i,min);
                SetElem(L,min_pos,e);
            }
     
        }
    }
     
    //已知线性表La和Lb中的数据元素按值非递减排列
    //归并La和Lb得到新的线性表Lc,Lc的数据元素也按值非递减排列
    void MergeList(SqList La,SqList Lb,SqList *Lc){
        InitList(Lc);
        int alen=La.length;
        int blen=Lb.length;
        int i=1,j=1,k=0;
        ElemType ai,bj;
        while(i<=alen && j<=blen){
            GetElem(La,i,&ai);
            GetElem(Lb,j,&bj);
     
            if(ai<bj){
                ListInsert(Lc,++k,ai);
                i++;
            }else{
                ListInsert(Lc,++k,bj);
                j++;
            }
        }
     
        while(i<=alen){
            GetElem(La,i,&ai);
            ListInsert(Lc,++k,ai);
            i++;
        }
     
        while(j<=blen){
            GetElem(Lb,j,&bj);
            ListInsert(Lc,++k,bj);
            j++;
        }
    }
     
    int main()
    {
        SqList L;
        ElemType e;
        printf("构造空List
    ");
        InitList(&L);
        if(ListEmpty(L)) printf("List为空
    ");
            else printf("List不为空
    ");
        printf("向List填充数据元素
    ");
        for(int i=1;i<11;i++){
            ListInsert(&L,L.length+1,i*10);
            //printf("%d,",i);
        }
     
        if(ListEmpty(L)) printf("List为空
    ");
            else printf("List不为空
    ");
        printf("打印List:");
        ListTraverse(L,my_print);
        printf("共%d个
    ",ListLength(L));
        GetElem(L,6,&e);
        printf("第%d个元素为%d
    ",6,e);
     
     
        ListDelete(&L,3,&e);
        printf("删除第%d个元素为%d
    ",3,e);
        printf("打印List:");
        ListTraverse(L,my_print);
        printf("共%d个
    ",ListLength(L));
     
        ListInsert(&L,3,33);
        printf("在第%d位插入元素为%d
    ",3,33);
        printf("打印List:");
        ListTraverse(L,my_print);
        printf("共%d个
    ",ListLength(L));
     
        e=60;
        int m=LocateElem(L,e,my_compare);
        if(m>0) printf("查找到值为%d的数据,在第%d个
    ",e,m);
            else printf("查找不到值为%d的数据
    ",e);
     
        ElemType next_e;
        GetElem(L,1,&e);
        printf("顺序打印List:%d,",e);
        while(NextElem(L,e,&next_e)){
            printf("%d,",next_e);
            e=next_e;
        }
        printf("
    ");
     
        ElemType pre_e;
        GetElem(L,ListLength(L),&e);
        printf("逆序打印List:%d,",e);
        while(PriorElem(L,e,&pre_e)){
            printf("%d,",pre_e);
            e=pre_e;
        }
        printf("
    ");
     
        clearList(&L);
        printf("清空List
    ");
        if(ListEmpty(L)) printf("List为空
    ");
            else printf("List不为空
    ");
     
        printf("销毁List
    ");
        DestroyList(&L);
     
     
        SqList La;
        InitList(&La);
        for(int i=1;i<11;i++){
            ListInsert(&La,La.length+1,i*10);
        }
        printf("打印La:");
        ListTraverse(La,my_print);
        printf("共%d个
    ",ListLength(La));
     
        SqList Lb;
        InitList(&Lb);
        for(int i=1;i<11;i++){
            ListInsert(&Lb,Lb.length+1,i);
        }
        printf("打印Lb:");
        ListTraverse(Lb,my_print);
        printf("共%d个
    ",ListLength(Lb));
     
        printf("合并La、Lb
    ");
        Union(&La,Lb);
        printf("打印La:");
        ListTraverse(La,my_print);
        printf("共%d个
    ",ListLength(La));
     
     
        srand(time(NULL));
        SqList Le;
        InitList(&Le);
        for(int i=1;i<11;i++){
            ListInsert(&Le,Le.length+1,rand()%11);
        }
        printf("打印Le:");
        ListTraverse(Le,my_print);
        printf("共%d个
    ",ListLength(Le));
     
        SqList Lf;
        InitList(&Lf);
        for(int i=1;i<11;i++){
            ListInsert(&Lf,Lf.length+1,rand()%11*10);
        }
        printf("打印Lf:");
        ListTraverse(Lf,my_print);
        printf("共%d个
    ",ListLength(Lf));
     
     
        printf("排序Le
    ");
        sortAscList(Le);
        printf("打印Le:");
        ListTraverse(Le,my_print);
        printf("共%d个
    ",ListLength(Le));
     
        printf("排序Lf
    ");
        sortAscList(Lf);
        printf("打印Lf:");
        ListTraverse(Lf,my_print);
        printf("共%d个
    ",ListLength(Lf));
     
        SqList Lg;
        printf("归并Le,Lf到Lg:
    ");
        MergeList(Le,Lf,&Lg);
     
        printf("打印Lg:");
        ListTraverse(Lg,my_print);
        printf("共%d个
    ",ListLength(Lg));
     
        return 0;
    }
    seqlist

    8、seqlist文件代码

     1 typedef int Status;
     2 typedef int ElemType;
     3 typedef struct sqlist{
     4     ElemType *elem;
     5     int length;
     6     int list_size;
     7 }sqlist, *ptr;
     8 
     9 Status list_init(sqlist *L);
    10 Status ResizeList(sqlist *L,int increment);
    11 void list_destroy(sqlist *L);
    12 void list_clear(sqlist *L);
    13 int list_empty(sqlist *L);
    14 int list_length(sqlist *L);
    15 ElemType Getelem(sqlist *L, int i);
    16 int compare(ElemType a, ElemType b);
    17 ElemType LocateElem(sqlist *L, ElemType e, int (*compare)(ElemType , ElemType ));
    18 Status PriorElem(sqlist *L, ElemType cur_e, ElemType *pre_e);
    19 Status list_insert(sqlist *L, int i, ElemType e);
    20 Status list_delete(sqlist *L, int i, ElemType *e);
    21 void list_print(sqlist *L);
    seqlist.h
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include "sqlist.h"
      4 
      5 #define list_maxsize 100
      6 #define list_increament 10
      7 #define success 1
      8 #define fail 0
      9 
     10 Status list_init(sqlist *L){
     11 
     12     Status s;
     13     s= fail; 
     14     /*L指针所指结构体的指针域elem指向一个申请的空间*/
     15     L->elem = (ElemType*)malloc(list_maxsize*sizeof(ElemType));
     16     
     17     if (L->elem){
     18             L->length = 0;
     19             L->list_size = list_maxsize;
     20             s = success;
     21     }
     22     return s;
     23 
     24 }
     25 
     26 Status ResizeList(sqlist *L,int increment){
     27     Status s;
     28     ElemType* newbase;
     29     s= fail; 
     30     /*realloc函数扩充顺序表空间*/
     31     newbase = (ElemType*)realloc(L->elem,(L->list_size+increment)*sizeof(ElemType));
     32     if(newbase){
     33         L->elem = newbase;
     34         L->list_size += increment;
     35         s = success;
     36     }
     37     return s;
     38 }
     39 
     40 void list_destroy(sqlist *L){
     41     
     42     free(L->elem); /*释放空间*/
     43     
     44     L->elem = NULL;
     45     L->length = 0;
     46     L->list_size = 0;
     47     
     48 }
     49 
     50 void list_clear(sqlist *L){
     51 
     52         L->length = 0;
     53 }
     54 
     55 int list_empty(sqlist *L){
     56 
     57     return  L->length == 0 ? 1 : 0;
     58 
     59 }
     60 
     61 int list_length(sqlist *L){
     62 
     63     return L->length;
     64 
     65 }
     66 
     67 ElemType Getelem(sqlist *L, int i){
     68     
     69     return L->elem[i - 1];
     70     
     71 }
     72 
     73 int compare(ElemType a,ElemType b){
     74 
     75     return a == b;
     76 
     77 }
     78 
     79 ElemType LocateElem(sqlist *L, ElemType e, int (*compare)(ElemType ,ElemType )){
     80     int i = 1;
     81     for (; i <= L->length; i++){
     82         if ((*compare)((*L).elem[i-1], e)){
     83             return i;
     84         }
     85     }
     86     return 0; /*位序>=1,没找到==0*/
     87 }
     88 
     89 Status PriorElem(sqlist *L, ElemType cur_e, ElemType *pre_e){
     90     
     91     Status s;
     92     int i;
     93     s = fail;
     94     i = LocateElem(L, cur_e, compare);
     95     if (i>1){
     96         *pre_e = L->elem[i-2];
     97         s = success;
     98     }
     99     return s;
    100 
    101 }
    102 
    103 Status list_insert(sqlist *L, int i, ElemType e){
    104 
    105     Status s = fail;
    106     int j;
    107 
    108     if (i< 1 || i >L->length + 1)
    109         return s;
    110 
    111     if (L->length >= L->list_size)
    112     {
    113         if(!ResizeList(L,list_increament))
    114             return s;
    115     }
    116     
    117     for (j = L->length - 1; j >= i - 1; i--)
    118         L->elem[j + 1] = L->elem[j];
    119     L->elem[i - 1] = e;
    120     L->length++;
    121     s = success;
    122 
    123     return s;
    124 
    125 }
    126 
    127 Status list_delete(sqlist *L, int i,ElemType *e){
    128 
    129     Status s = fail;
    130     int j;
    131 
    132     if (i<1 || i>L->length)
    133         return s;
    134 
    135     *e = L->elem[i - 1];
    136     for (j = i; j <= L->length-1; j++)
    137         L->elem[j - 1] = L->elem[j];
    138     L->length--;
    139     return success;
    140 
    141 }
    142 
    143 
    144 void list_print(sqlist *L){
    145     int i;
    146     
    147     for (i = 0; i <= L->length-1; i++)
    148         printf("%d ", L->elem[i]);
    149     printf("
    ");
    150     
    151 }
    seqlist.c
    #include "sqlist.h"
    
    Status Test_createlist(sqlist *L,ElemType data[],int n);
    Status Test_clearlist(sqlist *L);
    testlist.h
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include "testlist.h"
     4 
     5 #define success 1
     6 #define fail 0
     7 
     8 Status Test_createlist(sqlist *L, ElemType data[], int n)
     9 {
    10 
    11     Status s = fail;
    12     int i;
    13     s = list_init(L);
    14     
    15     if (s == success)
    16     {
    17         for (i = 1; i <= n; i++){
    18             s = list_insert(L, i, data[i-1]);
    19         if (s != success)
    20                 break;
    21         }
    22         list_print(L);
    23     }
    24     return s;
    25 
    26 }
    27 
    28 Status Test_clearlist(sqlist *L)
    29 {
    30     Status s;
    31     if (!list_empty(L)){
    32         list_clear(L);
    33         s = list_empty(L);
    34     }
    35     return s;
    36 }
    testlist.c
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include "testlist.h"
     4 
     5 #define success 1
     6 #define fail 0
     7 
     8 int main()
     9 {
    10     int opt,i;
    11     sqlist List;
    12     ElemType *data;
    13     int size;
    14     Status s;
    15 
    16     printf("input switch opt 1or2 and list size
    ");
    17     scanf("%d%d",&opt,&size);
    18 
    19     switch (opt)
    20     {
    21     case 1:
    22         printf("the length of the list is %d
    ", size);
    23         printf("the data of the list are 
    ");
    24         data = (ElemType*)malloc(size*sizeof(ElemType));
    25         if (!data) break;
    26         for ( i = 0; i < size; i++)
    27             data[i] = i;
    28         s = Test_createlist(&List, data, size);
    29         if (s != success)
    30             printf("fail creating the list
    ");
    31         break;
    32     case 2:
    33         data = (ElemType*)malloc(size*sizeof(ElemType));
    34         if (!data) break;
    35         for (i = 0; i < size; i++)
    36         data[i] = i;
    37         s = Test_createlist(&List, data, size);
    38 
    39         if (s == success){
    40             if (!list_empty(&List)){
    41                 list_clear(&List);
    42                 if (list_empty(&List))
    43                     printf("the list has been cleared
    ");
    44             }
    45         }
    46         break;
    47     default:
    48         break;
    49     }
    50     system("pause");
    51     return 0;
    52 }
    main.c

    三、特殊矩阵的压缩存储

    稀疏矩阵,三元组,顺序存储十字链表链式存储

    #define MAXSIZE 1000
    typedef int ElemType;
    typedef struct{
        int row,cow /* 行列 */
        ElemType e; /**/
    }Triple; /* 三元组 */
    
    typedef struct{
        Triple Data[MAXSIZE+1];
        int m, n, len;
    }TSMatrix;

    稀疏矩阵,三元组表示法的快速转置,算法:

    source矩阵,向dest矩阵转置:

    1、计算原矩阵的每一列 (col), 转置后为目标矩阵的行(row),原矩阵每一列元素的个数,用一个数组 num[col] 记录,可以桶排序计算;

    2、计算,转置后,每个三元组的元素,所在的位置,用一个数组 pos[col] 记录,原矩阵第一列即目标矩阵第一行,原矩阵第一列+该列元素个数,即为目标矩阵第二行元素的位置,依次类推,目标矩阵某行元素出现一次,pos[col]++,用以确定该行再次出现的元素的位置;下图,pos[col]的值,第一列0~1变化,第二列2~3变化,...

  • 相关阅读:
    《我与我的父辈》影评
    如何进行时间规划?
    内向者相关
    修己 0815
    loj 3102
    StringSequences
    解方程
    problem B
    uoj424 count
    fft相关的复习
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10296168.html
Copyright © 2011-2022 走看看