1.线性表:是n个具有相同特性的数据元素的有限序列
常见的线性表:顺序表、链表、栈、队列、字符串
2.顺序表:是用一段物理地址连续的存储单元依次存储数据元素的线性 结构,一 般情况下采用数组存储。在数组 上完成数据的删 查改。
1. 顺序表一般可以分为: 1. 静态顺序表:使用定长数组存储元素。
2. 动态顺序表:使用动态开辟的数组存储。
2.关于顺序表的思考:1. 中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的 消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。
顺序表的动态存储
#include "SeqList.h"
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListDestroy(SeqList* ps)
{
free(ps->a);
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SeqListCheckCapicity(SeqList* ps)
{
if (ps->size == ps->capacity)
{
size_t newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = (int*)realloc(ps->a,sizeof(SLDateType*) * newcapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity = newcapacity;
}
}
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
SeqListCheckCapicity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
SeqListCheckCapicity(ps);
int end = ps->size;
while (end >=0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin-1] = ps->a[begin];
begin++;
}
ps->size--;
}
void SeqListPopBack(SeqList* ps)
{
assert(ps->size > 0);
ps->size--;
//if (ps->size > 0)
//{
// ps->size--;//把有效数具个数减少一个就相当于删除最后一个数据
//}
}
//顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
return i;
}
return -1;
}
//顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos <= ps->size);
SeqListCheckCapicity(ps);
int end = ps->size;
while (end >= pos)
{
ps->a[end ] = ps->a[end-1];
end--;
}
ps->a[pos] = x;
ps->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(pos < ps->size);
int begin = pos;
while (begin < ps->size)
{
ps->a[begin] = ps->a[begin+1];
begin++;
}
ps->size--;
}
3.链表:一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链 接次序实现的 。
逻辑连续,物理不一定连续,从栈上申请
1.链表的分类
a. 单向或者双向
b.带头或者不带头
c.. 循环或者非循环
以上三种分类各自排列组合出现八种类型最常用的有两种,单向不带头非循环 和双向 带头循环
无头单向非循环链表:结构简单,是作为其他数据结构的子结 构
#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
void SListPrint(SListNode* phead)
{
SListNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
SListNode* BuySListNode(SLDataType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
printf("malloc fail");
exit(-1);
}
else
{
newnode->data = x;
newnode->next = NULL;
}
return newnode;
}
void SListPushBack(SListNode** pphead, SLDataType x)
{
SListNode* newnode = BuySListNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SListNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPushFront(SListNode** pphead, SLDataType x)
{
SListNode* newnode = BuySListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPopBack(SListNode** pphead)
{
assert(pphead);
if (*pphead == NULL)
{
return;
}
else if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SListNode* prev = NULL;
SListNode* tail = *pphead;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
void SListPopFront(SListNode** pphead)
{
assert(pphead);
assert(*pphead);
if (*pphead != NULL)
{
SListNode* next = ( * pphead)->next;
free(*pphead);
*pphead = next;
}
}
SListNode* SListFind(SListNode* phead, SLDataType x)
{
SListNode* cur = phead;
while (cur != NULL)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void SListInsert(SListNode** pphead, SListNode* pos, SLDataType x)
{
assert(pphead);
assert(pos);
if (*pphead == pos)
{
SListNode* newnode = BuySListNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
else
{
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
SListNode* newnode= BuySListNode(x);
prev->next = newnode;
newnode->next = pos;
}
}
void SListErase(SListNode** pphead, SListNode* pos)
{
assert(pphead);
assert(pos);
assert(*pphead);
if (*pphead ==pos)
{
*pphead = pos->next;
free(pos);
pos = NULL;
}
else
{
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
void SListInsertAfter(SListNode* pos, SLDataType x)
{
assert(pos);
SListNode* newnode = BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListEraseAfter(SListNode* pos)
{
assert(pos);
//SListNode* next = pos->next;
//if (next)
//{
// pos->next = next->next;
// free(next);
// next = NULL;
//}
if (pos->next)
{
pos->next->next = pos->next;
free(pos->next);
pos->next = NULL;
}
}
void SListDestroy(SListNode** pphead)
{
assert(pphead);
SListNode* cur = *pphead;
while (cur)
{
SListNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}
带头双向循环链表:结构最复杂使用代码实现以后会发现结构会带来很多优势,实现反而 简单
#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
ps->a = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
if (ps->a == NULL)
{
printf("realloc fail");
exit(-1);
}
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
--ps->top;
}
bool StackEmpty(ST* ps)
{
//if (ps->top > 0)
// return false;
//else
// return true;
return ps->top == 0;//判断表达式在这为0返回true 非0返回false
}
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}
STDataType StackTop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}