头文件
typedef int DataType;
//单链表节点
typedef struct SingleListNode
{
DataType _data;
struct SingleListNode* _next;
} SingleListNode;
typedef struct SingleList
{
SingleListNode* _head;
} SingleList;
void SingleListInit(SingleList* sl);
void SingleListDestroy();
SingleListNode* CreatNode(DataType x);
void SingleListPushBack(SingleList* sl, DataType x);
void SingleListPopBack(SingleList* sl);
void SingleListPushFront(SingleList* sl, DataType x);
void SingleListPopFront(SingleList* sl);
void SingleListInsertAfter(SingleListNode* pos, DataType x);
void SingleEraseAfter(SingleListNode* pos);
void SingleListRemove(SingleList* sl, DataType x);
#include "单链表.h"
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
//无头单向非循环
void SingleListInit(SingleList* sl)
{
assert(sl);
sl->_head = NULL;
}
//销毁单链表
void SingleListDestroy(SingleList* sl)
{
assert(sl);
SingleListNode* cur = sl->_head;
while (cur)
{
SingleListNode* next = cur->_next;
free(cur);
cur = next;
}
sl->_head = NULL;
}
//尾插
void SingleListPushBack(SingleList* sl, DataType x)
{
assert(sl);
//空链表
if (sl->_head == NULL)
{
sl->_head = CreatNode(x);
}
else
{
//遍历单链表
SingleListNode* cur = sl->_head;
while (cur->_next)
{
cur = cur->_next;
}
//找到最后一个节点
cur->_next = CreatNode(x);
}
}
//创建结点
SingleListNode* CreatNode(DataType x)
{
SingleListNode* newNode = (SingleListNode*)malloc(sizeof(SingleListNode));
newNode->_data = x;
newNode->_next = NULL;
return newNode;
}
//尾删: O(N)
void SingleListPopBack(SingleList* sl)
{
assert(sl);
SingleListNode* cur = sl->_head;
SingleListNode* prev = NULL;
if (cur == NULL)
return;
while (cur->_next)
{
prev = cur;
cur = cur->_next;
}
free(cur);
cur = NULL;
if (prev == NULL)
{
//删除的为头节点
sl->_head = NULL;
}
else
prev->_next = NULL;
}
// 头插: O(1)
void SingleListPushFront(SingleList* sl, DataType x)
{
assert(sl);
SingleListNode* cur = sl->_head;
SingleListNode* newNode = CreatNode(x);
//头插
newNode->_next = cur;
sl->_head = newNode;
}
//头删:O(1)
void SingleListPopFront(SingleList* sl)
{
assert(sl);
SingleListNode* cur, * next;
if (sl->_head == NULL)
return;
cur = sl->_head;
next = sl->_head->_next;
free(cur);
cur = NULL;
sl->_head = next;
}
//在指定位置pos之后插入
void SingleListInsertAfter(SingleListNode* pos, DataType x)
{
assert(pos);
SingleListNode* newNode = CreatNode(x);
SingleListNode* next = pos->_next;
pos->_next = newNode;
newNode->_next = next;
}
//删除指定位置pos之后的值
void SingleEraseAfter(SingleListNode* pos)
{
assert(pos);
SingleListNode* next = pos->_next;
if (next == NULL)
return;
else
{
SingleListNode* next2 = next->_next;
pos->_next = next2;
free(next);
next = NULL;
}
}
//删除x
void SingleListRemove(SingleList* sl, DataType x)
{
assert(sl);
SingleListNode* cur = sl->_head;
SingleListNode* prev = NULL;
while (cur)
{
if (cur->_data == x)
{
if (prev == NULL)
{
sl->_head = cur->_next;
}
else
{
prev->_next = cur->_next;
}
free(cur);
cur = NULL;
}
else
{
prev = cur;
cur = cur->_next;
}
}
}