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

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;
相关推荐
tachibana27 小时前
hot100 排序链表(148)
java·数据结构·算法·leetcode·链表
Yang_jie_039 小时前
笔记:数据结构(链表)
数据结构·笔记·链表
Funing71 天前
FreeRTOS学习day1:Keil 工程配置与 FreeRTOS 链表机制理解
数据结构·学习·链表
Tairitsu_H2 天前
[LC优选算法#17] 链表 | 合并 K 个升序链表 | K个⼀组翻转链表
数据结构·算法·链表
东华万里2 天前
第32篇 数据结构入门 单链表的增删查改实现
数据结构·链表
文祐2 天前
C语言用双向链表实现单调递减(递增)队列
c语言·开发语言·链表
剑挑星河月2 天前
234. 回文链表
java·数据结构·算法·leetcode·链表
六bring个六2 天前
链表学习(常规链表)
数据结构·学习·链表
tachibana23 天前
hot100 回文链表(234)
java·网络·数据结构·leetcode·链表
青山木3 天前
Hot 100 --- LRU 缓存
java·数据结构·算法·leetcode·链表·缓存·哈希