6-1 顺序表创建和就地逆置 (10 分)
//库函数头文件包含
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
//顺序表的存储结构定义
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct{
ElemType* elem; //存储空间基地址
int length; //表中元素的个数
int listsize; //表容量大小
}SqList; //顺序表类型定义
Status ListCreate_Sq(SqList &L);
void ListReverse_Sq(SqList &L);
int main() {
SqList L;
ElemType *p;
if(ListCreate_Sq(L)!= OK) {
printf("ListCreate_Sq: 创建失败!!!\n");
return -1;
}
ListReverse_Sq(L);
if(L.length){
for(p=L.elem;p<L.elem+L.length-1;++p){
printf("%d ",*p);
}
printf("%d",*p);
}
return 0;
}
/* 请在这里填写答案 */
Status ListCreate_Sq(SqList &L)
{
int i;
cin>>i;
L.length=0;
L.listsize=LIST_INIT_SIZE;
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElenType));
if(!L.elem)
exit(OVERFLOW);
ElemType *p,*q;
p=L.elem;
for(int j=0;j<i;j++){
L.length++;
cin>>&L.elem[j];
}
return OK;
}
void ListReverse_Sq(SqList &L)
{
//逆置,再不引入辅助数组的前提下
int i,j;
for(i=0;i<L.length/2;i++){
t=L.elem[i];
L.elem[i]=L.elem[L.length-1-i];
L.elem[L.length-1-i]=t;
}
}
7-2 jmu-ds-顺序表区间元素删除 (15 分)
#include<stdio.h>
int main()
{
int a[100];
int ji,i;
scanf("%d",&ji);
for(i=0;i<ji;i++)
{
scanf("%d",&a[i]);
}
int i1,i2,i3=0;
scanf("%d %d",&i1,&i2);
for(i=0;i<ji;i++)
{
if(i3==0&&(a[i]<i1||a[i]>i2))
{
printf("%d",a[i]);i3=1;
}
else
if((a[i]<i1||a[i]>i2))
printf(" %d",a[i]);
}
return 0;
}
这是错误的(表面过了而已
正确解法
最后一张只是伪码
//7-2 jmu-ds-顺序表区间元素删除 (15 分)
#include<bits/stdc++.h>
using namespace std;
typedef struct
{
int *elem;
int length;
}LL;
int main()
{
int n;
cin>>n;
LL L;
L.elem=(int *)malloc(sizeof(int)*(n+1));
if(!L.elem)
exit(OVERFLOW);
L.length=n;
for(int i=0;i<n;i++){
cin>>L.elem[i];
}
int minn,maxx,count1=0;
cin>>minn>>maxx;
//算法思想!!!
for(int i=0;i<n;i++){
if(L.elem[i]>=minn&&L.elem[i]<=maxx){
count1++;
L.length--;
}
// cout<<i-count1<<endl;
//if(i-count1+1>=0)
else//如果是并列而不是选择(没有else)的话,
//实际上并没有把要剔除的数删掉,把他也往前平移了
//所以他是选择关系不是并列关系
L.elem[i-count1]=L.elem[i];
}
for(int i=0;i<L.length;i++){
cout<<L.elem[i];
if(i!=L.length-1)
cout<<' ';
}
return 0;
}
7-1 两个有序链表序列的合并 (20 分)
//7-2 jmu-ds-顺序表区间元素删除 (15 分)
#include<bits/stdc++.h>
using namespace std;
#define OK 1
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
Status CreatList(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
if(!L)exit(OVERFLOW);
L->next=NULL;
return OK;
}
Status ListInsert(LinkList &L)
{
LNode *tmp,*cur=L;
tmp=(LNode*)malloc(sizeof(LNode));
if(!tmp) exit(OVERFLOW);
int e;
while(scanf("%d",&e)&&e!=-1)
{
tmp=(LNode*)malloc(sizeof(LNode));
tmp->data=e;
tmp->next=NULL;
cur->next=tmp;
cur=cur->next;
}//cur就是操作的L那个练
cur->next=NULL;
return OK;
}
Status combine(LinkList L1,LinkList L2,LinkList &L)//一共用了三个链,第一个第二个都是输入的,最后一个才是要输出的
{
LNode *l1,*l2,*tmp=L,*cur=L;//这些临时变量就是为了保证L始终是链表的名字
l1=L1->next;
l2=L2->next;
int m1=0;
while(l1&&l2)
{
if(l1->data<l2->data){
tmp=(LNode*)malloc(sizeof(LNode));
if(!tmp) exit(OVERFLOW);
tmp->data=l1->data;
tmp->next=NULL;
cur->next=tmp;
cur=tmp;
l1=l1->next;
}
else{
tmp=(LNode*)malloc(sizeof(LNode));
if(!tmp) exit(OVERFLOW);
tmp->data=l2->data;
tmp->next=NULL;
cur->next=tmp;
cur=tmp;
l2=l2->next;
}
if(m1==0)
cout<<cur->data;
else
cout<<' '<<cur->data;
m1=2;
}
if(l1) cur->next=l1;
else if(l2) cur->next=l2;
cur=cur->next;
while(cur){
if(m1==0)
printf("%d",cur->data);
else
printf(" %d",cur->data);
m1=1;
cur=cur->next;
}
if(m1==0)
printf("NULL");
return OK;
}
int main()
{
LNode *L1,*L2,*L;
CreatList(L1);
CreatList(L2);
CreatList(L);
int q,q1;
ListInsert(L1);
ListInsert(L2);
combine(L1,L2,L);
return 0;
}
//7-2 jmu-ds-顺序表区间元素删除 (15 分)
#include<bits/stdc++.h>
using namespace std;
int main()
{
//使用容器竟是如此简单!!!
//掌握容器吧
set<int> S;
set<int>::iterator S1;
int q;
while(cin>>q&&q!=-1)
{
S.insert(q);
}
while(cin>>q&&q!=-1)
{
S.insert(q);
}
for(S1=S.begin();S1!=S.end();S1++)
{
cout<<*S1<<' ';
}
return 0;
}