线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的,也可以是不连续的)。因此,为了表示每个数据元素与其直接后继数据元素之间的逻辑关系,对数据元素来说,除了存储其本身的信息之外,还需存储一个指示其直接后继的信息(即直接后继的存储位置)。这两部分信息组成数据元素的存储映像,称为结点(node)。它包括两个城: 其中存储数据元素信息的域称为数据域: 存储直接后继存储位置的域称为指针域: 指针域中存储的信息称做指针或链。n个结点链结成一个链表,即为线性表的链式存储结构。又由于此链表的每个结点中只包含一个指针域,故又称线性链表或单链表。
头文件SList:
#include<stdio.h>
#include<assert.h>
#include<windows.h>
#include<stdlib.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);//头插
SListNode* SListFind(SListNode* pHead, DataType x);//寻找x 返回地址
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x);//指定插入
void SListErase(SListNode** ppHead, SListNode* pos);//指定删除
函数实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
SListNode* BuySListNode(DataType x)
{
SListNode*newNode = (SListNode*)malloc(sizeof(SListNode));//开辟一段空间
if (NULL == newNode)//创建失败
{
printf("to create is failed\n");
}
newNode->_data = x;
newNode->_next = NULL;
return newNode;
}
void SListPrint(SListNode* pHead)
{
if (pHead == NULL)
{
printf("链表为空\n");
}
while (pHead)
{
printf("%d->", pHead->_data);
pHead = pHead->_next;
}
printf("NULL");
printf("\n");
}
void SListDestory(SListNode** ppHead)
{
assert(ppHead);
SListNode*next = *ppHead;;
SListNode*cur = *ppHead;
if (*ppHead == NULL)
{
return;
}
while (next)
{
cur = next;
next = next->_next;
free(cur);
cur= NULL;
}
*ppHead = NULL;
}
void SListPushBack(SListNode** ppHead, DataType x)
{
assert(ppHead);
SListNode*cur;
if (*ppHead == NULL)//如果是带头节点的空单链表,直接插入
{
*ppHead = BuySListNode(x);
(*ppHead)->_next = NULL;
}
else
{
cur = *ppHead;
while (cur->_next != NULL)
{
cur = cur->_next;
}
cur->_next = BuySListNode(x);//当cur走到尾节点时,创建一个新节点。
}
}
void SListPopBack(SListNode** ppHead)
{
assert(ppHead);
SListNode*cur = *ppHead;
SListNode*prev = NULL;
if (cur == NULL)
{
return;
}
else if (cur->_next == NULL)
{
cur = NULL;
free(cur);
}
else
{
while (cur->_next!=NULL)
{
prev = cur;
cur = cur->_next;
}
free(cur);
prev->_next = NULL;
}
}
void SListPushFront(SListNode** ppHead, DataType x)
{
assert(ppHead);
SListNode*cur = NULL;
if (*ppHead == NULL)
{
*ppHead = BuySListNode(x);
(*ppHead)->_next = NULL;
}
else
{
cur = *ppHead;
*ppHead = BuySListNode(x);
(*ppHead)->_next = cur;
}
}
SListNode* SListFind(SListNode* pHead, DataType x)
{
assert(pHead);
while (pHead)
{
if (x == pHead->_data)
{
return pHead;
}
pHead = pHead->_next;
}
printf("寻找元素不存在\n");
return NULL;
}
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)
{
assert(ppHead != NULL&&pos != NULL);
SListNode*prev = *ppHead;//记录pos位置的前一个节点
SListNode*newNode = BuySListNode(x);
if (prev == NULL || pos == prev)
{
SListPushFront(ppHead, x);
}
else
{
while (prev->_next != pos)
{
prev = prev->_next;
}
newNode->_next = pos;
prev->_next = newNode;
}
}
void SListErase(SListNode** ppHead, SListNode* pos)
{
assert(ppHead != NULL&&pos != NULL);
SListNode*cur = *ppHead;
SListNode*prev = NULL;
if (*ppHead == pos)
{
*ppHead = cur->_next;
free(cur);
}
else
{
prev = *ppHead;
while (prev->_next != pos)
{
prev = prev->_next;
}
prev->_next = pos->_next;
free(pos);
}
}
int main()
{
SListNode* pHead=NULL;
SListNode* pos = NULL;
SListPushBack(&pHead, 1);
SListPushBack(&pHead, 2);
SListPushBack(&pHead, 3);
SListPushBack(&pHead, 4);
SListPushBack(&pHead, 5);
SListPrint(pHead);
SListPopBack(&pHead);
SListPrint(pHead);
SListPushFront(&pHead, 10);
SListPrint(pHead);
pos = SListFind(pHead, 3);
SListInsest(&pHead, pos, 20);
SListPrint(pHead);
pos = SListFind(pHead, 2);
SListErase(&pHead, pos);
SListPrint(pHead);
SListDestory(&pHead);
SListPrint(pHead);
system("pause");
return 0;
}