每天打卡一小时 第二十六天

接昨天的题

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
#include <iostream>
using namespace std;
 
template <class T>
class Node
{
private:
    Node<T>* next;
 
public:
    T data;
 
    Node(const T& data, Node<T>* next = 0);
    Node(const Node<T>& p);
    ~Node();
    T getData();
};
 
template <class T>
Node<T>::Node(const T& data, Node<T>* next) : data(data), next(next)
{
    cout << "Node Constructor run" << endl;
}
 
template <class T>
Node<T>::Node(const Node<T>& p) : data(p.data), next(p.next)
{
    cout << "Node CopyConstructor run" << endl;
}
 
template <class T>
Node<T>::~Node()
{
    cout << "Node Destructor run" << endl;
}
 
template <class T>
T Node<T>::getData()
{
    return data;
}
 
template <class T>
class LinkList
{
private:
    Node<T>* headNode;
    Node<T>* position;
 
public:
    LinkList() : headNode(new Node<T>()), position(headNode)
    {
        cout << "LinkList Constructor run" << endl;
    }
 
    LinkList(const T arr[], int n) : headNode(new Node<T>()), position(headNode)
    {
        cout << "LinkList Constructor run" << endl;
        for (int i = n - 1; i >= 0; --i) {
            position->next = new Node<T>(arr[i], position->next);
        }
    }
 
    LinkList(const LinkList<T>& other) : headNode(other.headNode), position(other.position)
    {
        cout << "LinkList CopyConstructor run" << endl;
    }
 
    ~LinkList()
    {
        cout << "LinkList Destructor run" << endl;
        while (headNode->next) {
            Node<T>* p = headNode->next;
            headNode->next = p->next;
            delete p;
        }
        delete headNode;
    }
 
    bool searchNode(const T& value)
    {
        Node<T>* p = headNode->next;
        while (p) {
            if (p->getData() == value) {
                position = p;
                return true;
            }
            p = p->next;
        }
        return false;
    }
 
    int getSize()
    {
        int cnt = 0;
        Node<T>* p = headNode->next;
        while (p) {
            cnt++;
            p = p->next;
        }
        return cnt;
    }
 
    void next()
    {
        if (position->next) {
            position = position->next;
        }
    }
 
    Node<T>* currNode() const
    {
        return position;
    }
 
    void delNode()
    {
        if (!position)
            return;
        if (position == headNode) {
            headNode = headNode->next;
            delete position;
            position = headNode;
        } else {
            Node<T>* temp = headNode;
            while (temp->next != position) {
                temp = temp->next;
            }
            temp->next = position->next;
            delete position;
            position = temp->next;
        }
    }
 
    void insertNode(Node<T>* n)
    {
        const Node<T>* tmp = n;
        tmp->next = position->next;
        position->next = new Node<T>(*tmp);
        position = position->next;
    }
 
    void show() const
    {
        Node<T>* temp = headNode->next;
        while (temp) {
            std::cout << temp->data << " ";
            temp = temp->next;
        }
        std::cout << std::endl;
    }
};
 
int main()
{
    int i,a[5]= {0,1,2,3,4};
    for(i=0;i<5;i++)
        scanf("%d",&a[i]);
    LinkList<int> l1(a,5),l2(l1);
    cout<<l2.getSize()<<endl;
    l1.show();
    if (l2.searchNode(2))
        cout << "Found: " << l2.currNode()->getData() << endl;
    else
        cout<<"Not Found"<<endl;
    l2.delNode();
    Node <int> *p1=new Node<int>(11);
    l2.insertNode(*p1);
    l2.show();
    return 0;
}

  报错很多,修改很长时间,并没有解决问题

posted @   财神给你送元宝  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示