数据结构之顺序表

来源:互联网 发布:英语选择题答案软件 编辑:程序博客网 时间:2024/05/22 11:11
// arrlist.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>#include <iostream.h>//定义线性表Listtemplate <class T> class List  {void clear();  //置空线性表bool isEmpty();//线性表为空时,返回truebool append(const T value);//在表尾添加一个元素value,表的长度增1bool insert(const int p, const T value);//在位置p上插入一个元素value,表的长度增1bool delete1(const int p); //删除位置p上的元素,表的长度减 1bool getPos(int & p, const T value);//查找值为value的元素并返回其位置bool getValue(const int p, T& value); //把位置p的元素值返回到变量value中bool setValue(const int p, const T value);//用value修改位置p的元素值}; //定义顺序表arrListtemplate <class T> class arrList : public List<T>  {//顺序表,向量private:    //线性表的取值类型和取值空间T    *aList ;        //私有变量,存储顺序表的实例int  maxSize;            //私有变量,顺序表实例的最大长度int  curLen; //私有变量,顺序表实例的当前长度int  position;//私有变量,当前处理位置public: //顺序表的运算集arrList(const int size) {  //创建一个新的顺序表,参数为表实例的最大长度maxSize = size;  aList = new T[maxSize]; curLen = position = 0;}~arrList() {//析构函数,用于消除该表实例delete [] aList;}void clear() { //将顺序表存储的内容清除,成为空表delete [] aList; curLen = position = 0;aList = new T[maxSize];} int  length();            //返回此顺序表的当前实际长度bool list(); //显示表bool append(const T value);//在表尾添加一个元素value,表的长度增1bool insert(const int p, const T value);//在位置p上插入一个元素value,表的长度增1bool delete1(const int p); //删除位置p上的元素,表的长度减 1bool setValue(const int p, const T value);//用value修改位置p的元素值bool getValue(int &p, const T& value);//把位置p的元素值返回到变量value中  bool getPos(int &p, const T value);//查找值为value的元素,并返回第1次出现的位置  }; template <class T>int arrList<T> :: length() //求表长{ return curLen;}template <class T>bool arrList<T> :: list() //显示表{ int i; if(curLen==0) return false; printf("表元素为:"); for(i=0;i<curLen;i++) printf("%c ",aList[i]); printf("\n"); return true;}template <class T>bool arrList<T>::append(T value)   //在表尾添加一个元素value,表的长度增1{if(curLen>=maxSize){cout<<"The list is illegal"<<endl;return false;}aList[curLen++]=value;return true;}template <class T>bool arrList<T> :: insert(const int p, const T value) //添加元素{ int i; if(curLen>=maxSize) { cout << "The list is overflow"<<endl; return false; } if(p<0 || p>curLen+1) { cout<< "Insertion point is illegal"<<endl; return false; } for(i=curLen; i>=p; i--) aList[i]=aList[i-1]; aList[p-1]=value; curLen++; return true;}template<class T>bool arrList<T>::delete1(const int p)//删除元素{int i;if(curLen<=0){cout<<"表为空"<<endl;return false;}if(p<0||p>curLen-1){cout<<"删除位置不合法\n"<<endl;}for(i=p-1;i<curLen;i++){ aList[i]=aList[i+1];}curLen--;return true;}template<class T>bool arrList<T>::setValue(const int p, const T value)// // 用value修改位置p的元素值{if(curLen>=maxSize){cout<<"The list is overflow!"<<endl;return false;}if(p<0||p>maxSize){cout<<"The position is illegal!"<<endl;return false;}aList[p]=value;return true}template<class T>bool arrList<T>::getValue(int &p, const T& value)//把位置p的元素值返回到变量value中  {if(curLen>=maxSize){cout<<"The list is overflow!"<<endl;return false;}if(p<0||p>maxSize){cout<<"The position is illegal!"<<endl;return false;}int i;for(i=0;i<curLen;i++){aList[p]==value;return true;}return false;}template<class T>bool arrList<T>::getPos(int &p,const T value){int i;for(i=0;i<curLen;i++){if(value==aList[i]){p=i+1;return true;}}return false;}void main(){arrList<char> AL(1000);//定义及构造顺序表对象int choice;int pos;char value;    bool ok;do{printf("顺序表上机程序,请选择(0退出,1清除表,2显示表,3求表长,4插入元素,5追加元素,6删除元素,7查找元素值,8查找元素位置):");scanf("%d",&choice);switch(choice){case 0:      printf("再见!\n");  break; case 1: AL.clear(); printf("清除表操作成功!\n"); break; case 2:  ok=AL.list(); if(ok==false) printf("空表!\n");  break; case 3:  pos=AL.length(); if(pos==0) printf("空表!\n"); else printf("表长为:%d\n",pos); break; case 4:  printf("请输入插入位置:"); scanf("%d",&pos); getchar(); printf("请输入元素值:"); value=getchar(); ok=AL.insert(pos,value); if(ok==false) printf("插入操作失败!\n"); else printf("插入操作成功!\n"); break; case 5: printf("请输入追加元素值:");scanf("%s",&value); getchar(); ok=AL.append(value); if(ok==false) printf("插入操作失败!\n"); else printf("插入操作成功!\n"); break; case 6: printf("请输入删除位置:");scanf("%d",&pos);getchar(); ok=AL.delete1(pos); if(ok==false) printf("删除操作失败!\n"); else printf("删除操作成功!\n"); break; case 7:  printf("请输入查找元素值:");scanf("%s",&value);getchar(); ok=AL.getPos(pos,value); if(ok==false) printf("查找元素位置操作失败!\n"); else printf("查找元素位置操作成功,元素位置为:%d\n",pos); break; case 8: printf("请输入查找元素位置:");scanf("%d",&pos);getchar(); ok=AL.getValue(pos,value); if(ok==false) printf("查找元素位置操作失败!\n"); else printf("查找元素位置操作成功,元素值位置为:%c\n",value); break; default: printf("选择错误!\n");}    }while(choice!=0);getchar();//暂停}

原创粉丝点击