C++顺序表实现

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<iostream>
using namespace std;
 
template <class T>
class LinearList
{
private:
    T* element;//存储顺序表的数组
    int MaxSize;//顺序表的最大长度
    int length;//顺序表的实际长度
public:
    //构造函数,若申明顺序表实例时未给出表的最大长度,则最大长度为10
    LinearList(int);
    //析构函数
    ~LinearList(void)
    {
        delete[] element;
    }
    //判空
    bool ListEmpty(void) const
    {
        return length==0;
    }
    //判满
    bool ListFull(void) const
    {
        return length==MaxSize;
    }
    //返回表的长度
    int ListLength(void) const
    {
        return length;
    }
    //存取,将下标为k的节点字段赋值给item
    bool Find(int,T&) const;
    //查找,在表中查找字段值为item的结点并返回下标
    int Search(const T&) const;
    //删除,删除小标为k的节点后并将其字段值赋值给item
    bool Delete(int,T&);
    //插入,在下标为k的节点后插入字段为item的结点
    bool Insert(int,const T&);
    //打印,将顺序表内容按顺序打印到屏幕
    void Print(void);
};
//顺序表的构造函数
template <class T>
LinearList<T>::LinearList(int MaxListSize=10)
{
    MaxSize=MaxListSize;
    element=new T[MaxSize];
    length=0;
};
//存取:将下表为k的结点的字段值赋值给item并返回true,若不存在返回false
template <class T>
bool LinearList<T>::Find(int k,T& item) const
{
    if(k<0||k>length-1||length==0)
    {
        cout<<"unreasonable position or empty list!"<<endl;
        return false;
    }
    else
    {
        item=element[k];
        return true;
    }
}
//查找:在表中查找字段为item的结点并返回其下标;若表中没有item,则返回-1
template <class T>
int LinearList<T>::Search(const T& item) const
{
    for(int i=0; i<length; ++i)
    {
        if(element[i]==item)
            return i;
    }
    return -1;
}
//删除:删除表中下标为k的结点,并将其值赋给item
template <class T>
bool LinearList<T>::Delete(int k,T& item)
{
    if(Find(k,item))//若找到下标为k的结点,则将其后面所有结点均向前移动一个位置
    {
        for(int i=k+1; i<length; ++i)
            element[i-1]=element[i];
        length--;//表长度相应减一
        return true;
    }
    else
        return false;
}
//插入,在下标为k的结点后插入item
template <class T>
bool LinearList<T>::Insert(int k,const T& item)
{
    if(ListFull())
    {
        cout<<"The List is Full!"<<endl;
        return false;
    }
    else if(k<0||k>length)
    {
        cout<<"The node does not exist!"<<endl;
        return false;
    }
    else
    {
        for(int i=length; i>k; --i)
        {
            element[i]=element[i-1];
        }
        element[k]=item;
        length++;
        return true;
    }
}
//打印
template <class T>
void LinearList<T>::Print()
{
    for(int i=0; i<length; ++i)
    {
        cout<<element[i]<<"->";
    }
    cout<<endl;
}
//实例演示
 
int main()
{
    LinearList<char> list(16);
    char ch='a';
    for(int i=0; i<10; ++i)
    {
        list.Insert(i,ch);
        ch++;
    }
    list.Print();
    list.Delete(0,ch);
    list.Print();
    list.Insert(list.ListLength(),ch);
    list.Print();
    return 0;
}

  

posted @   大大维  阅读(204)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
点击右上角即可分享
微信分享提示