数据结构(一)线性表——顺序表

一、顺序表基本操作的实现

通常把顺序存储结构实现的线性表称为顺序表。

1.状态类型Status的定义

#define TRUE 1
#define FALSE 0 
#define OK 1 
#define ERROR 0 
#define INFEASIBLE -1
#define OVERFLOW -2  

typedef int Status;
typedef char ElemType;

2.顺序表类型SqList的定义

#define ListSpaceIncr 20
typedef struct
{
    LElemType * base;//LElemType为顺序表的元素类型,base表示存储空间的动态数组 
    int length;//顺序表长度 
    int listSize;//当前的存储空间大小 
}SqList;

3.初始化操作InitSqList(&L,InitSize)

Status InitSqList(SqList &L,int InitSize)
{
    L.base=(LElemType *)malloc(InitSize * sizeof(LElemType));//申请存储空间 
    if(!L.base) return(OVERFLOW);//若申请失败 
    L.length=0;//置为空表 
    L.listSize=InitSize;
    return OK;
}

4.求长度操作listLength(L)

int listLength(SqList L)
{
    return L.length;
} 

5.判空操作listIsEmpty(L)

Status lengthIsEmpty(SqList L)
{
    if(!L.length) return TRUE;//空返回true,非空返回false 
    else return FALSE;
}

6.清空操作clearList(&L)

void clearList(SqList &L)
{
    L.length=0;//将顺序表清空 
}

7.取元素操作getElem(L,I,&e)

Status getElem(SqList L,int i,LElemType &e)
{
    if(!L.length) return ERROR;//先判断顺序表是否为空 
    e=L.base[i-1];//由参数e返回第i个元素 
    return OK;
} 

8.按值查找操作locateElem(L,e)

int locateElem(SqList L,LElemType e)
{
    int i=0;
    while(i<L.length&&! equal(L.base[i],e))
    i++;
    if(i<L.length)
    return i+1;//找到返回位序 
    else return 0;//未找到返回0 
} 

9.插入操作listInsert(&L,i,e)

Status listInsert(SqList &L,int i,LElemType e)//在顺序表L的第i个位置插入元素e 
{
    LElemType * newBase;
    int j;
    if(i<1||i>L.length+1) return ERROR;//先判断插入位置 
    if(L.length==L.listSize)
    {
        newBase=(LElemType *)realloc(L.base,(L.listSize+ListSpaceIncr)*sizeof(LElemType));//追加存储空间 
        if(!newBase) return OVERFLOW;//失败 
        L.base=newBase;//成功 
        L.listSize+=ListSpaceIncr;
    }
    for(j=L.length-1;j>=i-1;j--)
        L.base[j+1]=L.base[j];//元素后移 
    L.base[i-1]=e;//插入e 
    L.length++;//长度增加1 
    return OK;    
} 

10.删除操作listDelete(&L,i,&e)

Status listDelete(SqList &L,int i,LElemType &e)//删除顺序表的第i个元素并返回其值 
{
    int j;
    if(i<1||i>L.length) return ERROR;//若不存在 
    e=L.base[i-1];
    for(j=i;j<L.length;j++)//元素前移 
        L.base[j-1]=L.base[j];
    L.length--;//长度减1 
    return OK;
} 

11.比较操作equal(e1,e2)

Status equal(LElemType e1,LElemType e2)//比较 
{
    if(e1==e2) return TRUE;
    else return FALSE;
}

12.遍历操作listTraverse(L)

void listTraverse(SqList L)
{
    int i;        
    for(i=0;i<L.length;i++)       
    {               
        printf("%d",L.base[i]);//遍历顺序表    
    }         
} 

把以上的所有内容保存到一个程序文件SqList.h中,以后使用的时候就可以直接调用顺序表的头文件来实现相应的操作了。

二、顺序表应用——集合表示与实现

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "Status.h"
 4 typedef int LElemType;
 5 #include "SqList.h"
 6 typedef SqList mySetType;
 7 void visitSetElem(LElemType e)
 8 {
 9     printf("%d",e);
10 }
11 void createSet(mySetType &A,int n)
12 {
13     int i,e;
14     InitSqList(A,n+10);
15     printf("	 输入%d个整数:",n);
16     for(i=0;i<n;i++)
17     {
18         scanf("%d",&e);
19         listInsert(A,i+1,e); //调用插入函数 
20     } 
21 } 
22 void setUnion(mySetType A,mySetType B,mySetType &C)
23 {
24     int i,k,len,e;
25     clearList(C);//调用清空函数 
26     k=0;
27     len=listLength(A);
28     for(i=1;i<=len;i++)
29     {
30         getElem(A,i,e);
31         listInsert(C,++k,e);//调用插入函数 
32     } 
33     len=listLength(B);
34     for(i=1;i<=len;i++)
35     {
36         getElem(B,i,e);
37         if(!locateElem(A,e)) listInsert(C,++k,e);//调用按值查找函数 
38     }
39 } 
40 void setIntersection(mySetType &A,mySetType B)
41 {
42     int i,e,len;
43     len=listLength(A);
44     i=1;
45     while(i<=len)
46     {
47         getElem(A,i,e);
48         if(!locateElem(B,e))
49         {
50             listDelete(A,i,e);//调用删除函数 
51             len--;
52         } 
53         else i++;
54     } 
55 }
56 void outputSet(mySetType A)
57 {
58     printf("{");
59     listTraverse(A);//调用遍历函数 
60     printf("}");
61 }
62 int main()
63 {
64     mySetType A,B,C;
65     int n;
66     printf("创建Set A:
");
67     printf("	元素数:");
68     scanf("%d",&n);
69     createSet(A,n);
70     printf("Set A=");
71     outputSet(A);
72     printf("创建Set B:
");
73     printf("	元素数:");
74     scanf("%d",&n);
75     createSet(B,n);
76     printf("Set B=");
77     outputSet(B); 
78     InitSqList(C,listLength(A)+listLength(B)+10);
79     setUnion(A,B,C);
80     printf("
 Set C=A并B=");
81     outputSet(C);
82     setIntersection(A,B);
83     printf("Set A=A交B=");
84     outputSet(A);
85     system("pause");
86     return 0; 
87 }

运行结果:

原文地址:https://www.cnblogs.com/wxywxy/p/6821475.html