前言
本篇文章主要接着上文的c++实现单向链表进一步实现单向链表的插入、删除。
单向链表插入节点
在单向链表中插入新节点犹如给一列火车加入新的车厢,而根据新节点插入位置的不同,分为三种不同的插入处理方式具体分析如下:
1.当在链表头部插入节点,只需把新节点的指针指向链表原来的第一个节点,再把链表头指针指向新节点即可,
用代码描述:
insertnode->next = head;
head = insertnode;
2.当在链表中间位置插入新节点,例如在A和B之间,则只需将A节点的指针指向新节点,新节点的指针指向B,
用代码描述:
A->next = insertnode;
insertnode->next = A->next;
3.当在链表尾插入新节点时,只需把链表最后一个节点的指针指向新节点,新节点的指针指向NULL即可,ptr为读取游标
用代码描述:
ptr->next = insertnode;
insertnode->next = NULL;
实现代码
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;
}
单向链表删除节点
大体和节点的插入同样思想,这里简说,分为三种情况分别进行操作:
1.当删除链表头的节点时,把头指针指向第二个节点即可,并释放第一个节点内存空间。
2.当删除中间节点时,将要删除节点的前一个节点指针指向要被删除节点的后一个节点,并释放内存空间。
3.当删除尾结点时,把指向最后一个节点的指针直接指向NULL,并释放内存空间即可。
实现代码
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;
}
整个项目的完整代码
#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 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);
return 0;
}
对上面代码中出现的函数做简单讲解:
FindNode()方法通过头结点和要查询的数据,遍历链表并返回预查询数据的节点;
CreateList()通过用户输入的数组创建一个链表,具体创建方式请看我前一篇文章c++实现单向链表;
InsertNode()阅读前文;
DeleteNode()阅读前文;
PrintList()通过传入的链表头指针遍历整个链表数据并输出。
运行截图
总结
本文完成的代码只作为单向链表插入、删除的简单实例,分别插入、删除了一次,读者也可自行添加判断语句通过输入来控制是插入还是删除,且本文代码所设前提为链表内数据唯一,即没有两个相同的数据,如果考虑数据不唯一,则增加了难度,不便于初学者理解单向链表的插入、删除节点。
作者水平有限,正在学习当中,大家发现有什么错误或者建议尽可提出,也让我学学呀!QWQ!
大家有问题评论讨论。