前言
本篇文章接着前文单链表的插入、删除(c++实现)实现链表的反转,主要也即是在前文的基础上完成了一个InvertList()函数。
单向链表的反转
通过前面两篇文章的学习,已经对于链表的操作有一定掌握,而反转的实现就是一点小技巧,需要用到三个指针变量,类似于两个数交换的思想,层次递进。
现在假设定义pre、phead、temp三个指针变量,用phead指向链表的头结点,而pre代表phead的前一个节点。具体实现代码如下:
实现代码
link InvertList(link head){
link pre,phead,temp;
phead = head; //将phead指向链表头,做游标使用
pre = NULL; //pre为头指针之前的节点
while(phead != NULL){
temp = pre;
pre = phead;
phead = phead->next;
pre->next = temp; //pre接到之前的节点
}
return pre;
}
以下用两个图加深理解:
1.执行while循环以前,链表和各个指针变量的情况:
2.第一次执行while循环,链表和各个指针变量的情况:
然后依次往下,最后实现链表反转。
总结
本文要结合上一篇文章一起看,文章开头有链接,把函数放进去再调用就行。
有不懂得评论区见(手动狗头)。
更新
正如上面总结所说,直接把反转函数复制到前一篇博客代码里面,便能实现一个完整的链表操作(创建、遍历、插入、删除、反转),运行结果如下:
如果怕麻烦,我就再给大家贴一下完整代码:
#include<iostream>
#include<cstdlib>
using namespace std;
class list{
public:
int data;
class list *next;
};
typedef class list node;
typedef node *link;
link FindNode(link head,int position_data){
link phead;
phead = head;
while(phead != NULL){
if(phead->data == position_data)return phead;
phead = phead->next;
}
return phead;
}
link InsertNode(link head,int position_data,int data){
link phead = new node;
phead = FindNode(head,position_data);
link insertnode = new node;
if(!insertnode) return NULL;
insertnode->data = data;
insertnode->next = NULL;
if(phead == NULL){ //插入第一个节点
insertnode->next = head;
return insertnode;
}
else if(phead->next == NULL) phead->next = insertnode; //插入最后一个节点
else{ //插入中间节点
insertnode->next = phead->next;
phead->next = insertnode;
}
return head;
}
link DeleteNode(link head,int position_data){
link top = head; //保留头指针
link phead = FindNode(head,position_data);
if(head == phead){ //删除头结点
head = head->next;
delete phead;
}
else{
while(top->next != phead) top = top->next;
if(phead->next == NULL){ //删除尾结点
top->next = NULL;
delete phead;
}
else{
top->next = phead->next;
delete phead;
}
}
return head;
}
link InvertList(link head){
link pre,phead,temp;
phead = head; //将phead指向链表头,做游标使用
pre = NULL; //pre为头指针之前的节点
while(phead != NULL){
temp = pre;
pre = phead;
phead = phead->next;
pre->next = temp; //pre接到之前的节点
}
return pre;
}
link CreateList(int a[],int n){
link head,phead,newnode;
phead = new node;
if(!phead) return NULL;
phead->data = a[0];
head = phead;
for(int i = 1;i<n;i++){
newnode = new node;
newnode->data = a[i];
newnode->next = NULL;
phead->next = newnode;
phead = phead->next;
}
return head;
}
void PrintList(link head){
link phead = new node;
phead = head;
cout<<"链表元素如下: "<<endl;
while(phead!=NULL){
cout<<phead->data<<"->";
head = phead;
phead = phead->next; //phead按序往后遍历整个链表
if(!phead) cout<<"NULL"<<endl;
}
}
int main(){
int position_data,data;
link head,phead;
int n;
cout<<"请输入初始链表元素个数: "<<endl;
cin>>n;
int a[n];
cout<<"请依次输入链表元素: ";
for(int i = 0;i<n;i++) cin>>a[i];
head = CreateList(a,n);
PrintList(head);
cout<<"请输入预插入位置之前的元素和要插入的元素(例:5 8): ";
cin>>position_data>>data;
head = InsertNode(head,position_data,data);
cout<<"插入之后的";
PrintList(head);
cout<<"请输入想删除的链表元素: ";
cin>>position_data;
head = DeleteNode(head,position_data);
cout<<"删除之后的";
PrintList(head);
cout<<"反转之后的";
head = InvertList(head);
PrintList(head);
return 0;
}