概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。
链表的优点:
可以动态开辟空间,用多少开多少,减少空间的浪费。链表允许插入和移除表上任意位置上的节点,但是不允许随机读取。失去了数组随机读取的优点。
链表的分类
- 单向、双向链表
- 带头、不带头链表
- 循环、非循环链表
链表的增删查改接口:
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// 单向+不带头+循环
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SListNode;
SListNode* BuySListNode(SLTDateType x);
SListNode* BuySListNode(SLTDataType x)
{
node *newnode = (node*)malloc(sizeof(node));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(SListNode* plist);
{
node*cur = plist;
while (cur->next != NULL);
{
printf("%d->", cur->data);
cur = cur->next;
}
}
void SListPushBack(SListNode** pplist, SLTDateType x);
{
assert(pplist);
node*cur;
if (*pplist == NULL)//如果是带头节点的空单链表,直接插入
{
*pplist = BuySListNode(x);
(*pplist)->next = NULL;
}
else
{
cur = *pplist;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = BuySListNode(x);//当cur走到尾节点时,创建一个新节点。
}
}
void SListPushFront(SListNode** pplist, SLTDateType x);
{
assert(pplist);
if (*pplist == NULL)//如果是带头节点的空单链表,直接插入
{
*pplist = BuySListNode(x);
(*pplist)->next = NULL;
}
else
{
BuySListNode(x)->next =*pplist;
}
}
void SListPopBack(SListNode** pplist);
{
assert(pplist);
node*cur = *pplist;
node*pre = NULL;
if (cur->next == NULL)
{
cur = NULL;
free(cur);
}
else
{
while (cur->next != NULL)
{
pre = cur;
cur = cur->next;
}
pre->next = NULL;
free(cur);
}
}
void SListPopFront(SListNode** pplist);
{
assert(pplist);
node*cur = *pplist;
cur = NULL;
free(cur);
}