头文件代码
包括基本的头文件和单链表的一些基本函数声明
#pragma once
#include<iostream>
#include<stdlib.h>
#include<assert.h>
typedef int Type;
typedef class SListNode
{
Type data;
struct SListNode* next;
}SL;
void SListPrint(SL* ps);
void SListPushBack(SL** pps, Type x);
void SListPushFront(SL** pps,Type x);
void SListPopBack(SL** pps);
void SListPopFront(SL** pps);
SL* SListFind(SL* phead, Type x);
void SListInsert(SL** pphead, SL* pos, Type x);
void SListInsertAfter(SL* pos, Type x);
void SListEraseAfter(SL* pos);
void SListErase(SL** pphead, SL* pos);
void Destroy(SL** pphead);
源文件函数实现部分
此部分包含头文件中所有接口函数的实现
#include"Slist.h"
SL* BuyListNode(Type x)
{
SL* newnode = (SL*)malloc(sizeof(SL));
if (newnode == NULL)
{
cout << "malloc fail" << endl;
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(SL* phead)
{
SL* cur = phead;
while (cur != NULL)
{
cout << cur->data<<" ";
//printf("%d", cur->data);
cur = cur->next;
}
cout << endl;
}
void SListPushBack(SL** pps,Type x)
{
SL* newnode = BuyListNode(x);
if (*pps == NULL)
{
*pps = newnode;
}
else
{
//找到尾节点
SL* tail = *pps;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SL** pphead, Type x)
{
SL* newnode = BuyListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPopBack(SL** pphead)
{
assert(*pphead != NULL);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SL* prev = NULL;
SL* tail = *pphead;
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
void SListPopFront(SL** pphead)
{
assert(*pphead != NULL);
SL* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
SL* SListFind(SL* phead, Type x)
{
SL* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
else
{
cur = cur->next;
}
return NULL;
}
}
//在pos位置之前去插入一个节点
void SListInsert(SL** pphead, SL* pos, Type x)
{
SL* newnode = BuyListNode(x);
if (*pphead == pos)
{
newnode->next = *pphead;
*pphead = newnode;
}
else
{
//找到pos的第一个位置
SL* posPrev = *pphead;
while (posPrev->next != pos)
{
posPrev = posPrev->next;
}
posPrev->next = newnode;
newnode->next = pos;
}
}
//在pos的后面插入
void SListInsertAfter(SL* pos, Type x)
{
SL* newnode = BuyListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListErase(SL** pphead, SL* pos)
{
if (*pphead == pos)
{
*pphead = pos->next;
free(pos);
}
else
{
SL* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void SListEraseAfter(SL* pos)
{
assert(pos->next);
SL* next = pos->next;
pos->next = next->next;
free(next);
}
void Destroy(SL** pphead)
{
assert(pphead);
free(*pphead);
SL* cur = *pphead;
while (cur)
{
SL* next = cur->next;
free(cur);
cur = next;
}
}
源文件住函数部分
#include"Slist.h"
void test01()
{
SL* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SListPushFront(&plist, 7);
SListPushFront(&plist, 6);
SListPushFront(&plist, 5);
SListPushFront(&plist, 4);
SListPopBack(&plist);
SListPopBack(&plist);
SListPopBack(&plist);
SListPrint(plist);
//根据具体需求调用不同的接口函数
}
int main()
{
test01();
system("pause");
return 0;
}