顺序表的基本操作(C++)

// Sequence.cpp : 定义控制台应用程序的入口点。 //

#include "stdafx.h" #include "iostream" using namespace std; //错误1,使用cout要加上此句 typedef int type; //忘记知识点:定义type好处就是便于以后总体更改数据类型

class SeqList{  type data[100];  int length;  int MaxSize;

public:  SeqList(){ length=0; };  void SLCreate(int MaxSize);     int Find(type x);  void Insert(type x,int i);  void Remove(int i);  void display(SeqList);  }; //错误2:每个class定义完后要加分号

//创建顺序表 void SeqList::SLCreate(int sz ){  type x;     cout<<"please input the SeqList:";  for(int i=0;i<sz;i++){   cin>>x;        data[i]=x;     length++;  }  } //找到值x在表中位置 int SeqList::Find(type x){

 for(int i=0;i<length;i++){       if(data[i]==x){     return i;    }        }        cout<<"no"<<x<<"in this seqlist\n";      } //在p位置插入值x void SeqList::Insert(type x,int p){

 if(p<0||p>length){  cout<<"the p is illegal";  }  else{  for(int i=length-1;i>p;i--)     data[i+1]=data[i];      data[p]=x;      length++;    }   } //显示顺序表中的所有元素 void SeqList::display(SeqList list){

 for(int i=0;i<list.length;i++){   cout<<list.data[i]<<endl;  }    } //删除表中位置p上元素 void SeqList::Remove(int p){      if(p<0||p>=length){  cout<<"the p is illegal";   }else{    for(int i=p;i<length;i++){   data[i]=data[i+1];     }    length--;   }     }

void main() {    SeqList FirstList;  int length,pdelete,pinsert;  type dataInsert,dataFind;  cout<<"please input the length of the seqlist:\n";  cin>>length;  FirstList.SLCreate(length);  FirstList.display(FirstList);

 cout<<"please input the data you want to find:";  cin>>dataFind;  int postion=FirstList.Find(dataFind);   cout<<"the data's postion is:\n"<<postion<<"\n";    

 cout<<"please input the postion that you want to delete";  cin>>pdelete;  FirstList.Remove(pdelete);  cout<<"the seqlist after delete is:\n";  FirstList.display(FirstList);

 cout<<"please input the postion that you want to insert";  cin>>dataInsert>>pinsert;  FirstList.Insert(dataInsert,pinsert);  cout<<"the seqlist after insert is:\n";  FirstList.display(FirstList);

 

}

 


运行结果如下(注意运行时是ctrl+F5):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值