创建一个单链表

#pragma once

typedef int DataType;

#include<stdlib.h>

#include<assert.h>

#include<stdio.h>

typedef struct ListNode

{

    struct ListNode* next;

    DataType data;

}ListNode;//链表中的一个节点

//初始化

void ListInit(ListNode** ppfirst)

{

    assert(ppfirst != NULL);

     * ppfirst = NULL;

 

}

//销毁

void ListDestroy(ListNode** ppfirst)

{

    assert(ppfirst != NULL);

    *ppfirst = NULL;

}

//头插

void Listpushfront(ListNode** ppfirst,DataType data){

    assert(ppfirst != NULL);

    ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));

    newNode->data = data;

    newNode->next = *ppfirst;

    *ppfirst = newNode;

}

//尾插

void Listpushback(ListNode** ppfirst, DataType data){

    assert(ppfirst != NULL);

    ListNode *cur = *ppfirst;

    ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));

    newNode->data = data;

    newNode->next = NULL;

    if (*ppfirst == NULL){

        *ppfirst = newNode;

    }

    while (cur->next != NULL){

        cur = cur->next;

    }

    cur->next = NULL;

}

//随意插

void ListInsert(ListNode **ppfirst, ListNode *pos, DataType data){

    assert(ppfirst != NULL);

    ListNode *cur = *ppfirst;

    ListNode *newNode = (ListNode*)malloc(sizeof(ListNode));

    if (*ppfirst == pos){

        Listpushfront(ppfirst, data);

        return;

    }

    for (cur = *ppfirst; cur->next != pos; cur = cur->next){}

        cur->next = newNode;

        newNode->next = pos;

   

}

//头删

void Listpopfront(ListNode **ppfirst){

    assert(ppfirst != NULL);

    assert(*ppfirst != NULL);

    ListNode * cur = *ppfirst;

    *ppfirst = (*ppfirst)->next;

    free(cur);

}

//尾删

void Listpopback(ListNode **ppfirst){

    assert(ppfirst != NULL);

    assert(*ppfirst != NULL);

    if ((*ppfirst)->next == NULL)

    {

        free(*ppfirst);

    }

    ListNode *cur = *ppfirst;

    while (cur->next->next != NULL){

        cur = cur->next;

    }

    free(cur->next);

    cur->next = NULL;

 

}

//随意删

void ListNodeErase(ListNode **ppfirst, ListNode *pos){

    assert(ppfirst != NULL);

    ListNode *cur=*ppfirst;

    if (*ppfirst == pos){

        void Listpopfront(ppfirst, pos);

        return;

    }

    while (cur->next != pos){

        cur = cur->next;

    }

        cur->next = pos->next;

        free(pos);

   

}

//打印

void ListPrint(ListNode *first){

    for (ListNode *cur = first; cur != NULL; cur = cur->next)

    {

        printf("%d", cur->data);

    }

    printf("\n");

}

//查找

void ListNodefound(ListNode *pfirst, DataType data){

    for (ListNode *cur = pfirst; cur != NULL; cur = cur->next){

        if (cur->data == data){

            return cur;

        }

    }

    return NULL;

}

void test(){

    ListNode* first ;

    ListInit(&first);

    Listpushfront(&first, 2);

    Listpushfront(&first, 3);

    Listpushfront(&first, 4);

    Listpushfront(&first, 5);

    ListPrint(first);

}

猜你喜欢

转载自blog.csdn.net/Summer___Hui/article/details/84024028
0条评论
添加一条新回复