第三周 项目4-顺序表应用 (1)

来源:互联网 发布:win7开启443端口 编辑:程序博客网 时间:2024/06/05 10:00

list.h 代码:

/*   *Copyright (c) 2017,烟台大学计算机与控制工程学院   *All rights reserved.   *文件名称:   *作    者:陈军正   *完成日期:2017年9月20日   *版 本 号:v1.0   *   *问题描述:删除元素在[x,y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1)
[
x
,
y
]
O
(
n
)
O
(
1
[
x
,
y
]
O
(
n
)
O
(
1
[
x
,
y
]
O
(
n
)
O
(
1
**/ #ifndef LIST_H_INCLUDED#define LIST_H_INCLUDED#define MaxSize 50typedef struct{ int data [MaxSize]; int length;}SqList;void CreateList(SqList *&,int [],int );void DispList(SqList *&);void Add (SqList *&L1,SqList *&L2,SqList *&L);void DeList (SqList *&L,int y,int z);#endif // LIST_H_INCLUDED
main.cpp 代码:

#include <iostream>#include "list.h"using namespace std;int main (){    int y,z;    int x[6] = {1,2,3,4,5,6};    SqList *sq;    CreateList(sq,x,6);    DispList(sq);    cout<<endl<<"请输入您想要的删除的数据范围:";    cin>>y>>z;    DeList(sq,y,z);    DispList(sq);    return 0;}

list.cpp代码:

#include "malloc.h"#include "list.h"#include <iostream>using namespace std;void CreateList(SqList *&L,int 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 DispList(SqList *&L){    int i;    for(i=0;i<L->length;++i)    {        cout<<L->data[i]<<" ";    }}void DeList (SqList *&L,int y,int z){    int i = z-y+1;    int n;    --y;    n=L->length;    while(z<n)    {        L->data[y] = L->data[z];        ++y;++z;    }    L->length = L->length-i;}
运行结果:

知识点总结:注意题目要求,算法复杂度和空间复杂的