动态顺序表在头部中部插入数据时和扩容时总会产生一些资源的浪费或性能的浪费所以我们可以用链表进行代替.
(顺序表自然也有其优势从不同方面讲总是各有各优点的)
链表优点
- 空间上,按需求给空间(不会造成空间的浪费
- 不要求物理空间连续头部中部插入时不需要挪动数据.
链表
链表是通过结构题创建的一种物理储存结构上非连续的,非顺序的存储结构链表之间的连接是通过指针进行次序连接实现的.
Slistnode.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLTdatetype;
struct SListNode
{
SLTdatetype date;
struct SListNode* next;
};
typedef struct SListNode SListNode;
void SListPrint(SListNode* plist);
void SListPushBack(SListNode** pplist, SLTdatetype x);
void SListPushFront(SListNode** pplist, SLTdatetype x);
void SListPopBack(SListNode** pplist);
void SListPopFront(SListNode** pplist);
SListNode* SListFind(SListNode* plist, SLTdatetype x);
void SListInsert(SListNode** pplist, SListNode* pos, SLTdatetype x);
void SListErase(SListNode** pplist, SListNode* pos);
Slistnode.c
#include"work.h"
void SListPrint(SListNode* plist)
{
SListNode* cur = plist;
while (cur != NULL)
{
printf("%d->", cur->date);
cur = cur->next;
}
printf("NULL\n");
}
SListNode* BuyNode()
{
SListNode* newnode = malloc(sizeof(SListNode));
if (newnode == NULL)
{
perror("BuyNode");
return NULL;
}
return newnode;
}
void SListPushBack(SListNode** pplist, SLTdatetype x)
{
SListNode* newnode = BuyNode();
newnode->date = x;
newnode->next = NULL;
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
SListNode* tail = *pplist;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SListNode** pplist, SLTdatetype x)
{
SListNode* newnode = BuyNode();
newnode->date = x;
newnode->next = *pplist;
*pplist = newnode;
}
void SListPopBack(SListNode** pplist)
{
assert(*pplist);
if ((*pplist)->next == NULL)
{
free(*pplist);
*pplist = NULL;
}
else
{
SListNode* tail = *pplist;
SListNode* prev = *pplist;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
prev->next = NULL;
free(tail);
}
}
void SListPopFront(SListNode** pplist)
{
assert(*pplist);
SListNode* next = (*pplist)->next;
free(*pplist);
*pplist = next;
}
SListNode* SListFind(SListNode* plist, SLTdatetype x)
{
while (plist != NULL)
{
if (plist->date == x)
{
return plist;
}
plist = plist->next;
}
return NULL;
}
void SListInsert(SListNode** pplist, SListNode* pos, SLTdatetype x)
{
if (pos == *pplist)
{
SListPushFront(pplist, x);
}
else
{
SListNode* newnode = BuyNode();
newnode->date = x;
SListNode* prve = *pplist;
while (prve->next != pos)
{
prve = prve->next;
}
prve->next = newnode;
newnode->next = pos;
}
}
void SListErase(SListNode** pplist, SListNode* pos)
{
if (pos ==*pplist)
{
SListPopFront(pplist);
}
else
{
SListNode* prve = *pplist;
while (prve->next != pos)
{
prve = prve->next;
}
prve->next = pos->next;
free(pos);
}
}
main.c
#include"work.h"
void TestSList()
{
SListNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushFront(&plist, 0);
SListPopBack(&plist);
SListPopBack(&plist);
SListPopBack(&plist);
SListPrint(plist);
}
void TestSList2()
{
SListNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListNode* pos = SListFind(plist, 1);
if (pos != NULL)
{
SListInsert(&plist, pos, 50);
}
SListPrint(plist);
}
void TestSList3()
{
SListNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListNode* pos = SListFind(plist, 1);
if (pos != NULL)
{
SListErase(&plist, pos);
}
SListPrint(plist);
}
int main()
{
TestSList3();
return 0;
}
结尾
未来还会有双向链表的实现和习题的练习.如果对你有帮助请帮舒文点个赞和收藏谢谢谢谢.
|