数据结构之对线性表的操作(C语言版)

#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "ctype.h"
#include <malloc.h>


#define LISTINITSIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0

typedef int Status;
typedef int ElemType;

typedef struct

{
    ElemType *elem;
    int length;
    int listsize;
}SqList;


Status InitList(SqList &L)
{
    L.elem=(ElemType*)malloc(LISTINITSIZE*sizeof(ElemType));
    if(!L.elem)
        exit(-1);
    L.length=0;
    L.listsize=LISTINITSIZE;
    return TRUE;
}


Status DestroyList(SqList &L)
{  
   if (L.elem == NULL)
   {
       printf("线性表不存在!\n");
       return FALSE;
   }

   free(L.elem);
   L.elem = NULL;
   return TRUE;
}

Status ClearList(SqList &L)
{
    if (!L.elem)
    { 
        printf("线性表不存在!\n");
        return FALSE;
    }

    L.length = 0;
    return TRUE;
}

int ListEmpty(SqList L)
{
    if (!L.elem)
    { 
        printf("线性表不存在!\n");
        return -1;
    }

    if(L.length==0)
        return TRUE;
    else
        return FALSE;
}

int ListLength(SqList L)
{
    if (!L.elem)
    { 
        printf("线性表不存在!\n");
        return -1;
    }
    return L.length;
}

Status GetElem(SqList L,int i,ElemType *e)
{
    if (!L.elem)
    { 
        printf("线性表不存在!\n");
        return FALSE;
    }

    if (i<1 || i>L.length)
    {
        printf("指定的位置不合法,应该在1到%d之间!\n", L.length);
        return FALSE;
    }
    *e = L.elem[i-1];
    return TRUE;
}

int PriorElem(SqList L,ElemType cur_e,ElemType *pre_e)
{
    int i=0;
    while ((L.elem[i] != cur_e)&&(i <L.length))
        i++;
    if (i < L.length)
    {
        if (i == 0)
            return -1;
        *pre_e = L.elem[i-1];
        return TRUE;
    }else
        return FALSE;
}

int NextElem(SqList L,ElemType cur_e,ElemType *next_e)
{
    int i=0;
    while ((L.elem[i] != cur_e)&&(i <L.length))
        i++;
    if (i < L.length)
    {
        if (i == L.length-1)
            return -1;
        *next_e = L.elem[i+1];
        return TRUE;
    }else
        return FALSE;

}

Status ListInsert(SqList &L,int i,ElemType e)
{
    ElemType *p,*q,*newelem;
    if(i<1 || i>L.length+1)
    {
        printf("指定的插入位置不合法,应该在1到%d之间!\n", L.length +1);
        return FALSE;
    }

    if(L.length>=L.listsize)
    {
        newelem=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newelem)
            exit(-1);

        L.elem=newelem;
        L.listsize+=LISTINCREMENT;
    }

    q=L.elem+i-1;
    for(p=L.elem+L.length-1;p>=q;p--)
        *(p+1)=*p;

    *q=e;
    L.length++;
    return TRUE;
}



Status ListDelete(SqList &L,int i,ElemType *e)
{
    ElemType *p,*q,*newelem;
    int pos;
    if(i<1 || i>L.length)
    {
        printf("指定的删除位置不合法,应该在1到表长之间!\n");
        return FALSE;
    }
    *e = L.elem[i-1];

    for (pos=i-1; pos < L.length-1 ; pos++)
        L.elem[pos] = L.elem[pos+1];

    L.length --;
    return TRUE;



}

Status ListTraverse(SqList L)
{
    ElemType *p;
    if (!L.elem)
    { 
        printf("线性表不存在!\n");
        return FALSE;
    }

    printf("该线性表中数据元素如下:");
    for(p=L.elem;p<=L.elem+L.length-1;p++)
        printf("%d  ",*p);
        printf("\n");
    return TRUE;
}

void MergeList(SqList La,SqList Lb,SqList &Lc)
{
//
}

int main()
{
    Status s = TRUE;
    SqList La;
    La.elem = NULL;
    int i = 0;
    int pos=0, num=0;
    char c;

    while ( i>=0 )
    {   system("cls");
        printf("1----初始化化一个线性表\n");
        printf("2----销毁线性表\n");
        printf("3----清空线性表\n");
        printf("4----判断线性表是否为空\n");
        printf("5----求线性表长度\n");
        printf("6----获取线性表制定位置元素\n");
        printf("7----求前驱\n");
        printf("8----求后继\n");
        printf("9----在线性表指定位置插入元素\n");
        printf("10---删除线性表指定位置元素\n");
        printf("11---显示线性表\n");
        printf("    若退出,输入一个负数!\n");
        printf("请输入操作代码:");
        scanf("%d", &i);

        if (i==1)
        {
            s = InitList(La);
            if (s)
              printf("已成功创建一个线性表"); 
        }
        else if (i == 2)
        {
            if (DestroyList(La))
                printf("已成功销毁线性表, 如需对线性表进行操作需要先初始化一个线性表\n");
        }
        else if (i == 3)
        {
            if (ClearList(La))
                printf("已经清空线性表\n");
        }
        else if (i == 4)
        {
            int res;
            res= ListEmpty(La);
            if (res == TRUE)
                printf("该线性表为空表\n");
            else 
                if (res == FALSE)
                printf("该线性表不是空表\n");

        }
        else if (i == 5)
        { int len;
          len = ListLength(La);
          if (len >=0)
            printf("线性表当前长度为:%d \n", len);
        }
        else if (i == 6)
        {
            int pos =0 ;
            ElemType element; 

            printf("请输入你所希望取的元素的位置:");
            scanf("%d", &pos);

            if (GetElem(La, pos, &element))
                printf("该线性表中第%d位置上的元素为%d\n", pos, element);

        }
        else if (i == 7)
        {
            ElemType cur_elem, pre_elem;
            int resu;
            if (!La.elem)
                printf("线性表不存在!\n");
            else
            {
                printf("请输入需要求前驱的数:");
                scanf("%d", &cur_elem);

                resu = PriorElem(La, cur_elem, &pre_elem);
                if (resu == TRUE)
                    printf("%d的前驱是%d\n", cur_elem, pre_elem);
                else 
                    if (resu == -1)
                        printf("%d是线性表中第一个元素,没有前驱!\n", cur_elem);
                    else
                        printf("%d不存在线性表中,故无法求其前驱!\n");
            }
        }
        else if (i == 8)
        {
            ElemType cur_elem, pre_elem;
            int resu;
            if (!La.elem)
                printf("线性表不存在!\n");
            else
            {
                printf("请输入需要求后继的数:");
                scanf("%d", &cur_elem);

                resu = NextElem(La, cur_elem, &pre_elem);
                if (resu == TRUE)
                    printf("%d的后继是%d\n", cur_elem, pre_elem);
                else 
                    if (resu == -1)
                        printf("%d是线性表中最后一个元素,没有后继\n", cur_elem);
                    else
                        printf("%d不存在线性表中,故无法求其后继\n", cur_elem);
            }
        }
        else if (i == 9)
        {
            if (!La.elem)
                printf("线性表不存在!\n");
            else
            {
                printf("请输入插入的位置和数据, 格式为: 位置 数据\n");
                scanf("%d %d", &pos, &num);
                if (ListInsert(La,pos,num))
                    printf("插入成功\n");
            }
        }
        else if (i == 10)
        {
            int num;
            if (!La.elem)
                printf("线性表不存在!\n");
            else
            {
                printf("请输入需要删除数据位置:");
                scanf("%d", &pos);
                if (ListDelete(La, pos, &num))
                   printf("插入成功, 删除的数据为%d\n", num);
            }

        }
        else if (i == 11)
        {
            s=ListTraverse(La);

        }
        else 
            break;
        c = _getch();

    }       
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值