C++実装シーケンステーブル基本操作


このコードは前編のブログ線の線形表の基礎概念に従って、順序表の実現は概念をコードで実現します
#include
#include

using namespace std;

//        
#define TRUE   1
#define FALSE  0
#define OK     1
#define ERROR  0
#define INFEASIBLE -1
#define OVERFLOW   -2
//Status       ,           

typedef int Status;    // Status     int 
typedef char ElemType; //Elemtype     char 

#define  MAXSIZE  100       //             
typedef  struct
{
  ElemType *elem;           //        
  int length;               //    
}SqList;                    //        Sqlist 

Status InitList(SqList &L);   //     ,         L 

void DestroyList(SqList &L);  //     L 

void ClearList(SqList &L);    //     L 

int ListEmpty(SqList L);      //     L      

int ListLength(SqList L);     //     L        
  
Status GetElem(SqList L,int i,ElemType &e);       // e     L  i    

Status LocateElem(SqList L,ElemType e);    //  L  1   e      L    。         ,     0

Status ListInsert(SqList &L,int i,ElemType e);    //    L  i       e 

Status ListDelete(SqList &L,int i,ElemType &e);   //     L  i     e   

void ListTraversals(SqList &L);  //    L    ,       L         


int main(){
	
	SqList L;   //     L 
	 
	if(InitList(L))
		cout << "   L    "  << endl;
	
//	ListInsert(L,1,'1');  
	if(ListInsert(L,1,'s'))  //              
		cout << "    "  << endl;
	else
		cout << "    "  << endl;
					 
//	ListInsert(L,2,'m');
	if(ListInsert(L,2,'m'))
		cout << "    "  << endl;
	else
		cout << "    "  << endl;	
		
//	ListInsert(L,3,'h');
	if(ListInsert(L,3,'h'))
		cout << "    "  << endl;
	else
		cout << "    "  << endl; 
	
	cout << "     :"  << endl;
	ListTraversals(L);
	
	cout << "     :" << ListLength(L) << endl; 
	
	ElemType e; 
	ListDelete(L, 1, e);
	cout << "      :" << e << endl;
	cout << "          :" << ListLength(L) << endl;
	
	ElemType a='m';
	int aa=LocateElem(L,a);
	cout << "   "  << a << "      :" << aa << endl;
	
	GetElem(L,1,e);
	cout << "   L     :" << e << endl;
	
	cout << "     " << endl; 
	ClearList(L);
	
	
	ListEmpty(L);
	if(ListEmpty(L))
		cout << "     " << endl;
	else
		cout << "     " << endl;
			 
	DestroyList(L);
	cout << "      " << endl; 
	
	
	return 0;
} 



Status  InitList(SqList &L)       //         L
{
   L.elem = new ElemType[MAXSIZE]; //        
   if(!L.elem) exit(OVERFLOW) ;   //      
   L.length=0;                    //     0
   return OK;
 }

void DestroyList(SqList &L){
	if(L.elem)delete L.elem;   //      
}	

void ClearList(SqList &L){
	L.length=0;                //          0
}	

int ListEmpty(SqList L){
	if (L.length == 0) return 1;
	else return 0;
}

int ListLength(SqList L){
	return (L.length);       //  SqList   length 
}

Status  GetElem(SqList L ,int i ,ElemType &e)
{
     if(i < 1 || i > L.length)  return ERROR;  //  i     
     e=L.elem[i-1];                        //elem[i-1]   i     
     return OK;
 }

Status  LocateElem(SqList L,ElemType e)     //    L     e     ,     (      )
{
    for(int i=0 ;i<L.length ;i++)
       if(L.elem[i] == e)   return i+1;  //    ,    i+1
    return 0;                            //    ,  0
 }

Status  ListInsert(SqList  &L,int i,ElemType e)
{
      if( (i<1)||(i>L.length+1) )   return ERROR;
      if(L.length == MAXSIZE)       return ERROR;
      for(int j=L.length-1 ;j>=i-1 ;j--){ 
    		L.elem[j+1] = L.elem[j];        //            
      } 
	  L.elem[i-1] = e;                  //     e     i    
      ++L.length;                       //   1
      return OK;
 }

Status  ListDelete(SqList  &L,int  i, ElemType &e)
{
     if( (i<1) || (i>L.length) )       return ERROR;
     e = L.elem[i-1];
     for(int j=i ;j<=L.length-1 ;j++)
        L.elem[j-1]=L.elem[j];        //            
     --L.length;                      //   1
     return OK;
 }

void ListTraversals(SqList &L){      //     
	for(int i=0;i<L.length;i++){
		cout << L.elem[i] << endl;
	}
}