线性表的顺序存储

 1 //List.h
 2 
 3 
 4 #ifndef LIST_H
 5 #define LIST_H
 6 #define LIST_INIT_SIZE 100  //线性表存储空间的初始分配量
 7 #define LISTINCREMENT 10    //线性表存储空间的分配增量
 8 
 9 #define OK 1
10 #define Error 0
11 typedef int Status;
12 typedef int ElemType;
13 
14 typedef struct {
15     ElemType *elem;//存储空间基地址
16     int length;    //当前长度
17     int listsize;  //当前分配的存储容量(以sizeof(Elemtype)为单位)
18 }SqList;
19 
20 Status InitList(SqList &L);
21 Status ListInsert(SqList &L, int i, ElemType e);
22 Status ListDelete(SqList &L, int i, ElemType &e);
23 void ListTraverse(SqList &L);
24 #endif

 

 

//List.cpp

#include"List.h"
#include<cstdlib>
#include<iostream>
Status InitList(SqList &L)
//构造一个空的线性表
{
    L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    if (!L.elem)
        exit(OVERFLOW);
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
    return OK;
}

Status ListInsert(SqList &L, int i, ElemType e)
//在顺序线性表L中第i个位置之前插入新的元素e
{
    if (i<1 || i>L.length + 1)
        return Error;
    if (L.length >= L.listsize)
    {
        ElemType * newbase;
        newbase= (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
        if (!newbase)
            exit(OVERFLOW);
        L.elem = newbase;//新基地址
        L.listsize += LISTINCREMENT;
    }
    ElemType* q;
    ElemType* p;
    q = &(L.elem[i - 1]);
    for (p = &(L.elem[L.length - 1]); p >= q; --p)
        *(p + 1) = *p;
    *q = e;
    ++L.length;
    return OK;
}

Status ListDelete(SqList &L, int i, ElemType &e)
{
    if (i<1 || i>L.length)
        return Error;
    ElemType* p;
    ElemType* q;
    p = &(L.elem[i - 1]);  //p为被删除元素的位置
    e = *p;                //被删除元素的值赋给e
    q = L.elem + L.length - 1;
    for (++p; p <= q; ++p)
        *(p - 1) = *p;
    --L.length;
    return OK;
}

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

 

主函数实现

//main.cpp

#include"List.h"
#include<iostream>
using namespace std;

int main()
{
    SqList L;
    InitList(L);
    ElemType n1 = 1;
    ElemType n2 = 2;
    ElemType n3 = 3;
    ElemType n4 = 4;
    ElemType n5 = 5;
    ElemType n6 = 6;
    ElemType n7 = 7;
    ElemType n8 = 8;
    ElemType n9 = 9;
    ElemType n0 = 0;
    ElemType temp = 0;

    ListInsert(L, 1, n7);
    ListInsert(L, 2, n7);
    ListInsert(L, 3, n7);
    ListInsert(L, 4, n4);
    ListInsert(L, 5, n6);
    ListInsert(L, 6, n5);
    ListInsert(L, 7, n8);
    ListInsert(L, 8, n3);
    ListTraverse(L);

    ListDelete(L, 5, temp);
    cout << temp;
    cout << endl;

    ListTraverse(L);

    system("pause");
    return 0;
}

 

转载于:https://www.cnblogs.com/sgawscd/p/10176046.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值