无头单向非循环链表的特点:不带头结点,只有next指针,不循环
SList.h
#ifndef __SLIST_H__
#define __SLIST_H__
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdlib.h>
#include <stdio.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 SListPushFront(SList* plist, SLTDataType x);//头插
void SListPopFront(SList* plist);//头删
SListNode* SListFind(SList* plist, SLTDataType x);//找到某个值
void SListInsertAfter(SListNode* pos, SLTDataType x);// 在pos的后面进行插入
void SListInsertFront(SListNode* pos, SLTDataType x);// 在pos的前面进行插入
void SListEraseAfter(SListNode* pos);//删除pos后面的值
void SListErase(SListNode* pos);//删除pos位置的值
void SListRemove(SList* plist, SLTDataType x);//删除所有x
void SListPrint(SList* plist);//打印
void TestSList();//测试函数
#endif//__SLIST_H__
SList.c
#include "SList.h"
void SListInit(SList* plist)
{
assert(plist);
plist->_head = NULL;
}
void SListDestory(SList* plist)
{
assert(plist);
SListNode* cur = plist->_head;
while (cur)
{
SListNode* next = cur->_next;
free(cur);
cur = next;
}
plist->_head = NULL;
}
SListNode* BuySListNode(SLTDataType x)
{
SListNode* newnode =(SListNode*) malloc(sizeof(SListNode));
newnode->_data = x;
newnode->_next = NULL;
return newnode;
}
void SListPushFront(SList* plist, SLTDataType x)
{
assert(plist);
SListNode* cur = plist->_head;
SListNode* newnode = BuySListNode(x);
newnode->_next = cur;
plist->_head = newnode;
}
void SListPopFront(SList* plist)
{
assert(plist);
SListNode* next = plist->_head->_next;
free(plist->_head);
plist->_head = next;
}
SListNode* SListFind(SList* plist, SLTDataType x)
{
assert(plist);
SListNode* cur = plist->_head;
while (cur)
{
if (cur->_data == x) return cur;
cur = cur->_next;
}
return NULL;
}
// 在pos的后面进行插入
void SListInsertAfter(SListNode* pos, SLTDataType x)
{
assert(pos);
SListNode* next = pos->_next;
SListNode* newnode = BuySListNode(x);
newnode->_next = next;
pos->_next = newnode;
}
// 在pos的前面进行插入
void SListInsertFront(SListNode* pos, SLTDataType x)
{
assert(pos);
SListNode* newnode = BuySListNode(x);
newnode->_next = pos->_next;
pos->_next = newnode;
newnode->_data = pos->_data;
pos->_data = x;
}
void SListEraseAfter(SListNode* pos)
{
assert(pos);
SListNode* next = pos->_next->_next;
if (pos->_next == NULL) return;
free(pos->_next);
pos->_next = next ;
}
void SListErase(SListNode* pos)
{
assert(pos);
SLTDataType n = pos->_next->_data;
SListNode* next = pos->_next->_next;
free(pos->_next);
pos->_next = next;
pos->_data = n;
}
void SListRemove(SList* plist, SLTDataType x)
{
assert(plist);
SListNode* cur = plist->_head;
/*if (cur->_data == x)
{
free(plist->_head);
plist->_head = cur->_next;
return;
}*/
SListNode* prev = NULL;
while (cur)
{
SListNode* next = cur->_next;
if (cur->_data == x)
{
if (cur == plist->_head)
plist->_head = next;
else
{
prev->_next = next;
free(cur);
cur = NULL;
}
break;
}
prev = cur;
cur = next;
}
}
void SListPrint(SList* plist)
{
SListNode* cur = plist->_head;
while (cur)
{
printf("%d ", cur->_data);
cur = cur->_next;
}
printf("\n");
}
void TestSList()
{
SList s1;
SListInit(&s1);
SListPushFront(&s1, 1);
SListPushFront(&s1, 2);
SListPushFront(&s1, 3);
SListPushFront(&s1, 4);
printf("头插的结果是: ");
SListPrint(&s1);
printf("头删的结果是: ");
SListPopFront(&s1);
SListPrint(&s1);
printf("在2后面插入4的结果是: ");
SListNode* n=SListFind(&s1, 2);
if (n == NULL) printf("位置不存在!原链表值为: \n");
else SListInsertFront(n, 4);
SListPrint(&s1);
printf("在2前面插入4的结果是: ");
n = SListFind(&s1, 2);
if (n == NULL) printf("位置不存在!原链表值为: \n");
else SListInsertAfter(n, 4);
SListPrint(&s1);
printf("删除2位置的值; ");
SListErase(n);
SListPrint(&s1);
printf("删除2后面的位置; ");
SListEraseAfter(n);
SListPrint(&s1);
printf("删除3这个值; ");
SListRemove(&s1, 3);
SListPrint(&s1);
SListDestory(&s1);
}
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
int main()
{
TestSList();
system("pause");
return 0;
}