顺序链表

> #include "StdAfx.h"
> #include <stdio.h>
> #include <string>
> #include <iostream>
> 
> using namespace std; //InitList() 
> typedef string Elem;
> #define Maxlenght 10
>class SqList{ 
> public:   
>   bool initSqlist();      
>   bool insertSqlist(int position, Elem p);    
>   Elem GetElem(int position);     
>   int GetLength();    
>   int  GetmaxLength();    
>   int  GetLocalElem(const Elem value);    
>   Elem  GetPreviousElem(Elem value);  
>   Elem GetNextElem(Elem value);   
>   bool EnLargeList();     
>   bool ClearList();
> private:  
     unsigned int length; 
>    unsigned int maxLength; 
> }; 
> bool SqList::initSqlist() 
> {    
>   p=new Elem[Maxlenght];    
>   length=0;
>   maxLength=Maxlenght;
>   if(p!=nullptr) return true;    else return false;    
> }; 
> bool SqList::insertSqlist(int position,Elem value) 
> {
>   if(length+1>maxLength)  
>   { EnLargeList();    }   
>   if (position<0 )return false; 
>   else 
>   {       
>      for(int j=length-1;j>=position;--j)      
>      {
>        p[j+1]=p[j];           
>        cout<<"["<<j<<"]"<<p[j]<<endl;         
>       }   
>   }
>   p[position]=value;
>   ++length;       
>   return true; 
> }; 
> Elem SqList::GetElem(int position) 
> {     if(position <maxLength)     return p[position]; 
> }; 
> int SqList::GetLength() {     return length; }; 
> int SqList::GetmaxLength() 
> {return maxLength; } 
> int  SqList::GetLocalElem(const Elem value) 
> {
>   for(int i=0;i<=length-1;i++) 
>   {   if (p[i]==value)return i;
>       else
>          return -1;   
>   } 
> } 
> Elem SqList::GetPreviousElem(Elem value) 
> {     int i=GetLocalElem(value);  
>    if (i!=0 &&i !=-1) {   return p[i-1]; } 
> }
> Elem SqList::GetNextElem(Elem value) 
> {     int i=GetLocalElem(value);
>   if(i!=-1 &&i!=length-1)     
>   {     return p[i+1];    }
> } 
> bool SqList::EnLargeList() 
> {     
>  Elem *newp=new Elem[maxLength+newSize];
>   for(int i=0;i<=length-1;++i){       newp[i]=p[i];   }
>   if(!DestoryList())  
>   { return false;     } else{         
>      p=newp;
>     maxLength+=newSize;       
>      return true;     
>    }  
>    return false; 
>} 
> bool SqList::DestoryList() 
> { 
>    delete []p;    
>    p=NULL;
 }
int main() {
> 
>   SqList sql_a,sql_b,sql_c;   
>   sql_a.initSqlist();
>   sql_a.insertSqlist(0,"10");     
>   sql_a.insertSqlist(1,"40");
>   sql_a.insertSqlist(2,"20");     
>   sql_a.insertSqlist(2,"20");
>   sql_a.insertSqlist(3,"30");         
>   sql_a.insertSqlist(1,"50");
>   sql_a.insertSqlist(1,"60");     
>   sql_a.insertSqlist(1,"70");
>   sql_a.insertSqlist(1,"80");     
>   sql_a.insertSqlist(1,"90");
>   sql_a.insertSqlist(1,"100");    
>   sql_a.insertSqlist(1,"102");
> 
>   //cout<<value<<endl;
> 
>   cout<<"总长度为:"<<sql_a.GetLength()<<endl;     
>   for(int i=0;i<sql_a.GetLength();i++)    
>   { cout<<sql_a.GetElem(i)<<endl;     }
>   system("pause"); 
>   return 1; 
> }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值