SList.h
#ifndef __SLIST_H__
#define __SLIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType _data;
struct SListNode* _next;
}SListNode;
typedef struct SList
{
SListNode* _head;
}SList;
void SListInit(SList* plist);
void SListDestory(SList* plist);
SListNode* BuySListNode(SLTDataType x);
void SListPushBack(SList* plist, SLTDataType x);
void SListPopBack(SList* plist);
void SListPushFront(SList* plist, SLTDataType x);
void SListPopFront(SList* plist);
SListNode* SListFind(SList* plist, SLTDataType x);
void SListInsertAfter(SListNode* pos, SLTDataType x);
void SListEraseAfter(SListNode* pos);
void SListRemove(SList* plist, SLTDataType x);
void SListPrint(SList* plist)
#endif
SList.c
#include "Slist.h"
void SListInit(SList* plist)
{
assert(plist != NULL);
plist->_head = NULL;
}
void SListDestory(SList* plist)
{
assert(plist != NULL);
SListNode* cur = plist->_head;
while (cur != NULL)
{
SListNode* next = cur->_next;
free(cur);
cur = next;
}
plist->_head = NULL;
}
SListNode* BuySListNode(SLTDataType x)
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));
assert(node != NULL);
node->_data = x;
node->_next = NULL;
return node;
}
void SListPushBack(SList* plist, SLTDataType x)
{
assert(plist != NULL);
SListNode* newnode = BuySListNode(x);
SListNode* tail = plist->_head;
if (plist->_head == NULL)
{
plist->_head = newnode;
}
else
{
while (tail->_next != NULL)
{
tail = tail->_next;
}
tail->_next = newnode;
}
}
void SListPopBack(SList* plist)
{
assert(plist != NULL);
SListNode* prev = NULL;
SListNode* tail = plist->_head;
if (tail->_next == NULL)
{
free(tail);
tail = NULL;
plist->_head = NULL;
}
else
{
while (tail->_next != NULL)
{
prev = tail;
tail = tail->_next;
}
free(tail);
tail = NULL;
prev->_next = NULL;
}
}
void SListPushFront(SList* plist, SLTDataType x)
{
assert(plist != NULL);
SListNode* newnode = BuySListNode(x);
newnode->_next = plist->_head;
plist->_head = newnode;
}
void SListPopFront(SList* plist)
{
assert(plist != NULL);
SListNode* next = plist->_head->_next;
free(plist->_head);
plist->_head = next;
}
SListNode* SListFind(SList* plist, SLTDataType x)
{
assert(plist != NULL);
SListNode* cur = plist->_head;
while (cur != NULL)
{
if (cur->_data == x)
{
return cur;
}
else
{
cur = cur->_next;
}
}
return cur;
}
void SListInsertAfter(SListNode* pos, SLTDataType x)
{
assert(pos != NULL);
SListNode* newnode = BuySListNode(x);
assert(newnode != NULL);
newnode->_next = pos->_next;
pos->_next = newnode;
}
void SListInsert(SListNode* pos, SLTDataType x)
{
assert(pos != NULL);
SListNode* newnode = BuySListNode(x);
assert(newnode != NULL);
newnode->_next = pos->_next;
pos->_next = newnode;
newnode->_data = pos->_data;
pos->_data = x;
}
void SListEraseAfter(SListNode* pos)
{
assert(pos != NULL);
if (pos->_next == NULL)
{
return;
}
SListNode* next = pos->_next->_next;
free(pos->_next);
pos->_next = next;
}
void SListRemove(SList* plist, SLTDataType x)
{
assert(plist != NULL);
if (plist->_head->_data == x)
{
SListPopFront(plist);
return;
}
SListNode* cur = plist->_head;
SListNode* prev = NULL;
while (cur != NULL)
{
if (cur->_data == x)
{
prev->_next = cur->_next;
free(cur);
cur = NULL;
break;
}
else
{
prev = cur;
cur = cur->_next;
}
}
}
void SListPrint(SList* plist)
{
assert(plist != NULL);
SListNode* cur = plist->_head;
while (cur != NULL)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("NULL\n");
}