线性表的顺序实现_使用C++实现线性表的基本功能
【shitiku.jxxyjl.com--软件水平学习指南】
#include <iostream.h>
#include <conio.h>
#include <alloc.h>
typedef int elemtype;
//线性表的基本操作
void initiallist(elemtype *l);
int isempty(elemtype *l);
void listtraverse(elemtype *l);
int nextelem(elemtype *l);
int priorelem(elemtype *l);
int locateelem(elemtype *l,elemtype &e);
void getelem(elemtype *l);
void listinsert(elemtype *l);
void listdelete(elemtype *l);
void clearlist(elemtype *l);
const int n=10;
elemtype temp;//全局变量!
void initiallist(elemtype *l)
{
for(int i=0;i<n;i++)
*(l+i)="#";
}
int isempty(elemtype *l)
{
if(*l=="#")
return 1;
else
return 0;
}
void listtraverse(elemtype *l)
{
static int k;
if(*(l)==35)
cout<<"the list is null!\n";
else
{
cout<<"the records of the list are:\n";
for(int i=0;i<n+k;i++)
{
if((*(l+i)>32768))
break;
else
cout<<*(l+i)<<" ";
}
k++;
}
}
int nextelem(elemtype *l)
{
int index;
elemtype e;
cout<<"input the records for searching it"s next elem!\n";
cin>>e;
index=locateelem(l,e);
if(*(l+index+1)>32768)
cout<<"it has no next elem!\n";
else
cout<<e<<"的后继是:"<<*(l+index+1)<<endl;
}
int priorelem(elemtype *l)
{
int index;
elemtype e;
cout<<"input the records for searching it"s prior elem!\n";
cin>>e;
index=locateelem(l,e);
if(index>n)
cout<<"it has no next elem!\n";
else if(index==-1||index==0)
{
cout<<"it has no prior elem!\n";
return 0;
}
else
cout<<e<<"的前驱是:"<<*(l+index-1)<<endl;
}
int locateelem(elemtype *l,elemtype &e)
{
int i;
for(i=0;i<n+1&&(*(l+i)!="#");i++)
{
if(*(l+i)==e)
return i;
else
continue;
}
if(i<n||i>=n)
return -1;
return i;
}
void getelem(elemtype *l)
{
int index;
cout<<"input the value of i:\n";
cin>>index;
if(*(l+index-1)>32768)
cout<<"the records null!\n";
else
cout<<"the records which you want to search are: "<<*(l+index-1)<<endl;
}
void listinsert(elemtype *l)
{
elemtype e;
int index,i;
cout<<"please input only one inserted number and it"s location!\n";
cin>>e;
cin>>index;
if(index>n)
*(l+index-1)=e;
else
{
for(i=n;i>=index;i--)
*(l+i)=*(l+i-1);
*(l+index-1)=e;
}
listtraverse(l);
}
void listdelete(elemtype *l)
{
elemtype e;
int index,i;
cout<<"input the number which you want to deleted!\n";
cin>>e;
index=locateelem(l,e);
for(i=index;i<n+1&&(*(l+i)!="#");i++)
*(l+i-1)=*(l+i);
cout<<"deleted "<<e<<endl;
listtraverse(l);
}
void clearlist(elemtype *l)
{
for(int i=0;i<n+1;i++)
*(l+i)="#";
}
int main()
{
int choice,flag=0;
char ch;
elemtype *p,e;
p=(elemtype *)malloc((n+1)*sizeof(elemtype));
if(!p)
{
cout<<"no more memory can be obtained!\n";
goto loop;
}
loop: p=(elemtype *)realloc(p,(n+1)*sizeof(elemtype));
if(!p)
{
cout<<"overflow!\n";
exit(0);
}
initiallist(p);
cout<<"input "<<n<<" records!\n";//数据互不相同!
for(int i=0;i<n;i++)
cin>>*(p+i);
if(isempty(p))
cout<<"the list is null!\n";
cout<<" menu fuction \n"
<<" 1: 遍历线性表的元素!( 只允许调用一次!)\n"
<<" 2: 求某一元素的前驱和后继!\n"
<<" 3: 获取线性表l中的第i个数据元素内容!\n"
<<" 4: 在线性表中插入一个元素!\n"
<<" 5: 删除线性表中值为e的元素!\n"
<<" 6: 检索值为e的数据元素!\n"
<<" 7: 清空线性表!"<<endl;
do
{
cout<<"please input your choice!\n";
cin>>choice;
switch(choice)
{
case 1: listtraverse(p); break;
case 2: nextelem(p);
priorelem(p);
break;
case 3: getelem(p); break;
case 4: listinsert(p); break;
case 5: listdelete(p); break;
case 6:
cout<<"input the records !\n" ;
cin>>e;
cout<<"it"s location is "<<locateelem(p,e)+1;
break;
case 7: clearlist(p);
cout<<"now ";
listtraverse(p);
break;
}
cout<<"\ndid you want to continue the operation?(y/n)\n";
cin>>ch;
if(ch=="y"||ch=="y")
flag=1;
else
flag=0;
}while(flag);
free(p);
getch();
return 0;
}
本文来源:https://shitiku.jxxyjl.com/ruanjianshuipingxuexizhinan/49046.html
-
交换机连接交换机_交换机系列培训:交换机性价比基准测试详细阅读
美国《network world》与tolly group近期联合进行的switchmetric(第3轮)测试结果显示,基于铜线交换技术的进步及来自新厂商的竞争是千兆以太网交换价格下降的主要原因。这次测试于3月和4月在tolly group试验室进行,对一些具有大规模端口配置交换机的测试...
-
[希赛网络工程师讲义]网络工程师辅导班讲义详细阅读
内容简介:第3章 交换技术 主要内容:1、线路交换 2、分组交换 3、帧中继交换 4、信元交换 一、线路交换1、线路交换进行通信:是指在两个站之间有一个实际的物理连接,这种连接是结点之间线路的连接序列。2、线路通信三种状态:线路建立、数据传送...
-
面向对象程序设计常用类型思维导图_面向对象程序设计C++的const类型详细阅读
常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。因此,定义或说明常类型时必须进行初始化。 一般常量和对象常量 1 一般常量 一般常量是指简单类型的常量。这种常量在定义时,修饰符const可以用在类型说明符前,也可以用在类型说明符后。如:...
-
【软浮云软件】软件指导:软设过关经验谈详细阅读
一,有标准辅导教材,学习方便,不要到处去找相关的书看了,比如软件工程、数据库、数据结构啊什么的。有了标准教材复习有个范围,这样就不会盲目乱复习了,或者漏复习部分内容。可以以标准教材为中心,其它资料为参考,复习有主次,这样进步应该比较快。 二,有经验丰富的老师指导,遇到问题能及时解决...
-
计算机软件水平考试与计算机四级_计算机软件水平考试应避免的五大失误详细阅读
上机操作不当-成绩不算在考试前,请仔细听清监考老师所说的一些考试注意事项,不要急于操作。我们知道,有关计算机的考试,其成绩是直接记录在计算机上的,考试过程中,如果因为你的操作不当,而将你的考试成绩或准考证号等丢失,你的辛苦将付之东流,这恐怕是你最不愿看到的失误吧。考题跳做-不给分参加考试如...
-
【使用chrome浏览器】使用C#在应用程序之间发送消息详细阅读
首先建立两个c 应用程序项目。 第一个项目包含一个windows form(form1),在form1上有一个button和一个textbox。 第二个项目包含一个windows form(form1),在form1上有两个button,分别用来测试第一个应用程序中butt...
-
[在uml的需求分析建模中 对用例模型]UML用例建模的慨念和应用详细阅读
一. uml简介 uml(统一建模语言,unified modeling language)是一种定义良好、易于表达、功能强大且普遍适用的可视化建模语言。它融入了软件工程领域的新思想、新方法和新技术。它的作用域不限于支持面向对象的分析与设计,还支持从需求分析开始的软件开发的全过程。在...
-
【ping命令的工作原理】ping命令工作原理详细解析详细阅读
内容简介: ping的原理就是首先建立通道,然后发送包,对方接受后返回信息,这个包至少包括以下内容,发送的时候,包的内容包括对方的ip地址和自己的地址,还有序列数,回送的时候包括双方地址,还有时间等,主要是接受方在都是在操作系统内核里做好的,时刻在监听,提供一段c程序的代码,希望对大家有用。...
-
445端口常见漏洞|常见端口的作用、漏洞和操作建议(2)详细阅读
21端口:21端口主要用于ftp(file transfer protocol,文件传输协议)服务。 端口说明:21端口主要用于ftp(file transfer protocol,文件传输协议)服务,ftp服务主要是为了在两台计算机之间实现文件的上传与下载,一台计算机作为ftp客户...
-
如何使用打印机_如何使用ADO.NET轻松操纵数据库详细阅读
ado net提供了connection来连接数据库,同时也提供了command对象来查询数据库。同connection对象一样,command也有两种:oledbcommand和sqlcommand 其区别同connection对象。 要操纵数据库,必须先使用connect...