1.创建一个节点,并将链表的首节点返回
创建一个独立节点,没有和原链表产生任何关系
#include "head.h"typedef struct Node
{
int num;
struct Node*pNext;
struct Node*pPer;
}NODE;
后续代码:
NODE*createNode(int value)
{NODE*new node=(NODE*)malloc(sizeof(NODE));if(NULL==new node){perror("createNode malloc fail");return NULL;}new node->num=value;new node->pNext=NULL;new node->pPer=NULL;return new node;
}
//头插法
NODE*instetHead(NODE*head,int value)
{NODE*new node=createNode(value);if(NULL==new code){//新节点创建失败return head;}if(head==NULL){//原链表为空return new node;}new node->pNext=head;head->pPer=new node;return new node;
}
//获取链表的尾节点指针
NODE*getListTail(NODE*head)
{if(head==NULL){return NULL;}while(head->pNext!=NULL){head=head->pNext;}return head;
}NODE*currentPosNOde(NODE*head,int pos)
{if(head==NULL){return NULL;}for(int i=0;i<pos-1;i++){head=head->pNext;}return head;
}
//pos=0时为头插法
//当前函数不处理头插和尾插的情况
NODE*insertMid(NODE*head,int value,int pos)
{NODE*new node=createNode(value);if(NULL==new node){//新节点创建失败return head;}if(head==NULL){//原链表为空return new node;}
//获取pos位置的节点指针
NODE*cur=currentPosNode(head,pos);
//将新节点与其插入位置之后的节点进行连接
new node->pNext=cur->pNext;
cur->pNext->pPer=cur;return head;