一、线性表:
线性表
(
linear list
)
是
n
个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使
用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串
...
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,
线性表在物理上存储时,通常以数组和链式结构的形式存储
二、顺序表:
2.1 概念及结构:
顺序表是用一段
物理地址连续
的存储单元依次存储数据元素的线性结构,一般情况下采用数组存
储。在数组上完成数据的增删查改。
顺序表一般可以分为:
1.
静态顺序表:使用定长数组存储元素
。
2.
动态顺序表:使用动态开辟的数组存储。
2.2 接口实现:
头文件SeqList.h:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size;
int capacity;
}SL;
void SLInit(SL* psl);
void SLDestroy(SL* psl);
void SLPrint(SL* psl);
void SLCheckCapacity(SL* psl);
void SLPushBack(SL* psl, SLDataType x);
void SLPushFront(SL* psl, SLDataType x);
void SLPopBack(SL* psl);
void SLPopFront(SL* psl);
void SLInsert(SL* psl, int pos, SLDataType x);
void SLErase(SL* psl, int pos);
int SLFind(SL* psl, SLDataType x);
源文件SeqList.c
#include"SeqList.h"
void SLInit(SL* psl)
{
assert(psl);
psl->a = NULL;
psl->capacity = 0;
psl->size = 0;
}
void SLDestroy(SL* psl)
{
assert(psl);
if (psl->a)
{
free(psl->a);
psl->capacity = 0;
}
psl->size = 0;
}
void SLCheckCapacity(SL* psl)
{
assert(psl);
if (psl->capacity == psl->size)
{
int newcapacity = psl->capacity == 0 ? 4 : 2 * psl->capacity;
SLDataType* tmp = (SLDataType*)realloc(psl->a, newcapacity*sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc");
return;
}
psl->a = tmp;
psl->capacity = newcapacity;
}
}
void SLPushBack(SL* psl, SLDataType x)
{
assert(psl);
SLCheckCapacity(psl);
psl->a[psl->size++] = x;
}
void SLPrint(SL* psl)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
void SLPushFront(SL* psl, SLDataType x)
{
assert(psl);
SLCheckCapacity(psl);
for (int i = psl->size; i >= 1; i--)
{
psl->a[i] = psl->a[i - 1];
}
psl->a[0] = x;
psl->size++;
}
void SLPopBack(SL* psl)
{
assert(psl);
assert(psl->size > 0);
psl->size--;
}
void SLPopFront(SL* psl)
{
assert(psl);
assert(psl->size > 0);
int begin = 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
++begin;
}
psl->size--;
}
void SLInsert(SL* psl, int pos, SLDataType x)
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
SLCheckCapacity(psl);
int end = psl->size - 1;
while (end >= pos)
{
psl->a[end + 1] = psl->a[end];
--end;
}
psl->a[pos] = x;
psl->size++;
}
void SLErase(SL* psl, int pos)
{
assert(psl);
assert(pos >= 0 && pos < psl->size);
int begin = pos + 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
begin++;
}
psl->size--;
}
int SLFind(SL* psl,SLDataType x)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
{
return i;
}
}
return -1;
}
三、单链表:
3.1 链表的概念及结构:
概念:链表是一种
物理存储结构上非连续
、非顺序的存储结构,数据元素的
逻辑顺序
是通过链表
中的
指针链接
次序实现的 。
3.2 链表的分类 :
实际中链表的结构非常多样,以下情况组合起来就有
8
种链表结构:
1.
单向或者双向
2.
带头或者不带头
3.
循环或者非循环
虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:
1.
无头单向非循环链表:
结构简单
,一般不会单独用来存数据。实际中更多是作为
其他数据结
构的子结构
,如哈希桶、图的邻接表等等。另外这种结构在
笔试面试
中出现很多。
2.
带头双向循环链表:
结构最复杂
,一般用在单独存储数据。实际中使用的链表数据结构,都
是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带
来很多优势,实现反而简单了,后面我们代码实现了就知道了。
3.3 链表的实现:
1、无头+单向+非循环链表增删查改实现:
头文件SList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SListNode
{
SLDataType val;
struct SListNode* next;
}SLNode;
void SLTPrint(SLNode* phead);
void SLTPushBack(SLNode** pphead, SLDataType x);
void SLTPushFront(SLNode** pphead, SLDataType x);
SLNode* CreatNode(SLDataType x);
void SLTPopBack(SLNode** pphead);
void SLTPopFront(SLNode** pphead);
SLNode* SLTFind(SLNode* phead,SLDataType x);
void SLTInsert(SLNode** pphead, SLNode* pos, SLDataType x);
void SLTInsertAfter(SLNode* pos, SLDataType x);
void SLTErase(SLNode** pphead, SLNode* pos);
void SLTDestroy(SLNode** pphead);
源文件SList.c
#include"SList.h"
void SLTPrint(SLNode* phead)
{
SLNode* cur = phead;
while (cur)
{
printf("%d->", cur->val);
cur = cur->next;
}
printf("NULL\n");
}
void SLTPushBack(SLNode** pphead, SLDataType x)
{
assert(pphead);
SLNode* newnode = CreatNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
return;
}
SLNode* tail = *pphead;
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
SLNode* CreatNode(SLDataType x)
{
SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
if (newnode == NULL)
{
perror("malloc");
exit(-1);
}
newnode->next = NULL;
newnode->val = x;
return newnode;
}
void SLTPushFront(SLNode** pphead, SLDataType x)
{
SLNode* newnode = CreatNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLTPopBack(SLNode** pphead)
{
assert(*pphead);
if ((*pphead)->next == NULL)//一个节点
{
free(*pphead);
*pphead = NULL;
}
else
{
SLNode* prev = NULL;
SLNode* tail = *pphead;
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
void SLTPopFront(SLNode** pphead)
{
assert(*pphead);
SLNode* tmp = (*pphead)->next;
free(*pphead);
*pphead = tmp;
}
SLNode* SLTFind(SLNode* phead, SLDataType x)
{
assert(phead);
SLNode* pcur = phead;
while (pcur)
{
if (pcur->val == x)
{
return pcur;
}
pcur = pcur->next;
}
return NULL;
}
void SLTInsert(SLNode ** pphead,SLNode* pos, SLDataType x)
{
assert(pphead);
assert(pos);
assert(*pphead);
SLNode* newnode = CreatNode(x);
if (pos == *pphead)
{
newnode->next = *pphead;
*pphead = newnode;
return;
}
SLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
newnode->next = pos;
prev->next = newnode;
}
void SLTInsertAfter(SLNode* pos, SLDataType x)
{
assert(pos);
SLNode* newnode = CreatNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SLTErase(SLNode** pphead, SLNode* pos)
{
assert(pphead);
assert(*pphead);
assert(pos);
if (pos == *pphead)
{
*pphead = (*pphead)->next;
free(pos);
pos = NULL;
return;
}
SLNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
void SLTDestroy(SLNode** pphead)
{
assert(pphead);
SLNode* pcur = *pphead;
while (pcur)
{
SLNode* next = pcur->next;
free(pcur);
pcur = next;
}
*pphead = NULL;
}
2.、带头+双向+循环链表增删查改实现:
头文件DLis.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 带头+双向+循环链表增删查改实现
typedef int LTDataType;
typedef struct ListNode
{
LTDataType _data;
struct ListNode* _next;
struct ListNode* _prev;
}ListNode;
// 创建返回链表的头结点.
ListNode* ListCreate();
// 创建新节点
ListNode* CreatLTNode(LTDataType x);
// 双向链表销毁
void ListDestory(ListNode* pHead);
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);
源文件DList.c
#include"DList.h"
// 创建返回链表的头结点.
ListNode* ListCreate()
{
ListNode* phead;
phead = CreatLTNode(-1);
phead->_next = phead;
phead->_prev = phead;
return phead;
}
// 创建新节点
ListNode* CreatLTNode(LTDataType x)
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("newnode malloc");
}
newnode->_data = x;
newnode->_next = NULL;
newnode->_prev = NULL;
return newnode;
}
// 双向链表销毁
void ListDestory(ListNode* pHead)
{
ListNode* pdel = pHead->_next;
while (pdel != pHead)
{
ListNode* next = pdel->_next;
free(pdel);
pdel = next;
}
pHead->_next = pHead;
pHead->_prev = pHead;
}
// 双向链表打印
void ListPrint(ListNode* pHead)
{
assert(pHead);
ListNode* pcur = pHead->_next;
printf("哨兵位 <->");
while (pcur != pHead)
{
printf("%d <-> ", pcur->_data);
pcur = pcur->_next;
}
printf("哨兵位\n");
}
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* ptail = pHead->_prev;
ListNode* newnode = CreatLTNode(x);
newnode->_next = pHead;
newnode->_prev = ptail;
ptail->_next = newnode;
pHead->_prev = newnode;
}
// 双向链表尾删
void ListPopBack(ListNode* pHead)
{
assert(pHead);
assert(pHead->_next != pHead);
ListNode* ptail = pHead->_prev;
pHead->_prev = ptail->_prev;
ptail->_prev->_next = pHead;
free(ptail);
ptail = NULL;
}
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x)
{
assert(pHead);
ListNode* newnode = CreatLTNode(x);
newnode->_next = pHead->_next;
newnode->_prev = pHead;
pHead->_next->_prev = newnode;
pHead->_next = newnode;
}
// 双向链表头删
void ListPopFront(ListNode* pHead)
{
assert(pHead);
assert(pHead->_next != pHead);
ListNode* phead = pHead->_next;
pHead->_next = phead->_next;
phead->_next->_prev = pHead;
free(phead);
phead = NULL;
}
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x)
{
ListNode* pcur = pHead->_next;
while (pcur != pHead)
{
if (pcur->_data == x)
{
return pcur;
}
pcur = pcur->_next;
}
return NULL;
}
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x)
{
assert(pos);
ListNode* newnode = CreatLTNode(x);
newnode->_prev = pos->_prev;
newnode->_next = pos;
pos->_prev->_next = newnode;
pos->_prev = newnode;
}
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos)
{
pos->_next->_prev = pos->_prev;
pos->_prev->_next = pos->_next;
free(pos);
pos = NULL;
}