在单结点链表的GET操作中Node <T>*p=front->next;起什么作用
时间: 2023-05-23 17:02:18 浏览: 61
在单结点链表的GET操作中,Node<T> *p = front->next; 的作用是将链表的头指针front向下移动一个结点,指向当前待获取结点的前一个结点,同时将p指向要获取的结点的指针。这样就可以使用p指针获取到要查找的结点。
相关问题
#include <iostream>template <class T>class Node {private: T data;public: Node* next; Node(); Node(const T& data, Node<T>* nt = 0);// Node(T data, Node<T>* n = NULL); T& getData();};template <class T>Node<T>::Node() { data = 0; next = NULL;}template <class T>Node<T>::Node(const T& d, Node<T>* nt) { data = d; next = nt;}template <class T>T& Node<T>::getData() { return data;}/********** Begin **********//** 在LinkedList的设计中,采用了附加指针front和rear,即链表的结构为front->a1->a2->...->rear* 只有在析构函数中才删除这两个指针,其余的时间这两个指针都是存在的,其中的数据始终为0,不存储用户数据*/template <class T>class LinkedList {};/********** End **********/using namespace std;int main(){ LinkedList<int> A, B; int i, item; // cout << "请输入加入链表A的五个整数:"; for (i = 0; i < 5; i ++) { cin >> item; A.insertRear(item); } // cout << "请输入加入链表B的五个整数:"; for (i = 0; i < 5; i ++) { cin >> item; B.insertRear(item); } // cout << endl << "有序链表A中的元素为:"; A.reset(); while(!A.endOfList()) { cout << A.data() << " "; A.next(); } cout << endl; // cout << endl << "有序链表B中的元素为:"; B.reset(); while(!B.endOfList()) { A.insertRear(B.data()); cout << B.data() << " "; B.next(); } cout << endl; // cout << endl << "加入链表B中元素后,链表A中的元素为:"; A.reset(); while(!A.endOfList()) { cout << A.data() << " "; A.next(); } cout << endl; return 0;}
这是一个 C++ 程序,主要实现了链表的基本操作,包括链表节点类 Node 和链表类 LinkedList。其中,链表节点类 Node 包含一个数据成员和一个指向下一个节点的指针,链表类 LinkedList 包含了两个附加指针 front 和 rear,用于指向链表的头和尾。这段程序的主要功能是,创建两个链表 A 和 B,将 B 中的元素插入到 A 的尾部,最后输出 A 的元素。
请问你还有什么问题吗?
C++链表类
好的,我可以为您提供一个简单的C++链表类的模板代码,您可以根据自己的需要进行修改和使用:
```
#include <iostream>
template <typename T>
class Node {
public:
T data;
Node<T>* next;
Node(const T& d = T(), Node<T>* n = nullptr) : data(d), next(n) {}
};
template <typename T>
class LinkedList {
private:
Node<T>* head;
int size;
public:
LinkedList() : head(nullptr), size(0) {}
~LinkedList() {
while (head != nullptr) {
Node<T>* temp = head;
head = head->next;
delete temp;
}
}
void push_front(const T& data) {
Node<T>* temp = new Node<T>(data, head);
head = temp;
++size;
}
void push_back(const T& data) {
Node<T>* temp = new Node<T>(data);
if (head == nullptr) {
head = temp;
} else {
Node<T>* curr = head;
while (curr->next != nullptr) {
curr = curr->next;
}
curr->next = temp;
}
++size;
}
void pop_front() {
if (head == nullptr) {
return;
}
Node<T>* temp = head;
head = head->next;
delete temp;
--size;
}
void pop_back() {
if (head == nullptr) {
return;
}
if (head->next == nullptr) {
delete head;
head = nullptr;
--size;
return;
}
Node<T>* curr = head;
while (curr->next->next != nullptr) {
curr = curr->next;
}
delete curr->next;
curr->next = nullptr;
--size;
}
int get_size() const {
return size;
}
void print_list() const {
Node<T>* curr = head;
while (curr != nullptr) {
std::cout << curr->data << " ";
curr = curr->next;
}
std::cout << std::endl;
}
};
```
这个链表类包括了常见的链表操作:插入头节点、插入尾节点、删除头节点、删除尾节点、获取链表大小和打印链表。您可以根据需要进行修改和添加其他操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)