顺序表和链表
首先,我们知道,不论是顺序表还是链表,其在逻辑上都是一种线性表,因为它们都是一种线性结构,其实还有一些其他的线性表: 栈,队列,字符串等。
一、顺序表
1.1概念及结构
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。按照内存开辟方式的不同,可以分为静态顺序表和动态顺序表,由于静态顺序表的空间是固定的,限制了使用常见,因此大部分情况下都是使用动态顺序表。

1.2接口实现
我们实现一下动态顺序表
1.2.1顺序表类型创建
//定义顺序表结构
typedef int SLDataType;//为了方便后续修改顺序表的类型
typedef struct SeqList
{
SLDataType* arr;
int size;
int capicity;
}SQL;
1.2.2顺序表初始化
void SeqListInit(SQL* par)
{
assert(par);
par->ar = NULL;
par->size = 0;
par->capicity = 0;
}
1.2.3检查空间,扩容
void SeqListIsFull(SQL* par)
{
if (par->size == par->capicity)
{
int NewCapicity = par->capicity == 0 ? 4 : 2 * par->capicity;
par->ar = (SLDataType*)realloc(par->ar, NewCapicity * sizeof(SLDataType));
par->capicity = NewCapicity;
}
}
1.2.4顺序表尾插
void SeqListPushBack(SQL* par, SLDataType x)
{
assert(par);
SeqListIsFull(par);//判断当前是否需要扩容
par->ar[par->size] = x;
par->size++;
}
1.2.5顺序表尾删
void SeqListPopBack(SQL* par)
{
assert(par);
assert(par->size > 0);
par->size--;//直接将size的指向--即可
}
1.2.6顺序表头插
void SeqListPushFront(SQL* par, SLDataType x)
{
assert(par);
SeqListIsFull(par);
for (int i = par->size-1; i >= 0; i--)
{
par->ar[i+1] = par->ar[i];//先将所有的元素向右移动一位
}
par->ar[0] = x;
par->size++;
}
1.2.7顺序表头删
void SeqListPopFront(SQL* par)
{
assert(par);
assert(par->size > 0);
for (int i = 0; i < par->size - 1; i++)
{
par->ar[i] = par->ar[i + 1];
}
par->size--;
}
1.2.8顺序表查找
int SeqListFind(SQL* par, SLDataType x)
{
assert(par);
for (int i = 0; i < par->size - 1; i++)
{
if (par->ar[i] == x)
return i;//若找到,则返回下标
}
return -1;//若没找到,则返回-1
}
1.2.9顺序表在pos处插入x
void SeqListInsert(SQL* par, size_t pos, SLDataType x)
{
assert(par);
assert(pos <= par->size);
SeqListIsFull(par);
int end = par->size;
while(end > pos)
{
par->ar[end] = par->ar[end-1];
end--;
}
par->ar[pos] = x;
par->size++;
}
1.2.10顺序表在pos处删除x
void SeqListErase(SQL* par, size_t pos)
{
assert(par);
assert(pos <= par->size - 1);
size_t begin = pos + 1;
while(begin < par->size)
{
par->ar[begin-1] = par->ar[begin];
begin++;
}
par->size--;
}
1.2.11顺序表销毁
void SeqListDestory(SQL* par)
{
if (par->ar != NULL)//首先判断是否属于动态开辟的空间
{
free(par->ar);
}
par->capicity = par->size = 0;//同时将capicity和size置0
}
1.2.12顺序表打印
void SeqListPrint(SQL* par)
{
for (int i = 0; i < par->size; i++)
{
printf("%d ", par->ar[i]);
}
printf("\n");
}
二、单链表
2.1概念及结构
由于顺序表在头插时的复杂度较高,申请新空间时需要拷贝数据,效率不高,为了解决这些问题,提出了链表的结构。链表在逻辑上是连续的,但是在物理存储上不是连续的。
typedef int SListDataType;//存储数据类型
typedef struct SListNode
{
SListDataType data;
struct SListNode* next;
}SLNode;//节点数据类型
2.2接口及实现
2.2.1链表的打印
void SListPrint(SLNode* phead)
{
SLNode* tmp = phead;
while (tmp != NULL)
{
printf("%d ", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
2.2.2链表的动态申请
SLNode* SLBuyNode(SListDataType x)
{
SLNode* node = (SLNode*)malloc(sizeof(SLNode));
if (node == NULL)
{
printf("newnode fail");
exit(-1);
}
node->data = x;
node->next = NULL;
return node;
}
2.2.3尾插
void SListPushBack(SLNode** pphead, SListDataType x)
{
assert(pphead);
SLNode* newnode = SLBuyNode(x);
if (*pphead == NULL)//如果是空链表
{
*pphead = newnode;
}
else//如果不是空链表
{
SLNode* tail = *pphead;
//定义一个链表的尾,找到这个尾
while (tail->next != NULL)
{
tail = tail->next;
}
//找到了,需要对原链表尾的指向进行改变,改变为新创建的节点
tail->next = newnode;
}
}
2.2.4头插
void SListPushFront(SLNode** pphead, SListDataType x)
{
assert(pphead);
//申请新的节点
SLNode* newnode = SLBuyNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
2.2.5尾删
void SListPopBack(SLNode** pphead)
{
assert(pphead);
assert(*pphead);
SLNode* tail = *pphead;
if (tail->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLNode* prev = NULL;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
2.2.6头删
void SListPopFront(SLNode** pphead)
{
assert(pphead);
SLNode* cur = *pphead;
cur = (*pphead)->next;
free(*pphead);
*pphead = NULL;
*pphead = cur;
}
2.2.7判断是否为空
bool SListEmpty(SLNode* phead)
{
return phead == NULL;
}
2.2.8获取链表长度
int SListSize(SLNode* phead)
{
int count = 0;
SLNode* cur = phead;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
2.2.9根据给定的值查找节点
SLNode* SListFind(SLNode* phead, SListDataType x)
{
SLNode* cur = phead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
else
{
cur = cur->next;
}
}
return NULL;
}
2.2.10指定位置前插入节点
void SListInsert(SLNode** pphead, SLNode* pos, SListDataType x)
{
assert(pphead);
assert(pos);
SLNode* cur = *pphead;
if (pos == *pphead)
{
SListPushFront(pphead, x);
}
else
{
while (cur->next != pos)
{
cur = cur->next;
}
SLNode* newnode = SLBuyNode(x);
newnode->next = pos;
cur->next = newnode;
}
}
2.2.11指定位置后插入节点
void SListInsertAfter(SLNode* pos, SListDataType x)
{
assert(pos);
SLNode* newnode = SLBuyNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
2.2.12删除pos位置的节点
void SListErase(SLNode** pphead, SLNode* pos)
{
assert(pos);
assert(pphead);
if (pos = *pphead)
{
SListPopFront(pphead);
}
else
{
SLNode* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
2.2.13删除pos位置后的节点
void SListEraseAfter(SLNode* pos)
{
assert(pos);
assert(pos->next);
SLNode* after = pos->next;
pos->next = after->next;
free(after);
after = NULL;
}
2.2.14销毁链表
void SListDesTroy(SLNode** pphead)
{
assert(pphead);
assert(*pphead);
SLNode* cur = *pphead;
while (cur)
{
SLNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
从代码来看,在指定位置后插入/删除节点的复杂度要远低于指定位置前插入/删除,并且不需要头节点,因此链表更适合在指定位置后进行增加和删除操作。
三、带头双向循环链表
3.1概念
带头双向循环链表的特点是有一个哨兵位,不存储数据,而且每个节点都包含下一个节点的指针和上一个节点的指针。而且最后一个节点的下一个节点指向为头节点。相比单链表更加方便。
typedef int ListDataType;
typedef struct ListNode
{
ListDataType data;
ListNode* next;
ListNode* prev;
}ListNode;
3.2接口及实现
3.2.1链表的打印
void ListPrint(ListNode* phead)
{
assert(phead->next);
ListNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
3.2.2链表的初始化
ListNode* ListInit()
{
ListNode* head = ListBuyNode(-1);
head->next = head;
head->prev = head;
return head;
}
3.2.3创建一个节点
ListNode* ListBuyNode(ListDataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = x;
node->next = NULL;
node->prev = NULL;
return node;
}
3.2.4销毁链表
//方式一,需要手动释放head
void ListDestory(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while(cur != phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
}
//方法二,无需手动释放head
void ListDestory(ListNode** pphead)
{
assert(pphead);
ListNode* cur = *pphead;
while(cur != *pphead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
free(*pphead);
*pphead = NULL;
}
3.2.5链表的尾插
void ListPushBack(ListNode* phead, ListDataType x)
{
assert(phead);
ListNode* node = ListBuyNode(x);
ListNode* tail = phead->prev;
tail->next = node;
node->prev = tail;
phead->prev = node;
node->next = phead;
}
3.2.6链表的头插
void ListPushFront(ListNode* phead, ListDataType x)
{
assert(phead);
ListNode* node = ListBuyNode(x);
ListNode* FirstNode = phead->next;
node->next = FirstNode;
FirstNode->prev = node;
node->prev = phead;
phead->next = node;
}
3.2.7判断链表是否为空
bool ListEmpty(ListNode* phead)
{
assert(phead);
if (phead->next == phead)
return true;
else
return false;
}
3.2.8链表的尾删
void ListPopBack(ListNode* phead)
{
assert(phead);
assert(!ListEmpty(phead));
ListNode* tail = phead->prev;
ListNode* sec = tail->prev;
sec->next = phead;
phead->prev = sec;
free(tail);
}
3.2.9链表的头删
void ListPopFront(ListNode* phead)
{
assert(phead);
assert(!ListEmpty(phead));
ListNode* FirstNode = phead->next;
ListNode* SecNode = FirstNode->next;
phead->next = SecNode;
SecNode->prev = phead;
free(FirstNode);
}
3.2.10链表的查找
ListNode* ListFind(ListNode* phead, ListDataType x)
{
assert(phead);
assert(!ListEmpty(phead));
ListNode* cur = phead->next;
while (cur->next != phead)
{
if (cur->data == x)
return cur;
cur = cur->next;
}
}
3.2.11链表的任意位置插入
void ListPushPos(ListNode* phead, ListNode* pos, ListDataType x)
{
assert(phead);
ListNode* newnode = ListBuyNode(x);
ListNode* next = pos->next;
newnode->next = next;
newnode->prev = pos;
pos->next = newnode;
next->prev = newnode;
}
3.2.12链表的任意位置删除
void ListPopPos(ListNode* phead, ListNode* pos)
{
assert(phead);
assert(!ListEmpty(phead));
ListNode* prev, * next;
prev = pos->prev;
next = pos->next;
prev->next = next;
next->prev = prev;
free(pos);
}
本文介绍了顺序表和链表这两种线性表的实现,包括顺序表的动态扩容、尾插、头插、头删、查找和插入删除操作,以及单链表的创建、打印、插入、删除等操作。此外,还讲解了带头双向循环链表的概念和相关操作,如初始化、打印、插入和删除等。
2047

被折叠的 条评论
为什么被折叠?



