首页 > 其他分享 >数据结构--顺序线性表

数据结构--顺序线性表

时间:2023-02-17 15:04:25浏览次数:37  
标签:线性表 -- elem int length exit sqList ERROR 数据结构


#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <cstring>
using namespace std;
#define OK 1
#define ERROR -1
#define LIST_INIT_SIZE 100
#define LISTSIZE 10
#define true 1
#define flase 0
typedef int ElemType ;
typedef int Status ;
typedef struct list_{
ElemType * elem ;
int length ; //当前长度
int listsize ; //线性表的容量


}sqList;


Status InitList(sqList &L)
{
/* creat a empty sqlist */
/*创建了一个空线性表*/
L.elem = (ElemType *)malloc(sizeof(ElemType)*LIST_INIT_SIZE);
if(L.elem!=NULL)
cout<<"creat a empty sqlist "<<endl;
L.length = 0;
L.listsize =LIST_INIT_SIZE ;
return OK;

}
Status DestroyList(sqList &L)
{
/*销毁一个线性表 L*/
/*初始条件 : 线性表存在*/
if(L.elem ==NULL)
exit(ERROR);
free(L.elem) ;
L.elem =NULL ;
L.length = 0 ;
L.listsize = 0;
return OK ;

}
Status clearList(sqList &L)
{


if(L.elem==NULL)
exit(ERROR);
memset(&L,0,sizeof(sqList));
return OK;
}
Status ListLength(sqList &L)
{
if(L.elem==NULL)
exit(ERROR);
return L.length ;
}
void List_show(sqList &L)
{
if(L.elem==NULL)
exit(ERROR);
int i ;
for(i =0 ;i<L.length ;i++)
{
cout<<" "<<L.elem[i]<<" ";
}


}
Status write_List(sqList &L,int e)
{
int i ;
if(L.elem==NULL)
exit(ERROR) ;
if(L.listsize<e)
{
cout<<"容量不够"<<endl;
exit(ERROR);
}
for(i=0 ;i<e ;i++)
{
cin>>L.elem[i];
L.length++;

}
return OK ;

}
Status Getelem(sqList &L,int e)
{
if(L.elem==NULL)
exit(ERROR);
if(e<=0 || e>L.length)
{
cout<<"failed"<<endl;
exit(ERROR);

}


return L.elem[e-1];

}
Status ListEmpty(sqList &L)
{
if(L.elem==NULL)
exit(ERROR);
if(L.length ==0)
return false ;
}
Status ListInsert(sqList &L ,int i ,int e)
{
if(L.elem == NULL)
exit(ERROR) ;
if(i<=0 ||i>L.length)
exit(ERROR);

int *p = &L.elem[L.length-1];
int *q = &L.elem[i-1];
while(p>=q)
{
*(p+1) = *(p) ;
p--;
}
*q = e ;
L.length++;
return OK ;

}

void unionList(sqList &M ,sqList &N)
{
/*将所有的在线性表 M 中但不在 N 中的数据元素插入到M 中*/
int lenM = M.length ;
int lenN = N.length ;
int i ;
// M.length+=N.length ;
for(i = 0 ;i<lenM ;i++)
{
if(Getelem(M,i+1) )

}





}
int main()
{
int e ;
int m ;
int i ;
sqList a ;
InitList(a);
cout<<"输入个数:\t"<<endl;
scanf("%d",&e);
write_List(a,e);

cout<<"获得第几个:"<<endl;
cin>>m ;
cout<< Getelem(a,m)<<endl;
cout<<"在第几个位置插入几 :"<<endl;
cin>> i >> e;
ListInsert(a ,i ,e) ;

List_show(a);





return 0;
}

 

 

 

标签:线性表,--,elem,int,length,exit,sqList,ERROR,数据结构
From: https://blog.51cto.com/u_15970235/6064107

相关文章

  • 数据结构--基本概念及术语
    1. 数据:是对客观事物的符号表示,在我们计算机科学中是指所有能输入到计算机中,并能够被计算机程序处理的符号总称。他是计算机程序加工的“原料”。比如说,一个利用数值分......
  • 数据结构--线性表
    线性表最简单也是最常用的一种数据结构,它的特点是,在数据元素的非空有限集中,(1)存在唯一一个被称为“第一个”的数据元素,存在唯一一个被称为“最后一个”的数据元素。(2)除了第......
  • 《DFZU2EG_4EV MPSoC之嵌入式Vitis开发指南》第十三章 QSPI Flash读写测试实验​
    QSPIFlash读写测试实验​PS的输入/输出外设(IOP)有两个具有不同功能特性和IO接口性能的QSPI控制器。它们共享相同的APB从接口和MIO引脚。一次只能使用控制器中的一个。QSPI......
  • 负载均衡意思
    什么事负载均衡?将用户请求或者说流量通过负载均衡器,按照某种负载均衡算法把流量均匀地分散到后端的多个服务器上,接收到请求的服务器可以独立的响应请求,以期望的规则分摊到多......
  • 我的两群吃粽小伙子
    3270:我的两群吃粽小伙伴TimeLimit:1Sec  MemoryLimit:128MBSubmit:683  Solved:26[​​Submit​​][​​Status​​][​​WebBoard​​]Description......
  • C++继承--公有继承
    C++继承--公有继承#include<iostream>#include<cstdio>usingnamespacestd;classStudent{//基类public:voidget_value();voiddisplay();private:intnu......
  • 校园运动会报名系统
     大一课程设计: (运行环境DEVc++) 链接:https://pan.baidu.com/s/15ZBh826b2W3CJl5-bnzQAg 提取码:znka 解压到工程下,就可用,运行main.cpp(ps:需要修改一下里面......
  • STL 概述
    STL提供三种类型的组件:容器,迭代和算法,他们都支持泛型程序设计标准.容器有两类:顺序容器和关联容器.顺序容器(vector,list,deque,stringetc..)它是一......
  • 传递任意数量的实参
    一丶有时候,你预先不知道函数需要接受几个实参,好在python允许从调用语句中收集任意数量的实参,例如,来看一个制作披萨的函数,他需要接受很多配料,但你无法预先确......
  • c++继承---私有继承
    私有继承   在声明一个派生类的时候将基类的继承方式指定为private的,称为私有继承,用私有继承方式建立的派生类称为私有派生类,其基类称为私有基类.私有继承中的......