总结一下链表相关的操作
- 单链表节点的定义
template<typename T>
class Node{
public:
T data;
Node* next;
}
- 实现单向链表的反向
template<typename T>
Node<T>* reverse(Node<T>* head){
Node<T>* p = head;
Node<T>* q = p;
p = p->next;
q->next = NULL;
head = q;
while(p != NULL){
q = p;
p = p->next;
q->next = head;
head = q;
}
return head;
}
- 删除单链表的所有节点
template<typeneme T>
void delete(Node<T>* head){
Node<T>* p = head;
Node<T>* q;
while(p != NULL){
q = p;
p = p->next;
delete q;
}
}
网友评论