首页 > 其他分享 >单链表逆序

单链表逆序

时间:2024-05-06 21:23:36浏览次数:14  
标签:Head 单链 return next criSeL NULL data 逆序

逆序原理:保留头节点下一个结点地址,将头节点断开,遍历除头节点以外的节点,将那些节点头插入头节点中。就能实习逆序。

/*******************************************************************
*
*	file name:	demo2.c
*	author	 :  lzj
*	date	 :  2024/04/23
*	function :  单向链表倒序
* 	note	 :  None
*
*	CopyRight (c)  2023-2024   [email protected]   All Right Reseverd 
*
* *****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef int DataType;
//定义结构体
typedef struct cricSeqlist
{
    DataType data;
    struct cricSeqlist *next;
}criSeL;
//逆序

void nixu2(criSeL *Head)
{
    criSeL *p=Head->next;
    while (p!=NULL)
    {
        criSeL *tem=p->next;
        p->next=Head->next;
        Head->next=p;
        p=tem;
    }
    
}
//创建头节点
criSeL * InitcriSwl()
{
    criSeL *Head=(criSeL *)calloc(1,sizeof(criSeL));
    if (Head==NULL)
    {
        perror("Head Node creat fail");
        exit(-1);
    }
    Head->next=NULL;
    return Head;
}
//创建新节点
criSeL * CreatNode(DataType data)
{
    criSeL *NewNode=(criSeL *)calloc(1,sizeof(criSeL));
    if (NewNode==NULL)
    {
        perror("Head Node creat fail");
        exit(-1);
    }
    NewNode->data=data;
    NewNode->next=NULL;
    return NewNode;
}
//头插入节点
bool InsertNode(criSeL *Head,DataType data)
{
    criSeL *node=CreatNode(data);
    if (Head==NULL)
    {
        return false;
    }
    if (Head->next==NULL)
    {
        Head->next=node;
        node->next=NULL;
        return true;
    }
    node->next=Head->next;
    Head->next=node;
    return true;
}
//头删除
bool HeadRemove(criSeL *Head)
{
    if (Head->next==NULL)
    {
        return false;
    }
    criSeL *lasNode=Head->next;
    Head->next=Head->next->next;
    lasNode->next=NULL;
    free(lasNode);
    return true;
}
//遍历
void prin(criSeL *Head)
{
    if (Head->next==NULL)
    {
        return;
    }
    criSeL *pHead=Head;
    while (pHead->next!=NULL)
    {
        pHead=pHead->next;
        printf("%d\n",pHead->data);
    }
}
//单链表逆序
void nixu(criSeL *Head)
{
    if (Head->next==NULL)return;
    criSeL *p=Head->next;
    Head->next=NULL;
    while(p!=NULL)
    {
        if (Head->next==NULL)
        {
            Head->next=p;
            p=p->next;
            Head->next->next=NULL;
            continue;
        }
        criSeL *tem=p->next;
        p->next=Head->next;
        Head->next=p;
        p=tem;
    }
}
int main()
{
    criSeL *p=InitcriSwl();
    InsertNode(p,7);
    InsertNode(p,8);
    InsertNode(p,9);
    nixu(p);
    prin(p);
    return 0;
}

标签:Head,单链,return,next,criSeL,NULL,data,逆序
From: https://www.cnblogs.com/lzj-ZJ/p/18175962

相关文章

  • 无符号整数转二进制字符串逆序输出
    无符号整数转二进制逆序在C语言中,要实现一个函数来传入一个八位无符号整数,并返回其二进制倒序的字符串,你可以使用以下步骤:分配足够的堆空间来存储倒序后的二进制字符串。利用位运算符获取当成8位无符号整数的二进制数可以从高位往遍历也可以从低位往高位遍历从高遍......
  • 链表逆序
    数据结构链表逆序笔试题:编写一个函数,实现单链表逆序代码//方法一:将尾结点循环插到头节点后面,实现逆序voidreverse_list(single_list*head){single_list*p=head->next;//将链表除头节点的节点保存head->next=NULL;//将链表断开single_list*tmp=......
  • 链表逆序
    编写一个函数,实现单链表逆序,,函数原型如下:*voidreverse_list(single_listhead)程序代码如下:voidreverse_list(single_list*head){single_list*p=head->next;//将链表除头节点的节点保存head->next=NULL;//将链表断开single_list*tmp=NULL;while(......
  • 自定义单链表队列的基本接口函数(非循环队列)
    单链表构建队列的接口函数/********************************************************************文件名称: 单链表构建队列的接口函数文件作者:[email protected]创建日期:2024/04/26文件功能:对单链表循环队列的增删改查功能的定义注意事项:NoneCop......
  • 单链表根据尾插法建立
    include<stdio.h>include<stdlib.h>typedefstructslik{intdata;structslik*next;}sli;voidcreatesli(sli**head,inta[],intsize){for(inti=0;i<size;i++){sli*s=(sli*)malloc(sizeof(sli));s->data=a[i];s->next=NULL......
  • 单链表队列
    单链表队列队列:遵循先进先出1.创建初始化队列/******************************************************************************函数名称:LinQue_Create*函数功能:创建头结点*函数参数:NONE*......
  • 单链表的头插法的实现
    /***@filename: 文件名称*@brief单链表的头插法的实现*@[email protected]*@date2024/04/26*@version1.0:版本*@property:属性介绍*@note补充注意说明*CopyRight(c)[email protected]*/#......
  • 数据结构—单链表队列头删尾插
    单链表队列的头删尾插/*************************************************/***@filename: 单链表队列的头删尾插.md*@brief实现对单链表队列的头删尾插*@[email protected]*@date2024/04/26*@version1.0:在下坂本,有何贵干*@property:no......
  • 单链表的头插、尾插、中间插、头删、尾删、中间删
    //指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改typedefintDataType_t;//构造链表的结点,链表中所有结点的数据类型应该是相同的typedefstructLinkedList{ DataType_t data;//结点的数据域 structLinkedList *next;//结点的指针域}LList_t;......
  • cdq分治/逆序对 一点点总结
    cdq分治/逆序对一点点总结归并排序求普通逆序对问题#include<bits/stdc++.h>#defineINinline#defineRregisterintusingnamespacestd;constintN=5e5+5;typedeflonglongll;INintread(){intf=1;charch;while((ch=getchar())<'0'||ch>&#......