线性表的顺序表示

线性表的顺序表示终于马马虎虎的勉强写完了,

写的不是很完整,开始时比较不理解就是动态分

配内存,现在懂一点点了,数据结构落下很多了,

这几天要好好整了可怜可怜

#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;

#define    OK         1
#define    ERROR      0
#define    TRUE       1
#define    FALSE      0

typedef  int  Status;
typedef  int  ElemType;
typedef struct{
        int* elem;
        int length;
        int listsize;
}Sqlist;

Status InitList(Sqlist &L)       //线性表的初始化
{
       L.elem = (int *)malloc(10 * sizeof(ElemType));
       L.length = 0;
       L.listsize = 10;
       return OK;
}

Status CreatList(Sqlist &L)      //线性表的创建
{
    Status i,n;
    cout<<"please input list length:"<<endl;
    cin>>n;
    L.length = n;
    cout<<"please input L.elem:"<<endl;
    for(i=0;i<n;i++)
       cin>>L.elem[i];
    return OK;   
} 

Status DisplayList(Sqlist &L)      //线性表元素展示
{
       Status i;
       for(i=0;i<L.length;i++)
           cout<<L.elem[i]<<" ";
       cout<<endl;
       return OK;
}

Status ClearList(Sqlist &L)          //线性表的清空
{
    Status i;
    for(i=0;i<L.length;i++)
        L.elem[i] = 0;
    L.length = 0;
    return OK;
} 

Status ListEmpty(Sqlist &L)                      //判断线性表是否为空
{
     if(L.length == 0)
         return TRUE;
     else
         return FALSE;                              
}

Status ListLength(Sqlist &L)             //求线性表的长度
{
        return L.length;
} 

Status GetElem(Sqlist &L, Status &num, ElemType &e)            //用e返回线性表中第i个元素的值 
{
       e = L.elem[num-1];
       return e;
} 

Status LocateElem(Sqlist &L, ElemType &e)             //返回元素e的位序,若不存在返回0
{
       Status i;
       for(i=0;i<L.length;i++)
       {
            if(L.elem[i] == e)
            break;
       }
       if(i<L.length)
          return (i+1);
       else
          return 0;
} 

Status PriorElem(Sqlist &L, ElemType &cur_e, ElemType  &pre_e)         //求元素的前驱
{
       Status i;
       i = LocateElem(L,cur_e);
       if(i == 1)
       {
           //free(pre_e);
           return ERROR;
       }
       else
          {
              pre_e = L.elem[i-2];
              return pre_e;
          }
} 

Status NextElem(Sqlist &L, ElemType  &cur_e,  ElemType  &next_e)            //求元素后继 
{
       Status i;
       i = LocateElem(L, cur_e);
       if(i == L.length)
       {
           //free(next_e);
           return ERROR;
       }
       else
          {
              next_e = L.elem[i];
              return next_e;
          }     
}

Status ListInsert(Sqlist &L, Status &num, ElemType  &e)                 //在线性表i前插入元素e
{
       if(num<1 || num>L.length)
            return ERROR;
       else
       {
           Status j;
           if(L.length == 10)
           {
               L.elem = (int *)realloc(L.elem, (10+20)*sizeof(ElemType));
               L.listsize = 10 + 20;
           }
           for(j=L.length-1;j>=num-1;j--)
              L.elem[j+1] = L.elem[j];
           L.elem[num-1] = e;
           L.length++;
           return OK;
       }
} 

Status ListDelete(Sqlist &L, Status &num, ElemType &e)            //删除第i个数据元素
{
        Status j;
        if(num<1 || num>L.length)
            return ERROR;
        else
        {
            e = L.elem[num-1];
            for(j=num;j<L.length;j++)
                L.elem[j-1] = L.elem[j];
            L.length--;
            return e;
        }
} 

Status DistroyList(Sqlist &L)        //线性表的销毁
{
    free(L.elem);
    return OK;
} 

Status main()
{
       int temp;
       
       Sqlist L;
       ElemType e, cur_e, next_e;
       Status num;
       InitList(L);
       CreatList(L);
       DisplayList(L);
       cin>>num;
       temp = ListDelete(L, num, e);
       //ClearList(L);
       DisplayList(L);
       //temp = ListEmpty(L);
       cout<<temp<<endl;
       DistroyList(L);
       system("pause");
       return 0;
} 




 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页