#pragma once
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int Datatype;
typedef struct SListNode
{
Datatype data;
SListNode *next;
}SListNode;
SListNode* BuyNode(Datatype x);//申请空间
void PrintNode(SListNode*& pHead);//打印链表
void CreatNode(SListNode*& pHead);//创建表
void DestoryNode(SListNode*& pHead);//销毁
void PushBack(SListNode*& pHead, Datatype x);//尾插
void PopBack(SListNode*& pHead);//尾删
void PushFront(SListNode*& pHead, Datatype x);//头插
void PopFront(SListNode*& pHead);//头删
void Insert(SListNode*& pHead, SListNode*& pos, Datatype x);//指定位置插入
void Delete(SListNode*& pHead, SListNode*& pos);//删除指定位置
void Remove(SListNode*& pHead, Datatype x);//删除x
void Search(SListNode*& pHead, Datatype x);//查找X
#include"SNode.h"
SListNode* BuyNode(Datatype x)
{
SListNode* Node = (SListNode*)malloc(sizeof(SListNode));
Node->data = x;
Node->next = NULL;
return Node;
}
void CreatNode(SListNode*& pHead)
{
pHead = NULL;
}
void PrintNode(SListNode*& pHead)//打印链表
{
SListNode* cur = pHead;
while (cur)
{
printf("%d", cur->data);
cur = cur->next;
}
printf("\n");
}
void DestoryNode(SListNode*& pHead)//销毁
{
SListNode* cur = pHead;
while (cur)
{
SListNode* temp = cur;
cur = cur->next;
free(temp);
temp = NULL;
}
}
void PushBack(SListNode*& pHead, Datatype x)
{
if (pHead == NULL)
{
pHead=BuyNode(x);
}
else
{
SListNode* cur = pHead;
while (cur->next)
{
cur = cur->next;
}
cur->next = BuyNode(x);
}
}
void PopBack(SListNode*& pHead)
{
if (pHead == NULL)
{
printf("The List is Empty\n");
return;
}
else if (pHead->next == NULL)
{
free(pHead);
pHead = NULL;
}
else
{
SListNode* pre=NULL;
SListNode* cur=pHead;
while (cur->next)
{
pre = cur;
cur = cur->next;
}
free(cur);
pre->next = NULL;
}
}
void PushFront(SListNode*& pHead, Datatype x)
{
if (pHead == NULL)
pHead = BuyNode(x);
else
{
SListNode* temp;
temp = BuyNode(x);
temp->next = pHead;
pHead = temp;
}
}
void PopFront(SListNode*& pHead)
{
if (pHead == NULL)
{
printf("The List is Empty\n");
return;
}
else if (pHead->next == NULL)
{
free(pHead);
pHead = NULL;
}
else
{
SListNode* temp = pHead->next;
free(pHead);
pHead = temp;
}
}
void Insert(SListNode*& pHead, SListNode*& pos, Datatype x)//指定位置插入
{
assert(pos);
assert(pHead);
if (pos == pHead)
{
PushFront(pHead, x);
}
else
{
SListNode* pre = NULL;
SListNode* cur = NULL;
cur = pHead;
pre = pHead;
while (cur != pos)
{
pre = cur;
cur = cur->next;
}
SListNode* temp = NULL;
temp = BuyNode(x);
temp->next = cur;
pre->next = temp;
}
}
void Delete(SListNode*& pHead, SListNode*& pos)//删除指定位置
{
assert(pos);
assert(pHead);
if (pos == pHead)
{
free(pHead);
pHead = NULL;
}
else
{
SListNode* pre, *cur;
cur = pHead;
while (cur!=pos)
{
pre = cur;
cur = cur->next;
}
pre->next = cur->next;
free(cur);
}
}
void Remove(SListNode*& pHead, Datatype x)//删除x
{
if (pHead == NULL)
{
printf("The List is Empty\n");
}
else if (pHead->data == x)
{
SListNode* next = pHead->next;
free(pHead);
pHead = next;
}
else
{
SListNode* pre, *cur;
cur = pHead;
while (cur->data != x)
{
pre = cur;
cur = cur->next;
}
pre->next = cur->next;
free(cur);
}
}
SListNode* Search(SListNode*& pHead, Datatype x)//查找X
{
SListNode* cur = pHead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}