数据结构_单链表

来源:互联网 发布:四分位数java 编辑:程序博客网 时间:2024/06/14 16:25

单链表定义:

typedef int DataType;typedef struct SListNode{    struct SListNode* _next;    DataType _data;}SListNode;

对单链表“增删查改”的函数:

#ifndef __CODE_H__#define __CODE_H__#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <Windows.h>typedef int DataType;typedef struct SListNode{    struct SListNode* _next;    DataType _data;}SListNode;SListNode* BuySListNode(DataType x);  //创建一个结点void SListPrint(SListNode* pHead);    //打印单链表void SListDestory(SListNode** ppHead);    //删除单链表void SListPushBack(SListNode** ppHead, DataType x);   //尾插void SListPopBack(SListNode** ppHead);                //尾删void SListPushFront(SListNode** ppHead, DataType x);  //头插void SListPopFront(SListNode** ppHead);               //头删SListNode* SListFind(SListNode* pHead, DataType x);   //查找void SListInsest(SListNode** ppHead, SListNode* pos, DataType x);  //指定位置插入void SListErase(SListNode** ppHead, SListNode* pos);     //指定位置删除#endif  //__CODE_H__

函数的代码实现:

#include "code.h"SListNode* BuySListNode(DataType x){    SListNode* cur = (SListNode*)malloc(sizeof(SListNode));    cur->_data = x;    cur->_next = NULL;    return cur;}void SListPrint(SListNode* pHead){    //assert(pHead);    while (pHead)    {        printf("%d ", pHead->_data);        pHead = pHead->_next;    }    printf("\n");}void SListDestory(SListNode** ppHead){    assert(ppHead);    if (*ppHead == NULL)    {        return;    }    else    {        while (*ppHead)        {            SListPopFront(ppHead);        }    }}void SListPushBack(SListNode** ppHead, DataType x){    assert(ppHead);    if (*ppHead == NULL)    {        *ppHead = BuySListNode(x);    }    else    {        SListNode* cur = *ppHead;        while (cur->_next)        {            cur = cur->_next;        }        cur->_next = BuySListNode(x);    }}void SListPopBack(SListNode** ppHead){    assert(ppHead);    assert(*ppHead);    SListNode* cur = *ppHead;    SListNode* tail = NULL;    if ((*ppHead)->_next == NULL)    {        SListPopFront(ppHead);    }    else    {        while (cur->_next)        {            tail = cur;            cur = cur->_next;        }        free(cur);        tail->_next = NULL;    }}void SListPushFront(SListNode** ppHead, DataType x){    assert(ppHead);    if (*ppHead == NULL)    {        *ppHead = BuySListNode(x);    }    else    {        SListNode* cur = BuySListNode(x);        cur->_next = *ppHead;        *ppHead = cur;    }}void SListPopFront(SListNode** ppHead){    assert(ppHead);    if (*ppHead != NULL)    {        SListNode* cur = *ppHead;        *ppHead = (*ppHead)->_next;        free(cur);    }}SListNode* SListFind(SListNode* pHead, DataType x){    assert(pHead);    while (pHead)    {        if (pHead->_data == x)        {            return pHead;        }        else        {            pHead = pHead->_next;        }    }    return NULL;}void SListInsest(SListNode** ppHead, SListNode* pos, DataType x){    assert(ppHead);    assert(pos);    if (*ppHead == pos)    {        SListPushFront(ppHead, x);    }    else    {        SListNode* cur = *ppHead;        SListNode* tail = NULL;        while (cur->_next != pos)        {            cur = cur->_next;        }        tail = BuySListNode(x);        cur->_next = tail;        tail->_next = pos;    }}void SListErase(SListNode** ppHead, SListNode* pos){    assert(ppHead);    assert(pos);    if (*ppHead == pos)    {        SListPopFront(ppHead);    }    else    {        SListNode* cur = *ppHead;        while (cur->_next != pos)        {            cur = cur->_next;        }        SListPopFront(&pos);        cur->_next = pos;    }}

测试用例:

#include "code.h"SListNode* Node;void test1(){    SListPrint(Node);    SListPushFront(&Node, 3);    SListPushFront(&Node, 2);    SListPushFront(&Node, 1);    SListPushFront(&Node, 0);    SListPrint(Node);}void test2(){    SListPushBack(&Node, 4);    SListPushBack(&Node, 5);    SListPushBack(&Node, 6);    SListPrint(Node);}void test3(){    SListPopFront(&Node);    SListPopBack(&Node);    SListPrint(Node);}void test4(){    SListInsest(&Node, Node->_next, 10);    SListPrint(Node);}void test5(){    SListErase(&Node, Node->_next);    SListPrint(Node);}void test6(){    SListErase(&Node, SListFind(Node, 2));    SListPrint(Node);}void test7(){    SListDestory(&Node);    SListPrint(Node);}int main(){    test1();    test2();    test3();    test4();    test5();    test6();    test7();    return 0;}
原创粉丝点击