您的位置:首页 > 理论基础 > 数据结构算法

数据结构_单链表

2017-12-15 23:51 162 查看
单链表定义:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: