【数据结构实验一】顺序表的基本操作(动态分配)

目录

指针法

数组法


输入格式:

第1行输入若干个构成顺序表的正整数,使用 -1 结束,例如:1 2 3 4 5 -1

第2行输入要插入顺序表的元素。例如:99

测试数据在1~99之间

输出格式:

参照主函数及输出样例,输出格式使用%3d。

输入样例:

1 2 3 4 5 -1
99

输出样例:

顺序表里的元素有:
  5  4  3  2  1
顺序表的长度是:5
在顺序表头位置插入元素99...
顺序表里的元素有:
 99  5  4  3  2  1
在顺序表尾位置删除元素1...
顺序表里的元素有:
 99  5  4  3  2
正在清空顺序表...
顺序表为空!

指针法

#include <stdio.h>
#include <malloc.h>
#define LIST_INIT_SIZE 10 //线性表存储空间的初始分配量
#define LISTINCREMENT 2 //线性表存储空间的分配增量

typedef int ElemType; //元素类型
typedef struct
{
    ElemType *elem; //存储空间的基址
    int length; //当前长度
    int listsize; //当前分配的存储容量
}SqList;

void InitList_Sq(SqList &L); //构造一个空的线性表
void InsertList_Sq(SqList &L, int i, ElemType e); //插入操作
void DeleteList_Sq(SqList &L, int i, ElemType &e); //删除操作
int LocateElem_Sq(SqList &L, ElemType e); //查找
void PrintList_Sq(SqList &L); //输出顺序表
//int Lenth_Sq(SqList &L); //求长度
//ElemType GetData_Sq(SqList &L, int i); //获取第i个元素
void CreatList_Sq(SqList &L); //创建线性表
void ClearList_Sq(SqList &L); //清空操作

int main()
{
    SqList L;
    InitList_Sq(L);
    CreatList_Sq(L);
    ElemType e;
    scanf("%d", &e);
    PrintList_Sq(L);
    printf("顺序表的长度是:%d\n",  L.length);

    InsertList_Sq(L, 1, e);
    printf("在顺序表头位置插入元素%d...\n", e);
    PrintList_Sq(L);

    DeleteList_Sq(L, L.length, e);
    printf("在顺序表尾位置删除元素%d...\n", e);
    PrintList_Sq(L);

    ClearList_Sq(L);
    PrintList_Sq(L);

    return 0;
}

void InitList_Sq(SqList &L)
{
    L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem) return;
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
}

void InsertList_Sq(SqList &L, int i, ElemType e)
{
    if(i<1||i>L.length+1) return;

    if(L.length>=L.listsize)
    {
        ElemType *newbase;
        newbase = (ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newbase) return;
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    ElemType *p = &(L.elem[i-1]); //指针p指向第i个元素
    ElemType *q;
    for(q=&(L.elem[L.length-1]);q>=p;q--)
        *(q+1) = *q;
    
    *p = e;
    L.length++;
}

void DeleteList_Sq(SqList &L, int i, ElemType &e)
{
    if(i<1||i>L.length) return;

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

    ElemType *q = L.elem+L.length-1;//把q指针定位到最后一个元素
    
    for(++p;p<=q;++p)
        *(p-1) = *p; 

    L.length--;
}

int LocateElem_Sq(SqList &L, ElemType e)
{
    for(int i=0;i<L.length;i++)
        if(L.elem[i] == e)
            return i+1;
}

void PrintList_Sq(SqList &L)
{
    if(L.length == 0) printf("顺序表为空!");
    else{
        printf("顺序表里的元素有:\n");
        printf(" ");
        for(int i=0;i<L.length-1;i++)
            printf("%2d ",L.elem[i]);
        printf("%2d\n",L.elem[L.length-1]);
    }
}

void ClearList_Sq(SqList &L)
{
    for(int i=0;i<L.length;i++)
        L.elem[i]=0;
    L.length = 0;
    printf("正在清空顺序表...\n");
}

void CreatList_Sq(SqList &L)
{
    if(L.length>LIST_INIT_SIZE) return;
    
    int i=L.length+1;
    
    ElemType e;
    scanf("%d",&e);
    while(e!=-1)
    {
        InsertList_Sq(L, i, e);
        scanf("%d",&e);
    }
}

数组法

#include <bits/stdc++.h>
#include <iostream>
#include <malloc.h>
#define LIST_INIT_SIZE 10 //线性表存储空间的初始分配量
#define LISTINCREMENT 2 //线性表存储空间的分配增量
using namespace std;

typedef int ElemType; //元素类型
typedef struct
{
    ElemType *elem; //存储空间的基址
    int length; //当前长度
    int listsize; //当前分配的存储容量
}SqList;

void InitList_Sq(SqList &L); //构造一个空的线性表
void InsertList_Sq(SqList &L, int i, ElemType e); //插入操作
void DeleteList_Sq(SqList &L, int i, ElemType &e); //删除操作
int LocateElem_Sq(SqList &L, ElemType e); //查找
void PrintList_Sq(SqList &L); //输出顺序表
//int Lenth_Sq(SqList &L); //求长度
//ElemType GetData_Sq(SqList &L, int i); //获取第i个元素
void CreatList_Sq(SqList &L); //创建线性表
void ClearList_Sq(SqList &L); //清空操作

int main()
{
    SqList L;
    InitList_Sq(L);
    CreatList_Sq(L);
    ElemType e;
    scanf("%d", &e);
    PrintList_Sq(L);
    printf("顺序表的长度是:%d\n",  L.length);

    InsertList_Sq(L, 1, e);
    printf("在顺序表头位置插入元素%d...\n", e);
    PrintList_Sq(L);

    DeleteList_Sq(L, L.length, e);
    printf("在顺序表尾位置删除元素%d...\n", e);
    PrintList_Sq(L);

    ClearList_Sq(L);
    PrintList_Sq(L);

    return 0;
}

void InitList_Sq(SqList &L)
{
    L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem) return;
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
}

void InsertList_Sq(SqList &L, int i, ElemType e)
{
    if(i<1||i>L.length+1) return;

    if(L.length>=L.listsize)
    {
        ElemType *newbase;
        newbase = (ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
        if(!newbase) return;
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    for(int j=L.length-1;j>=i-1;j--)
        L.elem[j+1] = L.elem[j];
    L.elem[i-1]=e;
    L.length++;
}

void DeleteList_Sq(SqList &L, int i, ElemType &e)
{
    if(i<1||i>L.length) return;

    e=L.elem[i-1];

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

    L.length--;
}

int LocateElem_Sq(SqList &L, ElemType e)
{
    for(int i=0;i<L.length;i++)
        if(L.elem[i] == e)
            return i+1;
}

void PrintList_Sq(SqList &L)
{
    if(L.length == 0) cout << "顺序表为空!";
    else{
        cout << "顺序表里的元素有:" << endl;
        cout << " ";
        for(int i=0;i<L.length-1;i++)
            printf("%2d ",L.elem[i]);
        printf("%2d\n",L.elem[L.length-1]);
    }
}

void ClearList_Sq(SqList &L)
{
    for(int i=0;i<L.length;i++)
        L.elem[i]=0;
    L.length = 0;
    cout << "正在清空顺序表..." << endl;
}

void CreatList_Sq(SqList &L)
{
    if(L.length>LIST_INIT_SIZE) return;

    int i=L.length+1;

    ElemType e;
    cin >> e;
    while(e!=-1)
    {
        InsertList_Sq(L, i, e);
        cin >> e;
    }
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值