1、slist.h
#ifndef _slist_h_
#define _slist_h_
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include<memory.h>
#include<vld.h>
#pragma warning(disable:4996)
#define ElemType int
typedef struct SListNode
{
ElemType data;
struct SListNode *next;
}SListNode;
//不带头结点的单链表
typedef SListNode* SList;
void SListInit(SList *phead);
void SListPushBack(SList *phead, ElemType x);
void SListPushFront(SList *phead, ElemType x);
void SListShow(SList *phead);
void SListPopBack(SList *phead);
void SListPopFront(SList *phead);
bool SListInsertPos(SList *phead, SListNode* pos, ElemType x);
bool SListInsertVal(SList *phead, ElemType x);
void SListErasePos(SList *phead, SListNode* pos);
void SListEraseVal(SList *phead, ElemType x);
SListNode* SListFind(SList *phead, ElemType x);
size_t SListLength(SList *phead);
void SListSort(SList *phead);
void SListReverse(SList *phead);
void SListClear(SList *phead);
ElemType SListFront(SList phead);
ElemType SListBack(SList phead);
void SListErase_all(SList *phead, ElemType x);
#endif
(1) 单链表初始化
void SListInit(SList *phead)
{
assert(phead != NULL);
*phead = NULL;
}
(2)单链表尾插、头插
void SListPushBack(SList *phead, ElemType x)
{
assert(phead != NULL);
SListNode* s = (SListNode*)malloc(sizeof(SListNode));
assert(s != NULL);
s->data = x;
s->next = NULL;
SListNode* p = *phead;
if (p == NULL)
*phead = s;
else
{
while (p->next != NULL)
p = p->next;
p->next = s;
}
}
void SListPushFront(SList *phead, ElemType x)
{
assert(phead != NULL);
SListNode* s = (SListNode*)malloc(sizeof(SListNode));
assert(s != NULL);
s->data = x;
s->next = *phead;
*phead = s;
}
(3)单链表打印
void SListShow(SList *phead)
{
assert(phead != NULL);
SListNode* p = *phead;
while (p != NULL)
{
printf("%d-&