6-1 线性表元素的区间删除 (20分)
给定一个顺序存储的线性表,请设计一个函数删除所有值大于min而且小于max的元素。删除后表中剩余元素保持顺序存储,并且相对位置不能改变。
函数接口定义:
List Delete( List L, ElementType minD, ElementType maxD );
其中List结构定义如下:
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
L是用户传入的一个线性表,其中ElementType元素可以通过>、==、<进行比较;minD和maxD分别为待删除元素的值域的下、上界。函数Delete应将Data[]中所有值大于minD而且小于maxD的元素删除,同时保证表中剩余元素保持顺序存储,并且相对位置不变,最后返回删除后的表。
裁判测试程序样例:
#include <stdio.h>
#define MAXSIZE 20
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List ReadInput(); /* 裁判实现,细节不表。元素从下标0开始存储 */
void PrintList( List L ); /* 裁判实现,细节不表 */
List Delete( List L, ElementType minD, ElementType maxD );
int main()
{
List L;
ElementType minD, maxD;
int i;
L = ReadInput();
scanf("%d %d", &minD, &maxD);
L = Delete( L, minD, maxD );
PrintList( L );
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
10
4 -8 2 12 1 5 9 3 3 10
0 4
输出样例:
4 -8 12 5 9 10
代码
List Delete( List L, ElementType minD, ElementType maxD )
{
if(L==NULL)
{
return NULL;
}
int *p=L->Data;
int n=0;
while(p<=L->Data+L->Last)
{
if(*p>minD&&*p<maxD)
{
++n;
}
else
{
*(p-n)=*p;
}
++p;
}
L->Last-=n;
return L;
}
7-2 最长连续递增子序列 (20分)
给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。
输入格式:
输入第1行给出正整数n(≤10510^5105);第2行给出n个整数,其间以空格分隔。
输出格式:
在一行中输出第一次出现的最长连续递增子序列,数字之间用空格分隔,序列结尾不能有多余空格。
输入样例:
15
1 9 2 5 7 3 4 6 8 0 11 15 17 17 10
输出样例:
3 4 6 8
代码
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
typedef struct{
int length;
ElemType *p_start;
ElemType *p_end;
}ContinuousIncSub;
void GetLongestContinuousIncSub(SqList &L);
int main()
{
//freopen("data.txt","r",stdin);
int n;
scanf("%d",&n);
SqList L;
L.elem=(ElemType*)calloc(n,sizeof(ElemType));
if(!L.elem) exit(-1);
L.listsize=n;
L.length=0;
for(int i=0;i<n;++i)
{
scanf("%d",L.elem+i);
++L.length;
}
//求最长连续递增子序列
GetLongestContinuousIncSub(L);
//输出
for(int i=0;i<L.length;++i)
{
if(i==0)
printf("%d",*(L.elem));
else
printf(" %d",*(L.elem+i));
}
return 0;
}
//求最长连续递增子序列的函数
void GetLongestContinuousIncSub(SqList &L)
{
if(L.length==1)
return;
ContinuousIncSub cur_IncSub,max_IncSub;
//初始化
max_IncSub.p_start=max_IncSub.p_end=NULL;
max_IncSub.length=0;
cur_IncSub.p_start=cur_IncSub.p_end=NULL;
cur_IncSub.length=0;
int *p1=L.elem;
int *p2=L.elem+1;
cur_IncSub.p_start=p1;
cur_IncSub.length=1;
while(p1<L.elem+L.length&&p2<L.elem+L.length)
{
if(*p2>*(p2-1))
{
cur_IncSub.length++;
++p2;
}
if(p2>=L.elem+L.length)
{
cur_IncSub.p_end=p2;
if(cur_IncSub.length>max_IncSub.length)
{
max_IncSub.p_start=cur_IncSub.p_start;
max_IncSub.p_end=cur_IncSub.p_end;
max_IncSub.length=cur_IncSub.length;
}
break;
}
if(p2<L.elem+L.length&&*p2<=*(p2-1))
{
cur_IncSub.p_end=p2;
if(cur_IncSub.length>max_IncSub.length)
{
max_IncSub.p_start=cur_IncSub.p_start;
max_IncSub.p_end=cur_IncSub.p_end;
max_IncSub.length=cur_IncSub.length;
}
p1=p2;
++p2;
cur_IncSub.p_start=p1;
cur_IncSub.length=1;
}
}
L.elem=(ElemType*)malloc(sizeof(ElemType)*max_IncSub.length);
if(!L.elem) exit(-1);
L.listsize=max_IncSub.length;
L.length=0;
for(int i=0;max_IncSub.p_start<max_IncSub.p_end;++max_IncSub.p_start,++i)
{
*(L.elem+i)=*(max_IncSub.p_start);
++L.length;
}
}
本文介绍如何设计算法对线性表进行区间元素删除,保持元素顺序不变,以及查找线性表中最长的连续递增子序列。通过具体代码实现,展示了线性表操作和递增子序列查找的详细过程。
1097

被折叠的 条评论
为什么被折叠?



