双向链表,对其实现头插入,尾插入以及遍历倒序输出

1.创建一个节点,并将链表的首节点返回

创建一个独立节点,没有和原链表产生任何关系

cs 复制代码
#include "head.h"

typedef struct Node
{
int num;
struct Node*pNext;
struct Node*pPer;
}NODE;

后续代码:

cs 复制代码
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;
}

//头插法

cs 复制代码
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;
}

//获取链表的尾节点指针

cs 复制代码
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时为头插法

//当前函数不处理头插和尾插的情况

cs 复制代码
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位置的节点指针

cs 复制代码
NODE*cur=currentPosNode(head,pos);

//将新节点与其插入位置之后的节点进行连接

cs 复制代码
new node->pNext=cur->pNext;
cur->pNext->pPer=cur;

    return head;
相关推荐
tgethe1 小时前
Java 链表(LinkedList)
java·开发语言·链表
Bdygsl5 小时前
数据结构 —— 顺序表
数据结构·链表
2301_789015626 小时前
每日精讲:环形链表、两个数组中的交集、随机链表的复制
c语言·数据结构·c++·算法·leetcode·链表·排序算法
SadSunset20 小时前
力扣题目142. 环形链表 II的解法分享,附图解
算法·leetcode·链表
25Qi导航1 天前
专业期刊发表公司
链表
Dylan的码园1 天前
队列与queue
java·数据结构·链表
阿昭L2 天前
leetcode链表相交
算法·leetcode·链表
阿昭L2 天前
leetcode链表是否有环
算法·leetcode·链表
yaoh.wang2 天前
力扣(LeetCode) 83: 删除排序链表中的重复元素 - 解法思路
程序人生·算法·leetcode·链表·面试·职场和发展
阿昭L2 天前
leetcode旋转链表
算法·leetcode·链表