目录
链表的概念和结构
链表是一种物理存储上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的
链式结构逻辑连续,物理不一定连续
单链表的实现
无头 单向 非循环链表
申请新结点
打印
尾插
头插
尾删
头删
查找
在pos位置前插入元素
在pos位置后插入元素
删除pos位置的元素
删除pos位置之后的位置的元素
完整代码
SListNode.h
cpp
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;
SLTNode* BuySListNode(SLTDataType x); //申请一个结点
void SListNodePrint(SLTNode* plist); //打印
void SListPushBack(SLTNode** pplist, SLTDataType x); //尾插
void SListPushfront(SLTNode** pplist, SLTDataType x); //头插
void SListPopBack(SLTNode** pplist); //尾删
void SListPopfront(SLTNode** pplist); //头删
SLTNode* SListFind(SLTNode* plist, SLTDataType x); //查找
void SListInsert(SLTNode** pplist, SLTNode* pos, SLTDataType x); //在pos位置前插入元素
void SListInsertAfter(SLTNode* pos, SLTDataType x); //在pos位置后插入元素
void SListErase(SLTNode** pplist, SLTNode* pos); //删除pos位置前的元素
void SListEraseAfter(SLTNode* pos); //删除pos位置之后的位置的元素
SListNode.c
cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "SListNode.h"
SLTNode* BuySListNode(SLTDataType x) //申请一个新结点
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
perror("malloc failed");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListNodePrint(SLTNode* plist) //打印
{
SLTNode* cur = plist;
while (cur)
{
printf(" %d ->", cur->data);
cur = cur->next;
}
printf("NULL");
printf("\n");
}
void SListPushBack(SLTNode** pplist, SLTDataType x) //尾插
{
assert(pplist);
SLTNode* newnode = BuySListNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
SLTNode* tail = *pplist;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushfront(SLTNode** pplist, SLTDataType x) //头插
{
assert(pplist);
SLTNode* newnode = BuySListNode(x);
newnode->next = *pplist;
*pplist = newnode;
}
void SListPopBack(SLTNode** pplist) //尾删
{
assert(pplist);
assert(*pplist);//空链表
//一个结点
if ((*pplist)->next == NULL)
{
free(*pplist);
*pplist = NULL;
}
//一个以上结点
else
{
SLTNode* tail = *pplist;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
void SListPopfront(SLTNode** pplist) //头删
{
assert(pplist);
assert(*pplist);
SLTNode* cur = *pplist;
*pplist = (*pplist)->next;
free(cur);
}
SLTNode* SListFind(SLTNode* plist, SLTDataType x) //查找
{
SLTNode* cur = plist;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void SListInsert(SLTNode** pplist, SLTNode* pos, SLTDataType x) //在pos位置前插入元素
{
assert(pplist);
assert(pos);
if (*pplist == pos)
{
SListPopfront(pplist, x);
}
else
{
SLTNode* cur = *pplist;
while (cur->next != pos)
{
cur = cur->next;
}
SLTNode* newnode = BuySListNode(x);
newnode->next = cur->next;
cur->next = newnode;
}
}
void SListInsertAfter(SLTNode* pos, SLTDataType x) //在pos位置后插入元素
{
assert(pos);
SLTNode* newnode = BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListErase(SLTNode** pplist, SLTNode* pos) //删除pos位置的元素
{
assert(pplist);
assert(pos);
if (*pplist == pos)
{
SListPopfront(pplist);
}
else
{
SLTNode* cur = *pplist;
while (cur->next->next = pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
void SListEraseAfter(SLTNode* pos) //删除pos位置之后的位置的元素
{
assert(pos);
assert(pos->next);
SLTNode* cur = pos->next;
pos->next = cur->next;
free(cur);
cur = NULL;
}