实现的功能:
单链表的增删查改以及第pos位后插入
SL.h
#pragma once
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<stdio.h>
typedef int SLTDataType;//给类型起别名
typedef struct SList
{
SLTDataType _data;
struct SList* _next;//结构体类型的指针
}SList;
SList* BuySListNode(SLTDataType x);
void SListPushFront(SList** pplist, SLTDataType x);
void SListPopFront(SList** pplist);//头删
void SListPushBack(SList** pplist, SLTDataType x);//尾插
void SListPopBack(SList** pplist);//尾删
SList* SListFind(SList** pplist, SLTDataType x);
// 在pos的后面进行插入
void SListInsertAfter(SList** pplist, SLTDataType pos, SLTDataType x);//再pos的前面进行插入
void SListPrint(SList* plist);
void TestSList();
SL.c
#include"SL.h"
SList *BuySListNode(SLTDataType x)//创建一个新的结点
{
SList * node = (SList*)malloc(sizeof(SList));//node指针指向所开辟的空间,空间里有_data和_next
node->_data = x;
node->_next = NULL;
return node;
}
void SListPrint(SList* plist)//打印
{
SList * cur =plist;
while (cur)
{
printf("%d->", cur->_data);
cur = cur->_next;
}
printf("NULL\n");
}
void SListPushBack(SList** pplist, SLTDataType x)//尾插
{
SList* newnode = BuySListNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
SList* tail = *pplist;
while(tail->_next)
{
tail = tail->_next;
}
tail->_next = newnode;
}
}
void SListPopBack(SList** pplist)//尾删
{
SList*plist = *pplist;
SList*tail = *pplist;
SList*pre = *pplist;
if (plist == NULL || (plist->_next == NULL))
{
free(plist);
*pplist = NULL;
}
else
{
while (tail->_next)
{
pre = tail;
tail = tail->_next;
}
pre->_next = NULL;
free(tail);
tail = NULL;
}
}
void SListPopFront(SList** pplist)//头删
{
SList*plist = *pplist;
SList*next = *pplist;
if (plist == NULL )
{
*pplist = NULL;
}
else if (plist->_next == NULL)
{
free(plist);
*pplist=NULL;
}
else
{
next = plist->_next;
free(plist);
*pplist = next;
}
}
void SListPushFront(SList** pplist, SLTDataType x)//头插
{
SList* newnode =BuySListNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
newnode->_next =*pplist;
*pplist = newnode;
}
}
SList* SListFind(SList** pplist, SLTDataType x)//查找链表值
{
assert(pplist);
SList* tail = *pplist;
while (tail)
{
if (tail->_data == x)
{
printf("有\n");
return;
}
else
tail = tail->_next;
}printf("没有\n");
}
void SListInsertAfter(SList** pplist, SLTDataType pos, SLTDataType x)//再pos的后面进行插入
{
SList* newnode = BuySListNode(x);
SList* tail = *pplist;
SList* pre = tail;
if (* pplist== NULL)
{
tail = newnode;
*pplist = tail;
}
else
{
while (--pos)
{
if (tail == NULL)
{
printf("插入无效\n");
return;
}
tail = tail->_next;
}
SList* p= tail;
newnode->_next = tail->_next;
p->_next = newnode;
*pplist = pre;
}
}
test.c
#include"SL.h"
void test1()
{
SList*plist = NULL;
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
//SListPrint(&plist);//打印
// SListPushFront(&plist, 5);//头插
//SListPrint(&plist);//打印
SListPrint(plist);//打印
SListPopBack(&plist);//尾删
SListPrint(plist);//打印
SListPopFront(&plist);//头删
SListPrint(plist);//打印
SListPopFront(&plist);//头删
SListPrint(plist);//打印
}
void test2()
{
SList*plist = NULL;
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
//SListPushFront(&plist, 5);//头插
SListPrint(plist);//打印
//SListFind(&plist, 8);//查找链表值
SListInsertAfter(&plist, 2, 10);//再pos的后面进行插入
SListPrint(plist);//打印
}
int main()
{
//test1();
test2();
}