线性表的基本操作(顺序表)

实验内容:

建立顺序表,实现求表的长度、遍历表、查找、插入和删除元素、求前驱、求后继等操作;

实验基本要求:

进一步熟悉Turbo C或者VC++环境;

掌握线性表结构的基本操作

 

#include<iostream>
using namespace std;
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct
{
 ElemType data[MaxSize];
    int length;
} SqList;
void InitList(SqList *&L) //初始化线性表
{
 L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间
 cout<<"请输入字符个数:";
 int x;
 cin>>x;
    ElemType c[MaxSize];
 for(int i=0;i<x;i++)
  cin>>c[i];
 for(int j=0;j<x;j++)
 {
  L->data[j]=c[j];
 }
 L->length=x;      //置空线性表长度为0
}
void DestroyList(SqList *L)  //清空线性表
{
 L->length=0;
}
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("%c ",L->data[i]);
 printf("\n");
}
ElemType GetElem(SqList *L,int i,ElemType &e) //求线性表中某个数据元素值
{
 e=L->data[i-1];    //取元素值
 return e;   //成功找到元素时返回true
}
int LocateElem(SqList *L, ElemType e) //按元素值查找
{
 int i=0;
 while (i<L->length && L->data[i]!=e)
  i++;     //查找元素e
 if (i>=L->length)   //未找到时返回0
  return 0;
 else
  return i+1;    //找到后返回其逻辑序号
}

bool ListFull(SqList *L)             //判断线性表是否为满
{
 if(L->length==MaxSize)
  return true;
 else
  return false;
}

bool ListInsert(SqList *&L,int i,ElemType e) //插入数据元素
{
 int j;
 if (i<1 || i>L->length+1)
  return false;   //参数错误时返回false
 i--;      //将顺序表逻辑序号转化为物理序号
 for (j=L->length;j>i;j--) //将data[i]及后面元素后移一个位置
  L->data[j]=L->data[j-1];
 L->data[i]=e;    //插入元素e
 L->length++;    //顺序表长度增1
 return true;    //成功插入返回true
}
bool ListDelete(SqList *&L,int i,ElemType &e) //删除数据元素
{
 int j;
 if (i<1 || i>L->length)  //参数错误时返回false
  return false;
 i--;      //将顺序表逻辑序号转化为物理序号
 e=L->data[i];
 for (j=i;j<L->length-1;j++) //将data[i]之后的元素前移一个位置
  L->data[j]=L->data[j+1];
 L->length--;    //顺序表长度减1
 return true;    //成功删除返回true
}

ElemType SqListPrior(SqList *L,ElemType e) /*求顺序表中元素的前驱*/
{
 int i=0;
 while(i<L->length&&L->data[i]!=e)
  i++;
 if(i==0)
 {
  printf("第一个元素没有前驱\n");
  return 0;
 }
 else if
  (i<=L->length-1)
  return(L->data[i-1]);
 else
 {
  printf("不存在值为%d的元素\n",e);
  return 0;
 }
}


ElemType SqListNext(SqList *L,ElemType e) /*求顺序表中元素的后继*/
{
 int i=0;
 while(i<L->length&&L->data[i]!=e)
  i++;
 if(i==L->length-1)
 {
  printf("最后一个元素没有后继\n");
  return 0;
 }
 else if(i<L->length-1)
  return(L->data[i+1]);
 else
 {
  cout<<"不存在值为"<<e<<"的元素\n";
  return 0;
 }
}
void s()
{
    printf("1.初始化\n");
 printf("2.清空\n");
 printf("3.求链表长度\n");
 printf("4.检查链表是否为空\n");
 printf("5.检查链表是否为满\n");
 printf("6.遍历链表\n");
 printf("7.从链表中查找元素\n");
 printf("8.从链表中查找与给定元素值相同的元素在顺序表中的位置\n");
 printf("9.向链表中插入元素\n");
 printf("10. 从链表中删除元素\n");
 printf("其他键退出。。。。。\n");
}
int scan()
{
 int d;
    cout<<"请选择所要进行的操作:\n";
 scanf("%d",&d);
 return(d);
}
int main()
{
 SqList *h;
 int quit=0;
 printf("---------第一次操作需选择初始化---------\n");
 s();
 while(!quit)
  switch(scan())
 {
case 1:InitList(h);break;
case 2:DestroyList(h);break;
case 3: cout<<"线性表长度为:"<<ListLength(h)<<endl;break;
case 4:if(ListEmpty(h))cout<<"顺序表为空\n";else printf("顺序表不为空\n");break;
case 5:if(ListFull(h))cout<<"顺序表满\n";else cout<<"顺序表不满\n";break;
case 6:DispList(h); break;
case 7:cout<<"请输入要查找的元素的位置\n";
 int i;
 ElemType e;
 cin>>i;
 if(h->length==0) cout<<"顺序表已空\n";
 else
  if(i<=0||i>h->length) cout<<"查找的位置不正确\n";
 else
  cout<<"顺序表中第"<<i<<"个元素的值为:"<<GetElem(h,i,e)<<endl;
 break;
case 8:cout<<"请输入要查找的元素的值\n";
 ElemType a,location;
 cin>>a;
 if(h->length==0) printf("顺序表已空\n");
 else
 {
  location=LocateElem(h,a);
  if(location==0)
   printf("顺序表中不存在值为%d的元素\n",a);
  else
   cout<<a<<"在顺序表中的位置是:"<<LocateElem(h,a)<<endl;
 }
 break;
case 9:printf("请输入要插入的元素的位置和其值:\n");
 int i0;
 ElemType e0;
 cin>>i0>>e0;
 ListInsert(h,i0,e0);
 break;
case 10:printf("请输入要删除的元素的位置:\n");
 int i1;
 cin>>i1;
 ElemType e1;
 ListDelete(h,i1,e1);
 break;
case 11:cout<<"请输入求前驱的元素:";
 ElemType e2,prior;
 cin>>e2;
 prior=SqListPrior(h,e2);
 if(prior)
  cout<<e2<<" 的前驱是 "<<prior<<endl; break;
case 12:cout<<"请输入求后继的元素:";
 ElemType e3,next;
 cin>>e3;
 next=SqListNext(h,e3);
 if(next)
  cout<<e3<<" 的后继是 "<<next<<endl;
 break;
default:quit=1;
 }
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值