线性表的顺序实现_使用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
-
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...
-
软考考前卖答案_软考考前心理调整八种方法详细阅读
随软考的临近,不知道是不是有考生会很紧张,出现以下症状:对考试产生害怕、忧虑、不安等情绪,对考试结果缺乏信心,精神处于高度紧张状态,有时还伴有头痛、失眠、食欲不振、脾气暴躁、焦躁不安、注意力不集中、头脑发木等反应,甚至对考试产生严重的恐惧感。这就是我们常说的考试焦虑。心理学研究表明,...
-
[程序员逻辑思维培养]职业程序员培养之道详细阅读
软件开发是以人为核心的过程,对人的依赖性远高于传统的硬件生产企业,为了保持开发能力的稳定性,一方面需要定义软件过程,以过程为枢纽将人、技术、工具衔接起来,另一方面也要加强人才的培养,使人的工作能力能够稳定、提高人员的自治性。随着社会需求的膨胀,对程序员的需求量、对熟练的程序员的需求量在剧...
-
【指针万用表的使用方法】C++指针使用方法解惑详细阅读
c++指针使用方法解惑 void clearlist(lnode * & hl) 仔细看一下这种声明方式,确实有点让人迷惑。 下面以 void func1( myclass *&pbuildingelement ); 为例来说明这个问题。在某种意义上,*和&是意思相对的两个东西,把它们放在...
-
【ctoc模式什么意思】CtoC++新手指南详细阅读
c++技术固然是很时髦的,许多c用户都想在尽可能短的时间内为自己贴上c++的标签。介绍c++的书很多,但只有那些已经侥幸入门的用户才偶尔去翻翻,仍有不少在c++门口徘徊的流浪汉。 本文只针对c用户,最好是一位很不错的老用户(譬如他在遇到最简单的问题时都尝试着使用指针),通过一些c和更好的c++(本文...
-
线性表的顺序实现_使用C++实现线性表的基本功能详细阅读
include include include typedef int elemtype; 线性表的基本操作void initiallist(elemtype *l); int isempty(elemtype *l);void l...
-
计算机软件水平考试与计算机四级|计算机软件水平考试:《数据结构》是核心详细阅读
从2004年起,计算机与软件考试纳入全国专业技术人员职业资格证书制度的统一规划,报考任何级别的考生都不受学历、资历限制,这使得该考试报考人数骤增。而20%通过率无疑使该考试的竞争非常激烈,怎样能顺利通过考试已成为广大参考人员的热门话题。 为了让考生更好的备考,我们特走访了多位已经通过考试的朋...
-
软考 软考_软考专家谈软考趋势和复习要点详细阅读
2005 年上半年全国计算机技术与软件专业技术资格(水平)考试(简称软考)报考人数最多的专业级别是软件设计师和程序员。这两个级别是软件考试中最经典、考题最稳定和考生最多的级别,同时也是社会认同度最高的两个级别。近年来,随着信息技术的发展,考试内容的重点已作较大调整,考试更能体现出技术的进...
-
[路由器与交换机的主要区别]深入了解路由器与交换机的区别3详细阅读
2 负载集中:交换机之间只能有一条通路,使得信息集中在一条通信链路上,不能进行动态分配,以平衡负载。而路由器的路由协议算法可以避免这一点,ospf路由协议算法不但能产生多条路由,而且能为不同的网络应用选择各自不同的最佳路由。 3 广播控制:交换机只能缩小冲突域,而不能缩小广播域。整个交换...