单链表专题
test.c
#define _CRT_SECURE_NO_WARNINGS
//#include "SList.h"
//void SListTest01()
//{
// //创建几个节点
// SLTNode* node1 = (SLTNode*)malloc(sizeof(SLTNode));
// node1->data = 1;
//
// SLTNode* node2 = (SLTNode*)malloc(sizeof(SLTNode));
// node2->data = 2;
//
// SLTNode* node3 = (SLTNode*)malloc(sizeof(SLTNode));
// node3->data = 3;
//
// SLTNode* node4 = (SLTNode*)malloc(sizeof(SLTNode));
// node4->data = 4;
// //将四个节点连接起来
// node1->next = node2;
// node2->next = node3;
// node3->next = node4;
// node4->next = NULL;
// //调用链表的打印
// SLTNode* plist = node1;
//
//}
//
//void SListTest02()
//{
// SLTNode* plist = NULL;
// SLTPushBack(&plist, 1);
// SLTPushBack(&plist, 2);
// SLTPushBack(&plist, 3);
// SLTPushBack(&plist, 4);
// SLTPrint(plist);
//
// /*SLTPushFront(&plist, 5);
// SLTPrint(plist);
//
// SLTPushFront(&plist, 6);
// SLTPrint(plist);
//
// SLTPushFront(&plist, 7);
// SLTPrint(plist);
//
// SLTPopBack(&plist);
// SLTPrint(plist);
//
// SLTPopFront(&plist);
// SLTPrint(plist);*/
//
//
// SLTNode* p = SLTFind(plist, 1);
// /*SLTInsert(&plist, p, 11);
// SLTInsertAfter(&plist, p, 15);*/
// //SLTErase(&plist,p);
// SLTEraseAfter(p);
// SListDestory(&plist);
// SLTPrint(plist);
//
// /*if (p != NULL)
// {
// printf("找到了");
// }
// else
// {
// printf("未找到");
// }*/
//
//
//}
//
//
//int main()
//{
// //SListTest01();
// SListTest02();
// return 0;
//}
SList.c
//typedef int Int;
//void Delete(int* arr, Int i, int sz)
//{
// for (int m = i; m < sz - 1; m++)
// {
// arr[i] = arr[i + 1];
// }
//}
//int remove(Int* arr, int sz, Int a
//{
// int newsize = sz;
// for (int i = 0; i < sz; i++)
// {
// if (arr[i] == a)
// {
// Delete(arr, i, sz);
// newsize--;
// }
// }
// return newsize;
//}
//int main()
//{
// Int arr[] = { 1,23,4,5,3,2,1,4,2 };
// Int a = 23;
// int sz = sizeof(arr) / sizeof(arr[0]);
// int newsize = remove(&arr, sz, a)
// printf("%d", newsize);
// return 0;
//}
//采用双指针法
//int newsize(int* arr, int sz, int a)
//{
// assert(arr);
// int src, dst;
// src = dst = 0;
// while (src < sz)
// {
// if (arr[src] == a)
// {
// src++;
// }
// else
// {
// arr[dst] = arr[src];
// src++;
// dst++;
// }
// }
// return dst;
//}
// 1 4 5
// 1 3 4
// 1 1
//int main()
//{
// int arr[] = { 1,2,34,2,4,6,3,6,7 };
// int sz = sizeof(arr) / sizeof(arr[0]);
// int a = 2;
// int newsize = remove(arr,sz,a);
// printf("%d", newsize);
// return 0;
//}
//void move(int* nums1, int* nums2, int m, int n)
//{
// int l1 = m - 1;
// int l2 = n - 1;
// int l3 = m + n - 1;
// while (l1 >= 0 && l2 >= 0)
// {
// if (nums1[l1] < nums2[l2])
// {
// nums1[l3--] = nums2[l2--];
// }
// else
// {
// nums1[l3--] = nums2[l1--];
//
// }
// }
// while (l2 >= 0)
// {
// nums1[l3--] = nums2[l2--];
// }
//}
//int main()
//{
// int nums1[] = {1,2,3};
// int nums2[] = { 2,5,6 };
// int m = sizeof(nums1) / sizeof(nums1[0]);
// int n = sizeof(nums2) / sizeof(nums2[0]);
//
// move(nums1,nums2,m,n);
// for (int i = 0;i < m + n; i++)
// {
// printf("%d", nums1[i]);
// }
// return 0;
//}
//针对顺序表:中间/头部插入效率低下,增容降低运行效率、增容造成空间浪费
//链表
//链表也是线性表的一种,物理结构上非连续、非顺序,数据元素的逻辑顺序是通过链表中的指针链接次序实现的
// 链表是由一个一个的节点组成(结点)
// 链表有两部分组成——数据域和指针域
//
#include "SList.h"
void SLTPrint(SLTNode* phead)
{
SLTNode* pcur = phead;
while (pcur)
{
printf("%d->", pcur->data);
pcur = pcur->next;
}
printf("NULL\n");
}
SLTNode* SLTBuyNode(SLTDatatype x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
perror("SLTBuyNode()::malloc");
exit(1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SLTPushBack(SLTNode** pphead, SLTDatatype x)
{
assert(pphead);
SLTNode* newnode = SLTBuyNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
//尾插法:先找到尾结点,再将尾结点和新节点连接起来
SLTNode* ptail = *pphead;
while (ptail->next)
{
ptail = ptail->next;
}
ptail->next = newnode;
}
}
void SLTPushFront(SLTNode** pphead, SLTDatatype x)
{
assert(pphead);
SLTNode* newnode = SLTBuyNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
newnode->next = *pphead;
*pphead = newnode;
}
}
void SLTPopBack(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* prev = *pphead;
SLTNode* ptail = *pphead;
while (ptail->next)
{
prev = ptail;
ptail = ptail->next;
}
free(ptail);
ptail = NULL;
prev->next = NULL;
}
}
void SLTPopFront(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* pp = *pphead;
*pphead = (*pphead)->next;
free(pp);
pp = NULL;
}
}
SLTNode* SLTFind(SLTNode* phead, SLTDatatype x)
{
assert(phead);
SLTNode* pcur = phead;
while (pcur )
{
if (pcur->data == x)
{
return pcur;
}
else
{
pcur = pcur->next;
}
}
return NULL;
}
void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDatatype x)
{
assert(phead);
assert(pos);
if (pos == *phead)
{
SLTPushFront(phead,x);
}
else
{
SLTNode* pcur = *phead;
while (pcur->next != pos)
{
pcur = pcur->next;
}
SLTNode* newnode = SLTBuyNode(x);
newnode->next = pos;
pcur->next = newnode;
}
}
void SLTInsertAfter(SLTNode** phead, SLTNode* pos, SLTDatatype x)
{
assert(phead);
assert(pos);
SLTNode* newnode = SLTBuyNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
assert(pphead);
assert(pos);
SLTNode* prev = *pphead;
if (pos == *pphead)
{
SLTPopFront(pphead);
}
else
{
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
void SLTEraseAfter(SLTNode* pos)
{
assert(pos);
assert(pos->next);
SLTNode* prev = pos->next ;
pos->next = prev->next;
free(prev);
prev = NULL;
}
void SListDestory(SLTNode** pphead)
{
assert(pphead && *pphead);
SLTNode* pcur = *pphead;
while (pcur)
{
SLTNode* next = pcur->next;
free(pcur);
pcur = next;
}
*pphead = NULL;
}
SList,h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLTDatatype;
//定义节点的结构
typedef struct SListNode
{
SLTDatatype data;//数据域
struct SListNode* next;//指针域
}SLTNode;
void SLTPrint(SLTNode* phead);
void SLTPushBack(SLTNode** pphead, SLTDatatype x);
void SLTPushFront(SLTNode** pphead, SLTDatatype x);
//尾删
void SLTPopBack(SLTNode** pphead);
//头插
void SLTPopFront(SLTNode** pphead);
//查找
SLTNode* SLTFind(SLTNode* phead, SLTDatatype x);
//在指定位置之前插入数据
void SLTInsert(SLTNode** phead, SLTNode* pos, SLTDatatype x);
//在指定位置之后插入数据
void SLTInsertAfter(SLTNode** phead, SLTNode* pos, SLTDatatype x);
//删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos);
//删除pos之后的节点
void SLTEraseAfter(SLTNode* pos);
//销毁链表
void SListDestory(SLTNode** pphead);