数据结构

顺序表

2020-09-15  本文已影响0人  往sir_b2a2

一个C++工程
标头.h

#pragma once
#include <iostream>
#include <stdlib.h>
#include <malloc.h>
#define ListInitSize 100
#define ListIncreament 10
#define True 1
#define False 0
typedef int ElemType;
typedef int Status;
using namespace std;

标头1.h

#pragma once
#include "标头.h"
typedef struct node
{
    ElemType* elem;
    int size;//已规定的顺序表的元素个数
    int length;//顺序表长度或元素个数
    int increament;//扩容量
}SqList;
void Init(SqList& L, int size, int increament)
{
    L.elem = (ElemType*)malloc(size * sizeof(ElemType));
    L.length = 0;
    L.size = size;
    L.increament = increament;
}
void Create(SqList& L)//初始化建表和赋值
{
    int i;
    cout << "输入顺序表的元素个数:" ;
    cin >> L.length;
    if (L.size <= L.length)
        L.elem = (ElemType*)realloc(L.elem,(L.size + L.increament) * sizeof(ElemType));
    if (!L.elem)
    {
        cout << "ERROR!";
        return;
    }
    cout << "分别输入你想创建的顺序表元素数值:";
    for (i = 0; i < L.length; i++)
        cin >> L.elem[i];
}
void Print(SqList& L)
{
    for (int i = 0; i < L.length; i++)
        cout << L.elem[i] << " ";
    cout << endl;
}
int Search(SqList L,ElemType FindElem)//按数值找位置
{
    for (int i = 0; i < L.length; i++)
    {
        if (FindElem == L.elem[i])
            return i+1;//因为C语言是从下标为0开始的,表中第i个数据对应下标是第i-1个位置
    }
    return -1;
}
void Change(SqList &L, ElemType NewElem,ElemType Elem)//改变值
{
    for (int i = 0; i < L.length; i++)
    {
        if (Elem == L.elem[i])
        {
            L.elem[i] = NewElem;//这就不用管什么i-1与i,找到并改变值就行
        }
    }
}
void Insert(SqList& L, int i, ElemType InsertElem)//在第i-1个下标插入元素
{
    int k;
    if (i<1 || i>(L.length + 1))
    {
        cout << "插入位置不合法";
        exit(0);
    }
    else if (L.length >= L.size)
    {
        L.elem = (ElemType*)realloc(L.elem, (L.size + L.increament) * sizeof(ElemType));
    }
    if (L.length + 1 == i)//尾插
    {
        L.elem[L.length] = InsertElem;
        L.length++;
    }
    else 
    {
        for (k = L.length; k >= i; k--)//原来的i位数据及其后面的元素后移
            L.elem[k] = L.elem[k - 1];
        L.elem[i - 1] = InsertElem;
        L.length++;
    }
}
void Delete(SqList& L, int i)//按位置删除,第i个元素,第i-1个下标
{
    if (i<1 || i>L.length)
    {
        cout << "ERROR!";
        exit(0);
    }
    else
    {
        int k;
        for (int k = i; k <= L.length; k++)//k代表顺序表中第i个元素,不要改为k=i-1和k<L.length;前移
        {
            L.elem[k - 1] = L.elem[k];
        }
        L.length--;
    }
}
void Delete2(SqList& L, ElemType DeElem)//按数值删除
{
    if (L.length < 1)
    {
        cout << "ERROR!";
        exit(0);
    }
    else
    {
        int i;
        for (i = 0; i < L.length; i++)
        {
            if (L.elem[i-1] == DeElem)//第i个元素,第i-1个下标
            {
                for (int k = i; k <= L.length; k++)//k代表顺序表中第i个元素,不要改为k=i-1和k<L.length;前移
                {
                    L.elem[k - 1] = L.elem[k];
                }
            }
        }
        L.length--;
    }
}
void Destroy(SqList& L)
{
    if (L.elem == NULL)
    {
        cout << "ERROR!";
        exit(0);
    }
    else
    {
        free(L.elem);
        L.length = 0;
        L.size = 0;
    }

}
void Clear(SqList& L)
{   
    if (L.elem == NULL)
    {
        cout << "ERROR!";
        exit(0);
    }
    else
    {
        L.length = 0;
    }
}
Status IsEmpty(SqList L)
{
    if (0 == L.length)
        return True;
    else
        return False;
}
int GetLength(SqList L)
{
    return L.length;
}

Status GetFront(SqList L,int i)//获得前一个元素
{
    if (i<=1 || i>L.length-1)
    {
        cout << "ERROR!";
        exit(0);
    }
    return L.elem[i - 1];
}
Status GetBack(SqList L,int i)//获得后一个元素
{
    if (i < 1 || i >= L.length-1)
    {
        cout << "ERROR!";
        exit(0);
    }
    return L.elem[i + 1];
}


源.cpp

#include<iostream>
#include<stdlib.h>
#include"标头.h"
#include"标头1.h"
using namespace std;

int main()
{
    SqList L;
    cout << "**********顺序表初始化**********" << endl;
    cout << "输入顺序表的长度和扩容量:" ;
    int size, increament;
    cin >> size >> increament;
    Init(L, size, increament);
    Create(L);
    cout << "判断表是否为空:" ;
    if (IsEmpty(L))
        cout << "空" << endl;
    else
        cout << "非空" << endl;
    cout << "遍历此时的顺序表:" ;
    Print(L);

    cout << "**********插入元素**********" << endl;
    int i;
    cout << "输入你要插入的位置:";
    cin >> i;
    ElemType InsertElem;
    cout << "输入你要插入的元素数值:";
    cin >> InsertElem;
    Insert(L, i, InsertElem);
    cout << "遍历此时的顺序表:";
    Print(L);

    cout<< "**********按数值查找元素位置**********" << endl;
    ElemType FindElem;
    cout << "输入你要查找的元素数值:";
    cin >> FindElem;
    cout << "元素位置为" << Search(L, FindElem) << endl;
    cout << "遍历此时的顺序表:";
    Print(L);

    cout << "**********按位置删除元素**********" << endl;
    int j;
    cout << "输入你要删除元素的位置:";
    cin >> j;
    Delete(L, j);
    cout << "遍历此时的顺序表:";
    Print(L);

    cout << "**********按数值删除元素**********" << endl;
    int DeElem;
    cout << "输入你要删除元素的数值:";
    cin >> DeElem;
    Delete2(L, DeElem);
    cout << "删除后,遍历此时的顺序表:";
    Print(L);

    cout << "**********清空顺序表**********" << endl;
    Clear(L);

    cout << "**********销毁顺序表**********" << endl;
    Destroy(L);

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读