双向链表实现.cpp
include
using namespace std;
class Node {
public:
int data;
Node prev;
Node next;
};
class DoublyLinkedList {
private:
Node head;
public:
DoublyLinkedList() {
head = nullptr;
}
void insert(int value) {
Node newNode = new Node;
newNode->data = value;
newNode->prev = nullptr;
newNode->next = head;
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
}
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
DoublyLinkedList dll;
dll.insert(1);
dll.insert(2);
dll.insert(3);
dll.display();
return 0;
}
下载地址
用户评论