第三周项目-4顺序表应用
来源:互联网 发布:国际网络不应该受管制 编辑:程序博客网 时间:2024/05/29 16:52
1.删除元素在[x, y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1);
(1) list.h的代码
- #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);
- void InitList(SqList *&L);
- void DestroyList(SqList *&L);
- bool ListEmpty(SqList *L);
- int ListLength(SqList *L);
- void DispList(SqList *L);
- bool GetElem(SqList *L,int i,ElemType &e);
- int LocateElem(SqList *L, ElemType e);
- bool ListInsert(SqList *&L,int i,ElemType e);
- bool ListDelete(SqList *&L,int i,ElemType &e);
- void delx2y(SqList *&L, ElemType x, ElemType y);
(2)list.cpp中的代码
- #include"list.h"
- void delx2y(SqList *&L, ElemType x, ElemType y)
- {
- int k=0,i;
- ElemType t;
- if(x>y)
- {
- t=x;
- y=x;
- y=t;
- }
- for(i=0;i<L->length;i++)
- if(L->data[i]<x||L->data[i]>y)
- {
- L->data[k]=L->data[i];
- k++;
- }
- L->length=k;
- }
-
-
-
-
- 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;
- }
-
-
- void InitList(SqList *&L)
- {
- L=(SqList *)malloc(sizeof(SqList));
-
- L->length=0;
- }
-
-
- void DestroyList(SqList *&L)
- {
- free(L);
- }
-
-
- 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;
- }
-
-
- 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;
- }
(3)main.cpp的代码
- #include"list.h"
- int main()
- {
- SqList *sq;
- ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};
- CreateList(sq, a, 10);
- printf("删除前 ");
- DispList(sq);
-
- delx2y(sq, 4, 7);
-
- printf("删除后 ");
- DispList(sq);
- return 0;
- }
运行结果:
2、将所在奇数移到所有偶数的前面,要求算法的时间复杂度为O(n),空间复杂度为O(1)。
(1)move.cpp中的代码
- #include"list.h"
- void move(SqList *&L)
- {
- int i=0,j=L->length-1;
- ElemType tmp;
- while (i<j)
- {
- while ((i<j) && (L->data[j]%2==0))
- j--;
- while ((i<j) && (L->data[i]%2==1))
- i++;
- if (i<j)
- {
- tmp=L->data[i];
- L->data[i]=L->data[j];
- L->data[j]=tmp;
- }
- }
- }
(2)main.cpp的代码
- #include"list.h"
- int main()
- {
- SqList *sq;
- ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};
- CreateList(sq, a, 10);
- printf("操作前 ");
- DispList(sq);
-
- move(sq);
-
- printf("操作后 ");
- DispList(sq);
- return 0;
- }
运行结果:
0 0