#include<iostream.h>
#include<stdlib.h>
#include<iostream.h>
typedef struct seqlist //å®ä¹çº¿æ§è¡¨ç顺åºåå¨ç»æ
{
int *elem; //åå¨ç©ºé´åé
çé¦å°å
int length ; //线æ§é¿åº¦
}seqlist;
//æ¤åéå¯ä½ä¸ºçº¿æ§è¡¨æ°æ®ç±»åå®ä¹çº¿æ§è¡¨
void init(seqlist &l) //线æ§è¡¨çåå§å
{
l.elem=new int[20]; //åé
åå¨ç©ºé´åå¨çº¿æ§è¡¨ä¸çå
ç´
l.length=0; //空线æ§è¡¨çé¿åº¦ä¸º0
}
void delseqlist(seqlist &l) //éæ¯å»ºç«ç线æ§è¡¨
{
delete []l.elem;
l.length=0;
}
void insert(seqlist &l,int i, int x)//å
ç´ eæå
¥çº¿æ§è¡¨ä¸ç第i个ä½ç½®ä¸
{
if(i<1||i>l.length+1)
cout<<"iå¼ä¸åæ³ï¼"<<endl; //é误å¤ç
for(int j=l.length;j>=i;j--)
l.elem[j+1]=l.elem[j]; //第i个å
ç´ åå
¶åçå
ç´ ä¾æ¬¡å移
l.elem[i]=x; //æå
¥æ°å
ç´ äºç¬¬i个ä½ç½®ä¸
l.length++; //线æ§è¡¨çé¿åº¦å¢1
}
void printout(seqlist &l) //è¾åºçº¿æ§è¡¨å½ä¸çææå
ç´
{
int j;
for(j=1;j<=l.length;j++)
cout<<l.elem[j]<<" ";
}
int llength(seqlist &l) //æ±çº¿æ§è¡¨çé¿åº¦
{
return l.length;
}
int locateElem(seqlist &l, int e) //å¨é¡ºåºè¡¨Lä¸æ¥æ¾ç¬¬ä¸ä¸ªå¼äºeç¸ççæ°æ®å
ç´
{
int i=1;
int *p;
p=l.elem; p++; //pæå线æ§è¡¨ä¸ç第ä¸ä¸ªå
ç´
while (i<=l.length && *p++!=e ) //ä¾æ¬¡ååè¿è¡æ¯è¾
++i;
if(i<=l.length)
return i;
else
return 0; //è¿åç»æ
}
void Delete( seqlist &l,int i,int &e)//æ¤å½æ°å é¤ç¬¬i个å
ç´ ï¼å¹¶å°å
¶å¼ç¨eè¿å
{
int *p,*q;
if((i<1)||(i>l.length)) cout<<"iå¼ä¸åæ³ï¼"<<endl;
p=&(l.elem[i]); //å°è¢«å å
ç´ éç»e
e=*p;
q=l.elem+l.length; //表尾å
ç´ çä½ç½®
for(++p; p<=q; ++p)
*(p-1)=*p;//被å å
ç´ ä¹åçå
ç´ å移
l.length--;
}
void invert( seqlist &l,int s,int t) //å°±å°éç½®
{
int k;
int w;
for(k=s;k<=(s+t)/2;k++)
{
w=l.elem[k];
l.elem[k]=l.elem[s+t-k];
l.elem[s+t-k]=w;
}
}
void main() //ææå®æ´çæºç¨åºå¹¶å¯¹ç®æ³è¿è¡æµè¯
{
seqlist l;
int x,a,y,b,j;
init(l); //åå§å空线æ§è¡¨l
cout<<"è¾å
¥çº¿æ§è¡¨çé¿åº¦:"<<endl;
cin>>a;
cout<<"è¾å
¥çº¿æ§è¡¨ä¸çææå
ç´ :"<<endl;
for(j=1;j<=a;j++)//éè¿æ·»å å
ç´ ä½¿çº¿æ§è¡¨lå为å«æè¥å¹²ä¸ªå
ç´ ç线æ§è¡¨
{
cin>> x;
insert(l,j,x);
}
cout<<"线æ§è¡¨ä¸º: ";
printout(l);
cout<<endl<<"被å é¤çå
ç´ çä½ç½®:";
cin>>y;
Delete(l,y,b);
cout<<endl<<"åå(å é¤å
ç´ )åç线æ§è¡¨ä¸º:";
printout(l);
invert(l,1,l.length );
cout<<endl<<"éç½®å表为: ";
printout(l);
cout<<"è¾å
¥è¦æ¥æ¾çå
ç´ ï¼"<<endl;
cin>>x;
cout<<endl;
cout<<x<<" å¨çº¿æ§è¡¨ä¸çä½ç½®ä¸º:(å
¶ä¸0表示xä¸å¨çº¿æ§è¡¨ä¸ï¼å
¶ä»å¼è¡¨ç¤ºå
¶å¨çº¿æ§è¡¨ä¸çä½ç½®)";
cout<<endl<<locateElem(l,x)<<endl;
}
æ以ååç
温馨提示:内容为网友见解,仅供参考