链表的基本操作

/*
C++学习笔记02——链表的基本操作
2017.03.25
*/

#define _CRT_SECURE_NO_WARNINGS
#include "stdlib.h"
#include "stdio.h"
#include "string.h"

typedef struct Node
{
    int data;
    struct Node *next;
}SLIST;

SLIST *Creat_SList();
int SList_Print(SLIST *pHead);
//在结点数值为x的前面插入y
int SList_NodeInsert(SLIST *pHead, int x, int y);
//删除结点为y的链表结点
int SList_NodeDel(SLIST *pHead, int y);
int SList_Destory(SLIST *pHead);
int SList_revse(SLIST *pHead);

SLIST *Creat_SList()
{
    //1 创建头结点并初始化 
    int data = 0;
    SLIST *pHead = NULL, *pM = NULL, *pCur;
    pHead = (SLIST *)malloc(sizeof(SLIST));
    if (pHead == NULL)
    {
        return NULL;
    }
    pHead->data = 0;
    pHead->next = NULL;

    //2循环创建结点,结点数据域中的数值从键盘输入,
    //以-1作为输入结束标志
    printf("\nPlease enter the data of node(-1:quit) ");
    scanf("%d", &data);

    //准备环境 让pCur指向pHead
    pCur = pHead;
    while (data != -1)
    {
        //不断的malloc新节点 并且数据域 赋值
        pM = (SLIST *)malloc(sizeof(SLIST));
        if (pM == NULL)
        {
            SList_Destory(pHead);
            return NULL;
        }
        pM->data = data;
        pM->next = NULL;

        //1新节点入链表
        pCur->next = pM;

        //2 当前结点下移(新结点变成当前结点)
        pCur = pM; // (pCur = pCur->next)

        printf("\nPlease enter the data of node(-1:quit) ");
        scanf("%d", &data);
    }
    return pHead;
}


int Creat_SList2(SLIST **Head)
{
    int ret = 0;
    //1 创建头结点并初始化 
    int data = 0;
    SLIST *pHead = NULL, *pM = NULL, *pCur = NULL;
    pHead = (SLIST *)malloc(sizeof(SLIST));
    if (pHead == NULL)
    {
        ret = -1;
        printf("func Creat_SList() err:%d", ret);
        return ret;
    }
    pHead->data = 0;
    pHead->next = NULL;

    //2循环创建结点,结点数据域中的数值从键盘输入,
    //以-1作为输入结束标志
    printf("\nPlease enter the data of node(-1:quit) ");
    scanf("%d", &data);

    //准备环境 让pCur指向pHead
    pCur = pHead;
    while (data != -1)
    {
        //不断的malloc新节点 并且数据域 赋值
        pM = (SLIST *)malloc(sizeof(SLIST));
        if (pM == NULL)
        {
            SList_Destory(pHead);
            ret = -2;
            printf("func Creat_SList() err:%d malloc err", ret);
            return ret;
        }
        pM->data = data;
        pM->next = NULL;

        //1新节点入链表
        pCur->next = pM;

        //2 当前结点下移(新结点变成当前结点)
        pCur = pM; // (pCur = pCur->next)

        printf("\nPlease enter the data of node(-1:quit) ");
        scanf("%d", &data);
    }
    *Head = pHead;
    return ret;
}

int SList_Print(SLIST *pHead)
{
    SLIST *p = NULL;

    if (pHead == NULL)
    {
        return -1;
    }
    //准备环境
    p = pHead->next;
    printf("\nBegin ");
    while (p)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("End ");
    return 0;
}

//在结点数值为x的前面插入y
int SList_NodeInsert(SLIST *pHead, int x, int y)
{
    SLIST *pCur = NULL, *pPre = NULL, *pM = NULL;
    if (pHead == NULL)
    {
        return -1;
    }
    //环境准备
    pPre = pHead;
    pCur = pHead->next;

    //不断的malloc新节点 并且数据域 赋值
    pM = (SLIST *)malloc(sizeof(SLIST));
    pM->data = y;
    pM->next = NULL;

    while (pCur)
    {
        if (pCur->data == x)
        {
            break;
        }
        //让pPre下移
        pPre = pCur;
        //让当前节点下移
        pCur = pCur->next;
    }

    //让新节点链接后继结点
    pM->next = pPre->next; //(pCur)
    //让前驱结点连接pM
    pPre->next = pM;

    return 0;
}

//删除结点为y的链表结点
int SList_NodeDel(SLIST *pHead, int y)
{
    SLIST *pCur = NULL, *pPre = NULL;
    if (pHead == NULL)
    {
        return -1;
    }
    //环境准备
    pPre = pHead;
    pCur = pHead->next;

    while (pCur)
    {
        if (pCur->data == y)
        {
            break;
        }
        //让pPre下移
        pPre = pCur;
        //让当前节点下移
        pCur = pCur->next;
    }
    if (pCur == NULL)
    {
        printf("没有找到节点 y:%d", y);
        return -2;
    }
    //执行操作
    pPre->next = pCur->next;
    free(pCur);
    return 0;
}


int SList_Destory(SLIST *pHead)
{
    //1、删除当前结点前,需要把后继结点位置缓存

    SLIST *pTmp = NULL;
    SLIST *pCur = pHead;
    if (pHead == NULL)
    {
        return -1;
    }

    while (pCur)
    {
        //缓存后继结点位置
        pTmp = pCur->next;
        //删除当前结点
        free(pCur);
        //当前结点下移
        pCur = pTmp;
    }
    return 0;
}

int SList_revse(SLIST *pHead)
{

    SLIST *p = NULL, *q = NULL, *t = NULL;

    if (pHead == NULL)
    {
        return -1;
    }
    if (pHead->next == NULL || pHead->next->next == NULL)
    {
        return 0;
    }
    //准备环境
    p = pHead->next;
    q = pHead->next->next;

    while (q != NULL)
    {
        //1、在逆置之前需要做一个缓存
        t = q->next;

        //2、开始逆置
        q->next = p;

        //3、让p后移
        p = q;
        //4、q后移
        q = t;
    }

    //让第一个业务结点赋值null
    pHead->next->next = NULL;
    //让链表头指向最后一个节点
    pHead->next = p;
    return 0;
}


void main()
{
    int ret = 0;
    SLIST *pHead = NULL;
    //创建 并打印 
    pHead = Creat_SList();
    ret = SList_Print(pHead);

    //插入操作
    ret = SList_NodeInsert(pHead, 20, 19);
    ret = SList_Print(pHead);

    ret = SList_NodeDel(pHead, 19);
    ret = SList_Print(pHead);

    ret = SList_revse(pHead);
    ret = SList_Print(pHead);

    ret = SList_Destory(pHead);
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值