加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

顺序表小结

(2014-02-19 21:24:48)
标签:

数据结构

顺序表

c语言

分类: 数据结构总结

学完了数据结构也不知道自己学了什么,于是就想总结,如果有不对的地方,还请大家指出,评论.非常感谢!

参考严蔚敏《数据结构C语言版》

#include

#include

#define LIST_INIT_SIZE  100 //初始化分配量

 

#define LISTINCREMENT 10 //存储空间的分配增量

#define ok  1

 

typedef int Status;

 

typedef int ElemType;

 

typedef struct{

 

       ElemType *elem;//储存空间基址

 

       int length;//当前长度

 

       int listsize;//当前的分配的存储容量 (以sizeof (ElemType)为单位)

}SqList;

 

Status InitList_Sq(SqList &L){

 

       L.elem = (ElemType *)malloc(LIST_INIT_SIZE *sizeof(ElemType));

 

       if ( !L.elem) exit(0); //存储分配失败

       L.length =0;

       L.listsize = LIST_INIT_SIZE;

 

 

       return ok;

}

 

 

Status ListInsert_Sq(SqList &L,int i,ElemType e)   

  

    //在顺序线性表L中第i个位置之前插入新的元素    

    //i的合法值为1<=i<=ListLength_Sq(L)+1     

    if(i <1 || i> L.length + 1)   

        return false;   //i值不合法     

    if(L.length >= L.listsize)   //当前存储空间已满,增加分配     

     

        ElemType *newbase = (ElemType *)realloc(L.elem,(L.listsize + LISTINCREMENT )* sizeof(ElemType));   

        if(!newbase)   

            exit(1);    //存储分配失败     

        L.elem = newbase;//新基址     

        L.listsize += LISTINCREMENT;    //增加存储容量     

     

   

    ElemType *q = &(L.elem[i-1]);//q为插入位置     

   

    for(ElemType *p = &(L.elem[L.length-1]); p>=q ;--p)   

        *(p+1) = *p;    //插入位置及之后的元素右移     

   

    *q = e;     //插入    

    ++L.length;     //表长增 

      

       //ElemType l = *q; //用一个自定义的变量 返回指针q中所指代的值

    return 0;   

}

 

 Status display_sq(SqList &L){

 

        for ( int i = 0; i

        {

               printf ("   %d",L.elem[i]);

       //    cout << L.elem[i]<<endl;

        }

        printf ("\n\n");

        return true;

 }

 

 void visit (ElemType& m){

        printf (" %d  ",m);

 }

 void ListTraverse(SqList L,void(*vi)(ElemType&))

 { //初始条件:顺序线性表L已存在

        //操作结果:依次对L的每个数据元素调用函数vi()

        // vi()的形参加′&′,表明可通过调用vi()改变元素的值

        

        ElemType *p;

        int i;

        p=L.elem;

        for(i=1;i<=L.length;i++)

               vi(*p++);

        printf("\n");

 }

 

 

 

 

Status Delete (SqList &L,int i,ElemType &e){//线性表L中删除第i个元素 并用e 返回其值

       if ((i<1)|| (i>L.length))  return 0;

       int * p =&(L.elem[i-1]);

       e =*p;

       int *q = L.length +L.elem-1;

       for ( ++p ;p<=q; ++p){

              *(p-1) = *p;

       }

       --L.length;

       return e;

}

 

 

Status Search (SqList &L, int m){//查找m

              int flag =0;

       for (int i=0;i

      

              if (L.elem[i] ==m){

                     printf ("  %d 呵呵  我找的很准!\n", m);

                     flag =1;

              }

       }

       if (flag ==0){

              printf ("呜呜..  没有找到数据.\n");

       }

       return 0;

}

 

 

bool ListEmpty(SqList L){

       if (L.length==0)

              return true;

       else

              return false;

}

 

 

 

void ClearList(SqList &L)

{ //初始条件:顺序线性表L已存在。操作结果:将L重置为空表     

       L.length=0;

}

 

 

 

void DestroyList (SqList &L){

       //销毁线性表L

       free (L.elem);

       L.elem=NULL;

       L.length=0;

       L.listsize=0;

      

}

 

 

 

 

 

Status ListLength(SqList L){

       return L.length;

}

 

Status GetElem(SqList L,int i,Status &e){

       //e返回L中第i的元素

       if ((i<1)|| (i>L.length))  return -1;

       e=L.elem[i-1];

       return ok;

      

      

}//e返回第i个数据元素的值

 

Status compare (ElemType m,ElemType n){

       if (m==n)

              return 1;

       else

              return 0;

}

 

 

Status LocateElem(SqList L,ElemType e,Status(*compare)(ElemType m,ElemType n)){

       //返回L中第一个与e满足关系compare()的位序,若这样的位序不存在返回-1

       for (int i=0;i

              if (compare(e,L.elem[i]))

                     return i;

       }

         return -1;

}

 

 

Status PriorElem (SqList L,ElemType cur_e,ElemType &pre_e){

       //cur_eL的数据元素,且不是第一个,则用途pre_e返回它的前驱,

       //否则操作失败,pre_e无定义

//    if (L.elem[0]==cur_e) return false;

       for(int i=1;i

              if (cur_e ==L.elem[i]){

                     pre_e =L.elem[i-1];

                     return ok;

              }

       }

        return -2;//说明没有此元素

}

 

 

Status NextElem (SqList L,ElemType cur_e,ElemType &next_e){

       //cur_eL的数据元素,且不是最后一个元素,则用next_e返回它的后继,

       //否则操作失败,next_e无定义

//    if(L.elem[L.length -1]==cur_e)  return -2;

       for (int i=0;i

              if (cur_e==L.elem[i]){

                     next_e =L.elem[i+1];

                     return ok;

              }

       }

 return -2;//说明没有该元素

      

}

 

 

 void  chose(int m,SqList &L);

int main()   

  

 

 

              int ch;

              SqList L;

           printf("1,建立新表\n");

           printf("2,插入元素\n");

           printf("3,删除元素\n");

           printf("4,查找顺序表中元素\n");

              printf("6对表中前驱,后继的检验\n");

           printf("0,结束程序\n");

        printf ("请输入需要操作对应的数字:");

             

              for(ch=1;ch!=0;)

                scanf("%d",&ch);

                     chose(ch,L);

             

                     printf("请输入对应的数字");

                     printf ("你想继续吗? 0表示结束\n");

              }

 

              return 0;

           

 

 

 void  chose(int m,SqList &L)

{

      

      

       switch (m){

              case 1: {

                                   InitList_Sq( L);

                                   int n ;

                                   printf ("请输入链表的长度:");

                                   scanf("%d",&n);

                                   printf ("\n请输入数据: ");

                                   for (int i =1;i

                                          int e;

                                          scanf ("%d",&e);

                                          ListInsert_Sq(L,i,e);

 

                                   }

                                   ListTraverse( L,visit);  break;

                     }

              case 2:{

                     int  m,num;

                     printf ("请输入你要插入的位置 数字: ");

                     scanf("%d%d",&m,&num);

                     //fflush (stdin);

                     ListInsert_Sq(L, m,num);

                     display_sq( L);

                     break;

              }

              case 3: {

                     int i,b,e;

                     printf ("请输入删除的位置 :");

                     scanf("%d",&i);

                     b =Delete (L, i,e);

                     printf ("删除元素后带回的值 : ",b);

                     display_sq( L);break;

 

              }

              case 4:{

                     int num;

                     printf ("请输入要查找的数: ");

                     scanf(  "%d",&num);

                     Search (L, num);

                     display_sq( L);break;

 

              }

              case 6:int e;

                     int next_e;

                     int pre_e;

                     int num;

                     GetElem(L,2,e);

                     printf ("请输入一个数据:");

                     scanf("%d",&num);

                     int j = NextElem (L,num,next_e);

                     int k =PriorElem(L,num,pre_e);

                     if (k==-2)printf ("该元素没有前继元素\n");

                     else{     

                            printf ("它的前继为%d\n",pre_e);

                            }

                     //printf ("它的前驱为:%d\n",pre_e);

                     if (j==-2)printf ("该元素没有后继元素\n");

                     else{     

                            printf ("它的后继为%d\n",next_e);

                            }

                     printf ("第二个元素是   %d\n",e);

                     break;

       }

}

http://s15/mw690/003AAPhVgy6GIsR2q5w7e&690

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有