单链表--基本操作 (增删改销毁、逆置)

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "malloc.h"
typedef struct DataList
{
    int x;
    struct DataList *next;
}DataList;
// 链表创建
int List_Create(DataList **p)
{

    DataList *pHead;
    DataList *temp;
    DataList *pCur;
    int x;
    if (p == NULL)
    {
        return -1;
    }
    // 创建头结点
    pHead = (DataList *)malloc(sizeof(DataList));
    if (pHead == NULL)
    {
        return -1;
    }
    pHead->x = 0;
    pHead->next = NULL;

    pCur = pHead;
    // 
    printf("请输入数据\n");
    scanf("%d", &x);

    while (x != -1)
    {
        //创建新节点
        temp = (DataList *)malloc(sizeof(DataList));
        temp->next = NULL;
        temp->x = x;

        //将新节点插入
        pCur->next = temp;
        pCur = pCur->next;

        printf("请输入数据\n");
        scanf("%d", &x);
    }
    *p = pHead;
    return 0;
}
// 链表打印
int  List_Print(DataList *p)
{
    int ret = 0;
    DataList *temp;
    if (p == NULL)
    {
        ret = -1;
    }
    temp = p->next;
    printf("数字排序如下:\n");
    while (temp)
    {
        printf("%d\n", temp->x);
        temp = temp->next;
    }
    return ret;
}
// 在数字x之后插入一个数字y
int List_Insert(DataList **p, int x, int y)
{
    DataList *phead;
    DataList *pCur;
    DataList *temp;
    int ret = 1;
    phead = *p;

    if (p == NULL)
    {
        ret = 0;
        goto END;
    }
    pCur = phead->next;
    while (pCur != NULL)
    {
        if (pCur->x == x)
        {
            temp = (DataList *)malloc(sizeof(DataList));
            if (temp == NULL)
            {
                ret = -1;
                goto END;
            }
            temp->x = y;

            temp->next = pCur->next;
            pCur->next = temp;
            break;
        }
        pCur = pCur->next;
    }
END:

    return ret;
}
// 删除数字X
int List_Del(DataList **p, int x)
{
    DataList *temp;
    DataList *pCur;
    DataList *pHead;
    int ret = 1;

    if (p == NULL)
    {
        ret = 0;
    }
    pHead = *p;
    pCur = pHead;
    while (pCur!=NULL)
    {
        temp = pCur->next;
        if ( temp->x == x)
        {
            pCur->next = temp->next;
            free(temp);
            temp = NULL;
            break;
        }
        pCur = pCur->next;
    }
    *p = pHead;
    return ret;
}
// 销毁列表
int List_Dest(DataList *p)
{
    DataList *temp;
    temp = p;
    if (p == NULL)
    {
        return -1;
    }
    while (p != NULL)
    {
        temp = p->next;
        free(p);
        p = temp;
    }

}

// 链表逆置
int List_Reverse(DataList *p)
{
    DataList *temp =NULL;// 缓存一个指针
    DataList *pCur =NULL;// 当前指针
    DataList *pPre = NULL ;//前驱

    pCur = p->next;

    pPre = p;

    while (pCur)
    {
        temp = pCur->next;
        pCur -> next = pPre;

        pPre = pCur;
        pCur = temp;
    }
    p->next->next = NULL;
    p->next = pPre;
    return 0;
}
void main()
{
    DataList *p = NULL;
    // 链表创建
    List_Create(&p);
    // 链表打印
    List_Print(p);
    List_Insert(&p, 10, 11);
    // 链表打印
    List_Print(p);

    List_Del(&p, 11);
    List_Print(p);

    List_Reverse(p);
    List_Print(p);

    List_Dest(p);
}

你可能感兴趣的:(C)