List-C++数组

来源:互联网 发布:matlab约束粒子群算法 编辑:程序博客网 时间:2024/04/30 12:31

 

/*最常用的顺序表算法实现,实现方法:用C++的类实现 编译测试工具VC++6.0
int用数组实现顺序的线性表的共三个文件public.h,SqList.h,SqList.cpp
public.h存放一些符号约定常量
SqList.cpp 类的实现和类功能代码测试,测试类函数功能还是比较完整,
基本上能遍历每个函数功能

write by chinanetboy 09/07/2007
*/


//-------------------------------------------------------------------------------------
//filename public.h

#define TRUE   1
#define FALSE   0
#define OK   1
#define ERROR   0
#define INFEASIBLE   -1
#define OVERFLOW -2
#define NULL       0
typedef  int   status;

 

//-------------------------------------------------------------------------------------

//filename SqList.h

 

#include "public.h"
typedef int ElemType;

class SqList
{
private:
 int Length;
 int ListSize;
public:
 ElemType * elem;
 unsigned long GetSize();
   SqList()
   {
    elem=NULL;
    Length=0;
    ListSize=0;
   };
   ~SqList();

   status InitList();
   void   DestroyList(); void ClearList();
   status ListEmpty();   int ListLength();
   status GetElem(int i,ElemType &e);
   int LocateElem(ElemType e);
   status PriorElem(ElemType cur_e,ElemType &pre_e);
   status NextElem(ElemType cur_e,ElemType &next_e);
   status ListInsert(int i,ElemType e);
   status ListDelete(int i,ElemType &e);
   void   ListTraverse();
};

 

 

//-------------------------------------------------------------------------------------
//filename SqList.cpp

 

#include "SqList.h"
#include "malloc.h"
#include "stdio.h"
#include  "stdlib.h"
#include  "conio.h"
#include "iostream.h"

#define LIST_INIT_SIZE   30
#define INCREATMENT 5

status SqList::InitList ()
{
 if(elem!=NULL) return ERROR;
 elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
 if(!elem) return(OVERFLOW);
 ListSize=LIST_INIT_SIZE;
 Length=0;
 return OK;
}
status SqList::ListInsert (int i,ElemType e)
{//在顺序线性表第i个位置插入新的元素e,i的合法值为1<=i<=Length+1
 if((i<1)||(i>Length+1)||(elem==NULL)) return ERROR;
 if(Length>=ListSize)
 {
  //当前存储空间已满,增加空间
  ElemType * newbase=(ElemType *)realloc(elem,(ListSize+INCREATMENT)*sizeof(ElemType));
  if(!newbase) return( OVERFLOW);
  elem=newbase;
  ListSize+= INCREATMENT;
 }
    ElemType *q=&(elem[i-1]);
 for(ElemType *p=&(elem[Length-1]);p>=q;--p)
  *(p+1)=*p;
 *q=e;
 ++Length;
 return OK;
}
status SqList::ListDelete (int i,ElemType &e)
{
 //在顺序线性表中删除第i个元素,并用e返回其值,i的合法值为1<=i<=Length
 if((i<1)||(i>Length)||(elem==NULL))  return ERROR;
 ElemType * p=&(elem[i-1]);
 e=*p;
 ElemType *q=elem+Length-1;
 for(++p;p<=q;++p)  *(p-1)=*p;
 --Length;
 return OK;
}
void SqList::DestroyList ()
{
 if(elem!=NULL)
  free(elem);
 elem=NULL;
}
void SqList::ClearList ()
{
 Length=0;
}
status SqList::ListEmpty ()
{
 if(Length==0)
  return TRUE;
 else
  return FALSE;
}
int SqList::ListLength ()
{
 return Length;
}
//SqList::GetElem(int i,ElemType e);
status SqList::GetElem(int i,ElemType &e)
{
 //用e返回第i个元素,1<=i<=Length
 if((elem==NULL)||(i<1)||(i>Length)) return ERROR;
 
    e=elem[i-1];
 return OK;
}

int SqList::LocateElem(ElemType e)
{
 //返回元素e的位序,若线性表未初始化则返回-1
 if(elem==NULL) return -1;
 for(int i=1;i<Length;i++)
 {
  if(elem[i-1]==e)
   return i;
 }
 return 0;
}

status SqList::PriorElem (ElemType cur_e,ElemType &pre_e)
{
 //若线性表未初始化返回FALSE
 if(elem==0) return FALSE;
 if(elem[0]==cur_e) return FALSE;//如果cur_e为第一个元素返回FALSE
 for(int i=1;i<Length;i++)
 {
  if(elem[i]==cur_e)
  {
   pre_e=elem[i-1];
   return OK;
  }
 }
 return FALSE;
}

status SqList::NextElem(ElemType cur_e,ElemType &next_e)
{
 if(elem==NULL) return ERROR;
 if(elem[Length-1]==cur_e) return FALSE;
 for(int i=0;i<Length;i++)
  if(elem[i]==cur_e)
  {
   next_e=elem[i+1];
   return OK;
  }
 return FALSE;
}

void SqList::ListTraverse()
{
 for(int i=0;i<Length;i++)
  cout<<"  "<<elem[i];
 cout<<endl;
}


SqList::~SqList()
   {
    if(elem!=NULL)
     free(elem);
   };

void main()
{
 char select[2];SqList MyList;
 int temp,selected;
 ElemType e,e2;
 //ClearScreen();
 while(1)
 {
  system("cls");//执行清屏命令
  cout<<"   ====================int顺序存储结构的线性表测试============/n";
  cout<<"/t1、初始化表/n"; 
  cout<<"/t2、销毁表/n";
  cout<<"/t3  插入数据/n";
  cout<<"/t4  删除数据/n";   
  cout<<"/t5、计算表长/n";
  cout<<"/t6、位置返回元素/n";
  cout<<"/t7、元素返回位置/n";
  cout<<"/t8、取元素前驱/n";
  cout<<"/t9、取元素后继/n";  
   cout<<"/t10、清空表/n";
  cout<<"/t11、为空检查?/n";    
  cout<<"/t12 遍历表./n";
  cout<<"/t0  退出"<<endl;
  cout<<"   ===========================================================/n";

  cout<<"/t回车返回主菜单,请选择(0~10):";
  cin>>select;
  selected=atoi(select);
  switch(selected)
  {
  case 1:
   system("cls");
   if(MyList.InitList ()==OK)
    cout<<"初始化成功!"<<endl;
   else
    cout<<"初始化失败!"<<endl;
       getch();
   break;
  case 2:
   system("cls");
   MyList.DestroyList ();
   cout<<"线性表已销毁!"<<endl;
    getch();
   break;
  case 3:
   system("cls");
   cout<<"您想在哪个位置插入元素:";
   cin>>temp;
   cout<<"请输入要插入的元素:";
   cin>>e2;
   if(MyList.ListInsert (temp,e2)==OK)
    cout<<"插入成功"<<endl;
   else
    cout<<"插入失败,请检查位置是否正确。"<<endl;
    getch();
   break;
  case 4:
   system("cls");
   cout<<"您想删除第几个元素:";
   cin>>temp;
   if(MyList.ListDelete (temp,e)==OK)
    cout<<"删除成功!被删除的元素是:"<<e<<endl;
   else
    cout<<"删除失败!请检查输入位置是否正确"<<endl;
    getch();
   break;
  case 5:
   system("cls");
   cout<<"线性表的长度为:"<<MyList.ListLength ()<<endl;
    getch();
   break;
  case 6:
   system("cls");
   cout<<"您想获得第几个元素的值:";
   cin>>temp;
   if(MyList.GetElem (temp,e)==OK)
    cout<<"第"<<temp<<"个元素为:"<<e<<endl;
   else
    cout<<"第"<<temp<<"个元素不存在,当前线性表中共有"<<MyList.ListLength()<<"个元素。"<<endl;
    getch();
   break;
  case 7:
   system("cls");
   cout<<"你想想获得哪个元素的位置:";
   cin>>e;
   if((temp=MyList.LocateElem (e))!=0)
    cout<<e<<"的位置是:"<<temp<<endl;
   else
    cout<<e<<"在线性表中不存在。"<<endl;
    getch();
   break;
  case 8:
   system("cls");
   cout<<"您想获得哪一元素的前驱:";
   cin>>e;
   if(MyList.PriorElem (e,e2)==OK)
    cout<<e<<"的前驱为:"<<e2<<endl;
   else
    cout<<e<<"没有前驱或不存在"<<endl;
    getch();
   break;
  case 9:
   system("cls");
   cout<<"您想获得哪一元素的后继:";
   cin>>e;
   if(MyList.NextElem(e,e2)==OK)
    cout<<e<<"后继是:"<<e2<<endl;
   else
    cout<<e<<"没后继或不存在。"<<endl;
    getch();
   break;
  case 10:
   system("cls");
   MyList.ClearList ();
   cout<<"线性表已清空!"<<endl;
    getch();
   break;
  case 11:
   system("cls");
   if(MyList.ListEmpty ()==TRUE)
    cout<<"线性表已空!"<<endl;
   else
    cout<<"线性表非空"<<endl;
    getch();
   break;
  case 12:
   system("cls");
   MyList.ListTraverse ();
    getch();
   break;
  case 0:
   exit(0);
   break;
  default:
   cout<<"选择错误!请重新选择!"<<endl;
  }
 }
}

原创粉丝点击