/*02.文件名称:项目41.cpp
03.作者:高露
04.完成日期:2015.9.21
05.问题描述:
06.删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1)
07.输入描述:无
08.程序输出:删除前 5 8 7 0 2 4 9 6 7 3
09. 删除后 8 0 2 9 3
*/
?头文件list.h代码:
[cpp] view plaincopyprint?
01.#ifndef LIST_H_INCLUDED
02.#define LIST_H_INCLUDED
03.
04.#define MaxSize 50
05.#include <stdio.h>
06.#include <malloc.h>
07.typedef int ElemType;
08.typedef struct
09.{
10. ElemType data[MaxSize];
11. int length;
12.} SqList;
13.void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表
14.void InitList(SqList *&L);//初始化线性表InitList(L)
15.void DestroyList(SqList *&L);//销毁线性表DestroyList(L)
16.bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)
17.int ListLength(SqList *L);//求线性表的长度ListLength(L)
18.void DispList(SqList *L);//输出线性表DispList(L)
19.bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)
20.int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)
21.bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)
22.bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED
23.#endif
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#define MaxSize 50
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
} SqList;
void CreateList(SqList *&L, ElemType a[], int n);//用数组创建线性表
void InitList(SqList *&L);//初始化线性表InitList(L)
void DestroyList(SqList *&L);//销毁线性表DestroyList(L)
bool ListEmpty(SqList *L);//判定是否为空表ListEmpty(L)
int ListLength(SqList *L);//求线性表的长度ListLength(L)
void DispList(SqList *L);//输出线性表DispList(L)
bool GetElem(SqList *L,int i,ElemType &e);//求某个数据元素值GetElem(L,i,e)
int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)
bool ListInsert(SqList *&L,int i,ElemType e);//插入数据元素ListInsert(L,i,e)
bool ListDelete(SqList *&L,int i,ElemType &e);//删除数据元素ListDelete(L,i,e)#endif // LIST_H_INCLUDED
#endif
?功能函数list.cpp代码:
[cpp] view plaincopyprint?
01.#include "list.h"
02.//用数组创建线性表
03.void CreateList(SqList *&L, ElemType a[], int n)
04.{
05. int i;
06. L=(SqList *)malloc(sizeof(SqList));
07. for (i=0; i<n; i++)
08. L->data[i]=a[i];
09. L->length=n;
10.}
11.
12.//初始化线性表InitList(L)
13.void InitList(SqList *&L) //引用型指针
14.{
15. L=(SqList *)malloc(sizeof(SqList));
16. //分配存放线性表的空间
17. L->length=0;
18.}
19.
20.//销毁线性表DestroyList(L)
21.void DestroyList(SqList *&L)
22.{
23. L=(SqList *)malloc(sizeof(SqList));
24. free(L);
25.}
26.
27.//判定是否为空表ListEmpty(L)
28.bool ListEmpty(SqList *L)
29.{
30. return(L->length==0);
31.}
32.
33.//求线性表的长度ListLength(L)
34.int ListLength(SqList *L)
35.{
36. return(L->length);
37.}
38.
39.//输出线性表DispList(L)
40.void DispList(SqList *L)
41.{
42. int i;
43. if (ListEmpty(L)) return;
44. for (i=0; i<L->length; i++)
45. printf("%d ",L->data[i]);
46. printf("\n");
47.}
48.
49.//求某个数据元素值GetElem(L,i,e)
50.bool GetElem(SqList *L,int i,ElemType &e)
51.{
52. if (i<1 || i>L->length) return false;
53. e=L->data[i-1];
54. return true;
55.}
56.
57.//按元素值查找LocateElem(L,e)
58.int LocateElem(SqList *L, ElemType e)
59.{
60. int i=0;
61. while (i<L->length && L->data[i]!=e) i++;
62. if (i>=L->length) return 0;
63. else return i+1;
64.}
65.
66.//插入数据元素ListInsert(L,i,e)
67.bool ListInsert(SqList *&L,int i,ElemType e)
68.{
69. int j;
70. if (i<1 || i>L->length+1)
71. return false; //参数错误时返回false
72. i--; //将顺序表逻辑序号转化为物理序号
73. for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置
74. L->data[j]=L->data[j-1];
75. L->data[i]=e; //插入元素e
76. L->length++; //顺序表长度增1
77. return true; //成功插入返回true
78.}
79.
80.//删除数据元素ListDelete(L,i,e)
81.bool ListDelete(SqList *&L,int i,ElemType &e)
82.{
83. int j;
84. if (i<1 || i>L->length) //参数错误时返回false
85. return false;
86. i--; //将顺序表逻辑序号转化为物理序号
87. e=L->data[i];
88. for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移
89. L->data[j]=L->data[j+1];
90. L->length--; //顺序表长度减1
91. return true; //成功删除返回true
92.}
#include "list.h"
//用数组创建线性表
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;
}
//初始化线性表InitList(L)
void InitList(SqList *&L) //引用型指针
{
L=(SqList *)malloc(sizeof(SqList));
//分配存放线性表的空间
L->length=0;
}
//销毁线性表DestroyList(L)
void DestroyList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList));
free(L);
}
//判定是否为空表ListEmpty(L)
bool ListEmpty(SqList *L)
{
return(L->length==0);
}
//求线性表的长度ListLength(L)
int ListLength(SqList *L)
{
return(L->length);
}
//输出线性表DispList(L)
void DispList(SqList *L)
{
int i;
if (ListEmpty(L)) return;
for (i=0; i<L->length; i++)
printf("%d ",L->data[i]);
printf("\n");
}
//求某个数据元素值GetElem(L,i,e)
bool GetElem(SqList *L,int i,ElemType &e)
{
if (i<1 || i>L->length) return false;
e=L->data[i-1];
return true;
}
//按元素值查找LocateElem(L,e)
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;
}
//插入数据元素ListInsert(L,i,e)
bool ListInsert(SqList *&L,int i,ElemType e)
{
int j;
if (i<1 || i>L->length+1)
return false; //参数错误时返回false
i--; //将顺序表逻辑序号转化为物理序号
for (j=L->length; j>i; j--) //将data[i..n]元素后移一个位置
L->data[j]=L->data[j-1];
L->data[i]=e; //插入元素e
L->length++; //顺序表长度增1
return true; //成功插入返回true
}
//删除数据元素ListDelete(L,i,e)
bool ListDelete(SqList *&L,int i,ElemType &e)
{
int j;
if (i<1 || i>L->length) //参数错误时返回false
return false;
i--; //将顺序表逻辑序号转化为物理序号
e=L->data[i];
for (j=i; j<L->length-1; j++) //将data[i..n-1]元素前移
L->data[j]=L->data[j+1];
L->length--; //顺序表长度减1
return true; //成功删除返回true
}
?main.cpp测试文件代码:
[cpp] view plaincopyprint?
01.#include "list.h"
02.#include <stdio.h>
03.
04.//删除线性表中,元素值在x到y之间的元素
05.void delx2y(SqList *&L, ElemType x, ElemType y)
06.{
07. int k=0,i; //k记录非x的元素个数
08. ElemType t;
09. if(x>y)//使输出到下面的数都为前面小 后面小的形式
10. {
11. t=x;
12. x=y;
13. y=t;
14. }
15. for (i=0; i<L->length; i++)
16. if (L->data[i]<x || L->data[i]>y ) //复制不在[x, y]之间的元素
17. {
18. L->data[k]=L->data[i]; //k增加 并且每增加一个新赋值一个
19. k++;
20. }
21. L->length=k;
22.}
23.
24.//用main写测试代码
25.int main()
26.{
27. SqList *sq;
28. ElemType a[12]= {2,0,1,4,5,8,5,0,6,1,1,4};
29. CreateList(sq, a, 12);
30. printf("删除前 ");
31. DispList(sq);
32.
33. delx2y(sq, 1, 5);//主要运用此函数删除
34.
35. printf("删除后 ");
36. DispList(sq);
37. return 0;
38.}
运行结果: