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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*动态数组 使用链表实现*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
 
void menu();
template <class T>
class Link_Array
{
typedef struct node
{
    T data;
    struct node*next;
}Node;
 
public:
    Link_Array()
    {
        length = 0;
        head = NULL;
        temp = NULL;
    }
    void input()//数组中元素的输出
    {
        Node*pr = head;
        cout<<"当前数组中共有"<<length<<"个元素"<<endl;
        cout<<"数组中的元素有:";
        while(pr != NULL)
        {
            cout<<pr->data<<"->";
            pr = pr->next;
        }
        cout<<"NULL";
    }
    void add()//添加元素
    {
        T num;
        Node*pr = NULL;
        cout<<"请输入你要添加的元素:";
        cin>>num;
        pr = new Node;
        pr->data = num;
        if(head == NULL)head = pr;
        else temp->next = pr;
        temp = pr;
        temp->next = NULL;
        length++;
        cout<<"元素插入成功!!!"<<endl;
    }
    void del()//删除元素
    {
        int position,counter = 0;
        Node*pr = head,*p = NULL;
        cout<<"请输入元素的位置编号(1-"<<length<<"),以便于做删除操作.";
        cin>>position;
        if(position < 1 || position > length)cout<<"你输入的位置不存在!!!"<<endl;
        else
        {
           while(counter <= position-1)
           {
               p = pr;
               pr = pr->next;
               counter++;
           }
           p->next = pr->next;
           delete pr;
        }
        cout<<"元素删除成功!!!"<<endl;
    }
    void change()//按照位置修改元素
    {
        Node*pr = head;
        int position,num,counter = 0;
        cout<<"请输入元素的位置编号(1-"<<length<<"),以便于做修改操作.";
        cin>>position;
        if(position < 1 || position > length)cout<<"你输入的位置不存在!!!"<<endl;
        else
        {
            cout<<"请输入修改后的数字:";
            cin>>num;
            while(counter < position-1)
            {
                pr = pr->next;
                counter++;
            }
            pr->data = num;
            cout<<"元素修改成功!!!"<<endl;
        }
    }
 
    void insert()//插入元素
    {
        Node*pr = head,*p = NULL;
        int position,num,counter = 1;
        cout<<"请输入元素的位置编号(1-"<<length<<"),以便于做插入操作.";
        cin>>position;
        cout<<"请输入插入元素的元素值:";
        cin>>num;
        if(position < 1 || position > length)cout<<"你输入的位置不存在!!!"<<endl;
        else
        {
            p = new Node;
            p->data = num;
            while(counter < position-1)
            {
                pr = pr->next;
                counter++;
            }
            p->next = pr->next;
            pr->next = p;
            cout<<"元素插入成功!!!"<<endl;
        }
    }
    void main1(Link_Array arr)
    {
        int ch;
        menu();
        while(1)
        {
            cout<<"请输入你的选项";
            cin>>ch;
            if(ch==6)
            {
                exit(0);
            }
            else
            {
            switch (ch)
            {
                case 1:
                    arr.add();
                    break;
                case 2:
                    arr.del();
                    break;
                case 3:
                    arr.change();
                    break;
                case 4:
                    arr.insert();
                    break;
                case 5:
                    arr.input();
                    break;
            }
            }
        }
    }
private:
    int length = 0;
    struct node*head;
       struct node*temp;
};
 
void menu()
{
    cout<<"1.   添加元素   "<<endl;
    cout<<"2.   删除元素   "<<endl;
    cout<<"3.   修改元素   "<<endl;
    cout<<"4.   插入元素   "<<endl;
    cout<<"5.   输出元素   "<<endl;
    cout<<"6.   退出系统   "<<endl;
}
int main()
{
    int x;
    Link_Array<int> arr;
    Link_Array<char> arr2;
    Link_Array<double> arr3;
    Link_Array<string> arr4;
    printf("请你个用户选择链表元素的类型:\n1.整型  \n2.字符型 \n3.浮点型 \n4.字符串");
    scanf("%d",&x);
    switch(x)
    {
    case 1:
        arr.main1(arr);
        break;
    case 2:
        arr2.main1(arr2);
        break;
    case 3:
        arr3.main1(arr3);
        break;
    case 4:
        arr4.main1(arr4);
        break;
    }
    return 0;
}

 将原来的对动态数组的操作改为了对链表的操作,省去了数组空间的扩充,还有减掉了对数组运算符重载的使用,。

使用到了数据结构中的链表,C++中的类模板,大概就是这样吧。

主要的是当我将链表节点的结构体放入类class Link_Array的时候我的类模板T一下子就可以对链表节点中的data数据类型进行改写了,这实在是有点爽哦。

主要还是不知道具体老师让写的题目具体是什么,总之,在多态和虚函数中没有在代码中体现出来,这就有点恼火。

嗨呀,加油吧!!!

posted @   ppppppro  阅读(765)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示