您的位置:首页 > 其它

第三周项目1 顺序表的基本运算

2015-09-21 16:30 453 查看
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
} SqList;

void CreateList(SqList *&L, ElemType a[], int n);
bool ListEmpty(SqList *L);
void DispList(SqList *L);
#endif

int main()
{
SqList *sq;
ElemType x[8]= {1,9,9,6,0,6,0,1};
CreateList(sq, x, 8);

if((ListEmpty(sq))>0)
printf("是空表\n");
else
printf("不是空表\n");

DispList(sq);

return 0;
}

void CreateList(SqList *&L, ElemType a[], int n)
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0; i<n; i++)
L->data[i]=a[i];
L->length=n;
}

bool ListEmpty(SqList *L)
{
return(L->length==0);
}
void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0; i<L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}




#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
} SqList;
void CreateList(SqList *&L, ElemType a[], int n);
bool ListEmpty(SqList *L);
void DispList(SqList *L);
int ListLength(SqList *L);
bool GetElem(SqList *L,int i,ElemType &e);
int LocateElem(SqList *L, ElemType e);
#endif

int main()
{
SqList *sq;
int k;
ElemType E;
ElemType x[8]= {1,9,9,6,0,5,0,1};
CreateList(sq, x, 8);

printf("表长度:%d\n", ListLength(sq));

if(GetElem(sq, 3, E))
printf("找到了第3个元素值为:%d\n", E);
else
printf("第3个元素超出范围!\n");

if(GetElem(sq, 15, E))
printf("找到了第15个元素值为:%d\n", E);
else
printf("第15个元素超出范围!\n");

if((k=LocateElem(sq, 1))>0)
printf("找到了,值为1的元素是第 %d 个\n", k);
else
printf("值为1的元素木有找到!\n");

if((k=LocateElem(sq, 16))>0)
printf("找到了,值为16的元素是第 %d 个\n", k);
else
printf("值为16的元素木有找到!\n");

return 0;
}

void CreateList(SqList *&L, ElemType a[], int n)
{
int i;
L=(SqList *)malloc(sizeof(SqList));
for (i=0; i<n; i++)
L->data[i]=a[i];
L->length=n;
}

bool ListEmpty(SqList *L)
{
return(L->length==0);
}

int ListLength(SqList *L)
{
return(L->length);
}

void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0; i<L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}

bool GetElem(SqList *L,int i,ElemType &e)
{
if (i<1 || i>L->length)  return false;
e=L->data[i-1];
return true;
}

int LocateElem(SqList *L, ElemType e)
{
int i=0;
while (i<L->length && L->data[i]!=e) i++;
if (i>=L->length)  return 0;
else  return i+1;
}




#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
} SqList;
void DispList(SqList *L);
bool ListEmpty(SqList *L);
void InitList(SqList *&L);
void DestroyList(SqList *&L);
bool ListInsert(SqList *&L,int i,ElemType e);
bool ListDelete(SqList *&L,int i,ElemType &e);
#endif

int main()
{
SqList *sq;
ElemType E;

printf("初始化线性表\n");
InitList(sq);

printf("在第1个位置插入元素1\n");
ListInsert(sq, 1, 1);
DispList(sq);

printf("在第2个位置插入元素6\n");
ListInsert(sq, 2, 6);
DispList(sq);

printf("在第1个位置插入元素9\n");
ListInsert(sq, 1, 9);
DispList(sq);

printf("删除第2个位置的元素\n");
ListDelete(sq,2,E);
DispList(sq);

printf("销毁线性表\n");
DestroyList(sq);
DispList(sq);

return 0;
}

void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0; i<L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}

bool ListEmpty(SqList *L)
{
return(L->length==0);
}

void InitList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList));

L->length=0;
}

void DestroyList(SqList *&L)
{
free(L);
}

bool ListInsert(SqList *&L,int i,ElemType e)
{
int j;
if (i<1 || i>L->length+1)
return false;
i--;
for (j=L->length; j>i; j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
return true;
}

bool ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if (i<1 || i>L->length)
return false;
i--;
e=L->data[i];
for (j=i; j<L->length-1; j++)
L->data[j]=L->data[j+1];
L->length--;
return true;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: