头文件 SList.h
# pragma once
# include "SList.h"
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
typedef int DataType;
typedef struct SList
{
DataType data;
struct SList *next;
}SList, *phead, **pphead;
//头插
void SListPushFront(pphead head, DataType x);
//尾插
void SListPushBack(pphead head, DataType x);
//头删
void SListPopFront(pphead head);
//尾删
void SListPopBack(pphead head);
//任意位置插
void Insert(pphead head, phead pos, DataType x);
//任意位置删
void Erase(pphead head, phead pos);
//算节点数
int SListSize(phead head);
//判断是否为空
int SListEmpty(phead head);
//查某个数所在节点位置
phead SListFind(phead head, DataType x);
//删除第一个x
void SlistRemove(pphead head, DataType x);
//删除x
void SlistRemoveAll(pphead head, DataType x);
//销毁
void SListDestory(pphead head);
//打印
void Show(SList *head);
//测试
void SListTest();
SList.c 文件
# include "SList.h"
//生成一个新的结点
static SList* BuyNewNode(DataType data)
{ SList *node = (SList*)malloc(sizeof(SList));
node->data = data;
node->next=NULL;
return node;
}
void SListPushFront(pphead head, DataType x)//头插
{
if (*head == NULL)
*head = BuyNewNode(x);
else
{
phead node = BuyNewNode(x);
node->next = *head;
*head = node;
}
}
void SListPushBack(pphead head, DataType x)//尾插
{
if (*head == NULL)
*head = BuyNewNode(x);
else
{
phead node = *head;
while (node->next != NULL)
node = node->next;
node->next = BuyNewNode(x);
}
}
void SListPopFront(pphead head)//头删
{
if (*head == NULL)//链表为空链表
{
printf("链表为空\n");
return;
}
phead node = (*head)->next;
free(*head);
*head = node;
}
void SListPopBack(pphead head)//尾删
{
phead node = *head;
phead newnode;
if (*head == NULL)//链表为空链表
{
printf("链表为空\n");
return;
}
if ((*head)->next == NULL)//链表只有一个节点
{
free(*head);
*head = NULL;
}
while (node->next->next)//找到倒数第二个,删掉倒数第一个
node = node->next;
newnode = node->next;//倒数第一个
node->next = newnode->next;//最后一个NULL赋给倒数第二个
free(newnode);//释放倒数第一个
}
void SListInsert(pphead head, phead pos, DataType x)//任意位置插
{
phead node = *head;
phead newnode = BuyNewNode(x);
if (*head == pos)
SListPushFront(head, x);
while (node->next != pos)
node = node->next;
newnode->next = pos;
node->next = newnode;
}
void SListErase(pphead head, phead pos)//任意位置删
{
phead node = *head;
if (*head == pos)
{
SListPopFront(head);
return;
}
while (node->next != pos)
node = node->next;
node->next = pos->next;
free(pos);
}
int SListSize(phead head)//算节点数
{
size_t size = 0;
phead node = head;
while (node)
{
node = node->next;
size++;
}
return size;
}
int SListEmpty(phead head)//判断是否为空
{
return head == NULL ? 1 : 0;
}
phead SListFind(phead head, DataType x)//查某个数所在节点位置
{
phead node = head;
while (node)
{
if (node->data == x)
return node;
node = node->next;
}
return NULL;
}
void SlistRemove(pphead head, DataType x)//删除第一个x
{
phead pos = SListFind(*head, x);
if (head == NULL)
return;
if (*head == NULL)
return;
if (pos != NULL)
SListErase(head, pos);
}
void SlistRemoveAll(pphead head, DataType x)//删除x
{
phead node = *head;
while (node->next)
{
if (node->next->data == x)
{
phead newnode = node->next;
node->next = newnode->next;
free(newnode);
}
else
node = node->next;
}
if ((*head)->data == x)
SListPopFront(head);
}
void SListDestory(pphead head)//销毁
{
phead node = *head;
while (node)
{
phead newnode = node->next;
free(node);
node = newnode;
}
*head = NULL;
}
//显示
void Show(phead head)
{
SList *phead = head;
for (; phead!= NULL; phead = phead->next)
{
printf("%2d ->", head->data);
}
printf("NULL\n");
}
//测试
void SListTest()
{
SList *head = NULL;
SListPushFront(&head, 1);
SListPushFront(&head, 2);
SListPushBack(&head, 2);
printf("%d\n", SListSize(head));
Show(head);
}
测试文件 test.c
# include "SList.h"
int main()
{
SListTest();
system("pause");
return 0;
}