顺序表基本运算(线性表)
时间: 1ms 内存:128M
描述:
编写一个程序,实现顺序表的各种基本运算(假设顺序表的元素类型为char),主函数已给出,请补充每一种方法。
1、初始化顺序表L;
2、采用尾插法插入一个元素;
3、输出顺序表L;
4、输出顺序表L的长度;
5、判断顺序表是否为空;
6、输出顺序表L的第三个元素;
7、输出元素a的位置;
8、在第四个元素位置插入元素f;
9、输出顺序表L;
10、删除L的第三个元素;
11、输出顺序表L;
12、释放顺序表L;
数据元素类型定义为
typedef char ElemType;
顺序表的定义为
typedef struct{ElemType data[SizeMax];int length;} SqList;主函数:int main(){SqList *L;InitList(L); //初始化顺序表ElemType a,b,c,d,e;scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);Insert(L,a);Insert(L,b);Insert(L,c);Insert(L,d);Insert(L,e); //使用尾插法插入元素a,b,c,d,ePrint(L); //输出顺序表PrintLength(L); //输出顺序表长度if(SqNull(L))printf("顺序表不为空\n");else printf("顺序表为空\n"); //判断顺序表是否为空PrintData(L,3); //输出第三个元素printf("元素a的位置:%d\n",Find(L,a)); //输出元素a的位置ElemType f;scanf("%c",&f);Insertinto(L,4,f); //将f插入到第四个位置Print(L); //输出顺序表Delete(L,3); //删除第三个元素Print(L); //输出顺序表free(L); //释放内存return 0;}
输入:
第一行输入五个元素a,b,c,d,e;接下来输入元素f;请根据题目编写算法。
输出:
示例输入:
1 2 3 4 5
6
示例输出:
1 2 3 4 5
5
顺序表不为空
3
元素a的位置:1
1 2 3 6 4 5
1 2 6 4 5
提示:
参考答案(内存最优[0]):
#include <iostream>
#include<stdio.h>
#include<malloc.h>
#define SizeMax 50
using namespace std;
typedef char ElemType;
typedef struct
{
ElemType data[SizeMax];
int length;
} SqList;
void InitList(SqList *&L)
{
L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
bool Insert(SqList *&L,char x)
{
L->data[L->length]=x;
L->length++;
return true;
}
bool Print(SqList *&L)
{
int i=0;
for(i=0; i<L->length; i++)
printf("%c ",L->data[i]);
printf("\n");
return true;
}
bool PrintLength(SqList *&L)
{
printf("%d\n",L->length);
return true;
}
bool SqNull(SqList *&L)
{
if(L->length==0)
return 0;
else return 1;
}
bool PrintData(SqList *&L,int i)
{
printf("%c\n",L->data[i-1]);
return true;
}
bool Find(SqList *&L,char x)
{
int i,j=0;
for(i=0; i<L->length; i++)
{
if(L->data[i]==x)
j=i+1;
}
return j;
}
bool Insertinto(SqList *&L,int i,ElemType f)
{
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]=f;
L->length++;
return true;
}
bool Delete(SqList *&L,int i)
{
int j;
if(i<1||i>L->length+1)
return false;
i--;
for(j=i; j<L->length-1; j++)
L->data[j]=L->data[j+1];
L->length--;
return true;
}
int main()
{
SqList *L;
InitList(L); //初始化顺序表
ElemType a,b,c,d,e;
scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);
Insert(L,a);
Insert(L,b);
Insert(L,c);
Insert(L,d);
Insert(L,e); //使用尾插法插入元素a,b,c,d,e
Print(L); //输出顺序表
PrintLength(L); //输出顺序表长度
if(SqNull(L))
printf("顺序表不为空\n");
else printf("顺序表为空\n"); //判断顺序表是否为空
PrintData(L,3); //输出第三个元素
printf("元素a的位置:%d\n",Find(L,a)); //输出元素a的位置
ElemType f;
scanf("%c",&f);
Insertinto(L,4,f); //将f插入到第四个位
Print(L); //输出顺序表
Delete(L,3); //删除第三个元素
Print(L); //输出顺序
free(L); //释放内存
return 0;
}
参考答案(时间最优[0]):
#include <stdio.h>
#include <stdlib.h>
#define SizeMax 10
typedef char ElemType;
typedef struct
{
ElemType data[SizeMax];
int length;
} SqList;void InitList(SqList *&L)
{
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
}
void Insert(SqList *&L,ElemType n)
{
L->data[L->length]=n;
L->length++;
}
void Print(SqList *L)
{
for(int i=0; i<L->length; i++)
printf(i!=L->length-1?"%c ":"%c\n",L->data[i]);
}
void PrintLength(SqList *L)
{
printf("%d\n",L->length);
}
bool SqNull(SqList *L)
{
if(L->length)return 1;
return 0;
}
void PrintData(SqList *L,int n)
{
if(L->length<n)return;
printf("%c\n",L->data[n-1]);
}
int Find(SqList *L,ElemType a)
{
for(int i=0;i<L->length;i++)
if(L->data[i]==a)return i+1;
return 0;
}
void Insertinto(SqList *&L,int n,ElemType f)
{
for(int i=L->length;i>=n;i--)
L->data[i]=L->data[i-1];
L->data[n-1]=f;
L->length++;
}
void Delete(SqList *&L,int n)
{
for(int i=n-1;i<L->length-1;i++)
L->data[i]=L->data[i+1];
L->length--;
}
int main()
{
SqList *L;
InitList(L); //初始化顺序表
ElemType a,b,c,d,e;
scanf("%c %c %c %c %c%*c",&a,&b,&c,&d,&e);
Insert(L,a);
Insert(L,b);
Insert(L,c);
Insert(L,d);
Insert(L,e); //使用尾插法插入元素a,b,c,d,e
Print(L); //输出顺序表
PrintLength(L); //输出顺序表长度
if(SqNull(L))
printf("顺序表不为空\n");
else printf("顺序表为空\n"); //判断顺序表是否为空
PrintData(L,3); //输出第三个元素
printf("元素a的位置:%d\n",Find(L,a)); //输出元素a的位置
ElemType f;
scanf("%c",&f);
Insertinto(L,4,f); //将f插入到第四个位置
Print(L); //输出顺序表
Delete(L,3); //删除第三个元素
Print(L); //输出顺序表
free(L); //释放内存
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。