C++实现数据结构的顺序表详解

时间:2022-06-29 22:40:39

前言:

hello,大家好,这篇文章博主来分享一下C++实现数据结构中的顺序表的代码。希望对大家有所帮助。

在博主之前的文章中,已经详细地写过顺序表,读者可以点击查看C语言如何建立链表并实现增删查改,在之前的文章中,是用C语言来实现的,这篇文章中,我们用C++来实现。

代码

1.SeqList.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef SEQLIST_H
#define SEQLIST_H
#include<iostream>
using namespace std;
template<class T,int MAXSIZE>
class SeqList
{
    T data[MAXSIZE];
    int length;
public:
    SeqList();
    SeqList(T a[],int n);
    ~SeqList();
    int ListLength();
    T Get(int pos);
    int Locate(T item);
    void SeqPrint();
    void Insert(int i, T item);
    T Delete(int i);
};
#endif

2.SeqList.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#define _CRT_SECURE_NO_WARNINGS   1
#include"SeqList.h"
template<class T, int MAXSIZE>
SeqList<T,MAXSIZE>::SeqList()
{
    length = 0;
}
template<class T, int MAXSIZE>
SeqList<T, MAXSIZE>::SeqList(T a[], int n)
{
    if (n < MAXSIZE)
    {
        length = n;
        for (int i = 0; i < n; i++)
        {
            data[i] = a[i];
        }
    }
    else
    {
        cerr << "您的数据已经超过范围,系统无法继续工作" << endl;
        exit(-1);
    }
}
template<class T, int MAXSIZE>
SeqList<T, MAXSIZE>::~SeqList()
{
}
template<class T, int MAXSIZE>
int SeqList<T, MAXSIZE>::ListLength()
{
    return length;
}
template<class T, int MAXSIZE>
T SeqList<T, MAXSIZE>::Get(int pos)
{
    if (pos > length || pos < 0)
    {
        cerr << "您要查找的位置不存在,系统无法继续为您服务" << endl;
        exit(-1);
    }
    else
    {
        return data[pos - 1];
    }
}
template<class T, int MAXSIZE>
int SeqList<T, MAXSIZE>::Locate(T item)
{
    for (int i = 0; i < length; i++)
    {
        if (data[i] == item)
            return i + 1;
    }
    return -1;
}
template<class T, int MAXSIZE>
void SeqList<T, MAXSIZE>::SeqPrint()
{
    for (int i = 0; i < length; i++)
    {
        cout << data[i] << "  ";
    }
    cout << endl;
}
template<class T, int MAXSIZE>
void SeqList<T, MAXSIZE>::Insert(int i, T item)
{
    if (length < MAXSIZE)
    {
        for (int j = length - 1; j>=i - 1; j--)
        {
            data[j + 1] = data[j];
        }
        data[i - 1] = item;
        length++;
    }
    else
    {
        cerr << "抱歉,当前已经达到系统最大的储存,无法为您插入" << endl;
        exit(-1);
    }
}
template<class T, int MAXSIZE>
T SeqList<T, MAXSIZE>::Delete(int i)
{
    if (length == 0)
    {
        cerr << "当前无可删除元素" << endl;
        exit(-1);
    }
    if (i<1 || i>length)
    {
        cerr << "该位置非法" << endl;
        exit(-1);
    }
    T x = data[i - 1];
    for (int j = i; j < length; j++)
    {
        data[j - 1] = data[j];
    }
    length--;
    return x;
}

3.test.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define _CRT_SECURE_NO_WARNINGS   1
#include"SeqList.cpp"
#include<iostream>
using namespace std;
void menu()
{
    cout << "|------------------------------------|" << endl;
    cout << "|----------- 欢迎来到顺序表 ---------|" << endl;
    cout << "|---------------1.插入---------------|" << endl;
    cout << "|---------------2.删除---------------|" << endl;
    cout << "|---------------3.求长---------------|" << endl;
    cout << "|---------------4.取值---------------|" << endl;
    cout << "|---------------5.定位---------------|" << endl;
    cout << "|---------------6.打印---------------|" << endl;
    cout << "|---------------0.退出---------------|" << endl;
    cout << "|------------------------------------|" << endl;
}
int main()
{
    int *a;
    int n;
    cout << "请输入您要构造的顺序表的长度" << endl;
    cin >> n;
    a = new int[n];
    cout << "请输入该顺序表中的每一个元素" << endl;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    SeqList<int, 20>seq(a, n);
    cout << "现在开始我们的程序之旅" << endl;
    int input=0;
    do
    {
        menu();
        cout << "输入您要进行的操作的编号" << endl;
        cin >> input;
        switch (input)
        {
        case 1:
            cout << "请输入您要插入的位置和数值" << endl;
            int pos;
            int value;
            cin >> pos;
            cin >> value;
            seq.Insert(pos,value);
            break;
        case 2:
            cout << "请输入您要删除的位置" << endl;
            int pos1;
            cin >> pos1;
            cout << "您删除的元素的值为:";
            cout << seq.Delete(pos1) << endl;
            break;
        case 3:
            cout << "您的顺序表当前的长度为:" << seq.ListLength() << endl;
            break;
        case 4:
            cout << "请输入您要查找的位置" << endl;
            int pos2;
            cin >> pos2;
            cout << "您查找的元素的值为:";
            cout << seq.Get(pos2) << endl;;
            break;
        case 5:
            cout << "请输入您要查找的元素" << endl;
            int item;
            cin >> item;
            cout << "您查找的元素的位置为:";
            cout << seq.Locate(item) << endl;;
            break;
        case 6:
            cout << "当前顺序表如下:" << endl;
            seq.SeqPrint();
            break;
        case 0:
            cout << "程序退出,感谢使用" << endl;
            exit(-1);
            break;
        default :
            cout << "您的输入有误,请重新选择" << endl;
        }
    } while (input);
    return 0;
}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/weixin_53472920/article/details/121341808