0
点赞
收藏
分享

微信扫一扫

JavaEE笔记

1 顺序表

1.1 静态顺序表结构体定义

typedef int ElemDataSL;

typedef struct SequeList {
ElemDataSL arr[100];
int size;
}SL;

例子:通讯录(C语言实现)-CSDN博客

1.2 动态顺序表结构体定义

typedef int ElemDataSL;

typedef struct SequeList {
ElemDataSL* arr;
int size;
int capatity;
}SL;

1.2.1 接口声明

//初始化
void InitSequeList(SL* s);
//尾插
void SequeListPushBack(SL* s,ElemDataSL num);
//尾删
void SequeListPopBack(SL* s);
//头插
void SequeListPushFront(SL* s, ElemDataSL num);
//头删
void SequeListPopFront(SL* s);
//pos处插
void SequeListPushInsert(SL* s,ElemDataSL num,int pos);
//pos处删
void SequeListPopInsert(SL* s,int pos);
//显示
void SequeListShowData(SL* s);
// 顺序表删除pos位置的值
void SequeListErase(SL*s, size_t pos);
// 顺序表销毁
void SequeListDestory(SL* s);

1.2.2 初始化

//初始化
void InitSequeList(SL* s) {
assert(s);
s->arr = (ElemDataSL*)malloc(DefaultData * sizeof(ElemDataSL));
s->capatity = DefaultData;
s->size = 0;
}

1.2.3 尾插

//尾插
void SequeListPushBack(SL* s,ElemDataSL num) {
assert(s);
AddCapatity(s);
s->arr[s->size] = num;
s->size++;
}

 1.2.4 尾删

//尾删
void SequeListPopBack(SL* s)
{
assert(s);
s->size--;
}

1.2.5 头插

//头插
void SequeListPushFront(SL* s,ElemDataSL num) {
AddCapatity(s);
for (int i = s->size-1; i >=0; i--)
{
s->arr[i + 1] = s->arr[i];
}
s->arr[0] = num;
s->size++;
}

1.2.6 头删

//头删
void SequeListPopFront(SL* s)
{
for (int i = 0; i <= s->size - 1; i++)
{
s->arr[i] = s->arr[i + 1];
}
s->size--;
}

1.2.7 中间插入

//pos处插
void SequeListPushInsert(SL* s,ElemDataSL num,int pos)
{
AddCapatity(s);
for (int i = s->size - 1; i >= pos-1; i--)
{
s->arr[i + 1] = s->arr[i];
}
s->arr[pos-1] = num;
s->size++;
}

1.2.8 中间删除

//pos处删
void SequeListPopInsert(SL* s, int pos) {
for (int i = pos-1; i <= s->size - 1; i++)
{
s->arr[i] = s->arr[i + 1];
}
s->size--;
}

1.2.9 打印顺序表

//显示
void SequeListShowData(SL* s)
{
assert(s);
for (int i = 0; i < s->size; i++)
{
printf("%d ", s->arr[i]);
}
printf("\n");
}

1.3 顺序表销毁

// 顺序表销毁
void SequeListDestory(SL* s) {
free(s->arr);
s->arr = NULL;
}

2 链表

2.1 单链表

2.2 接口声明

typedef int SLDATATYPE;
typedef struct SListNode {
int data;
struct SListNode* next;
}SListNode;
//尾插
void SListPushBack(SListNode** pphead, SLDATATYPE num);
//尾删
void SListPopBack(SListNode* phead);
//头插
void SListPushFront(SListNode** pphead, SLDATATYPE num);
//头删
void SListPopFront(SListNode* phead);
//查找
SListNode* SListFind(SListNode* phead, SLDATATYPE num);
//中间后插
void SListInsertAfter(SListNode* pos, SLDATATYPE num);
//中间删
void SListEraseAfter(SListNode* pos);
//打印
void SListPrint(SListNode* phead);
//销毁链表
void SListDistory(SListNode** phead);

2.3 尾插

//尾插
void SListPushBack(SListNode* *pphead, SLDATATYPE num) {


SListNode* NewNode = BuySListNode(num);
if (*pphead == NULL)
{
*pphead= NewNode;
}
else
{ SListNode* tail = *pphead;
while (tail->next!=NULL)
{
tail = tail->next;
}
tail->next= NewNode;
}
}

2.4 尾删

//尾删
void SListPopBack(SListNode** phead)
{
SListNode* tail = *phead;
SListNode* prev = tail;
if (*phead == NULL)
{
return;
}
else if ((*phead)->next == NULL)
{
free(*phead);
*phead = NULL;
}
else
{
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}


}

2.5 头插

//头插
void SListPushFront(SListNode** pphead, SLDATATYPE num)
{
SListNode* newNode = BuySListNode(num);

if ((*pphead) == NULL)
{
*pphead = newNode;
}
else
{
newNode->next = *pphead;
}
*pphead = newNode;
}

2.6 头删

//头删
void SListPopFront(SListNode* *pphead)
{
SListNode* head = *pphead;
if (head == NULL)
{
return;
}
else if (head->next == NULL)
{
*pphead = NULL;
}
else
{

(*pphead) = (*pphead)->next;
free(head);
}


}

2.7 查找

SListNode* SListFind(SListNode* phead, SLDATATYPE num) {
SListNode* ptr = phead;
while (ptr)
{
if (ptr->data == num)
{
printf("%d找到了!\n",ptr->data);
return ptr;
}
ptr = ptr->next;
}
printf("%d没找到!\n",num);
return NULL;
}

2.8 中间后插

void SListInsertAfter(SListNode* *pos, SLDATATYPE num)
{
SListNode* newNode = BuySListNode(num);
newNode->next = (*pos)->next;
(*pos)->next = newNode;
}

2.9 中间后删

void SListEraseAfter(SListNode* pos)
{
assert(pos);
assert(pos->next);
SListNode* next = pos->next;
pos->next = next->next;
free(next);
}

3 打印

//打印
void SListPrint(SListNode* phead)
{
SListNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}

3.1 销毁链表

void SListDistory(SListNode** phead)
{
assert(*phead);
SListNode* next = (*phead)->next;
while (next)
{
free(*phead);
*phead = next;
next = next->next;
}
free(*phead);
*phead = NULL;
}

2.2 双链表

2.2.1 接口声明

typedef int ListData;
typedef struct ListNode
{
ListData data;
struct ListNode* next;
struct ListNode* prev;
}ListNode;
//链表创建
ListNode* ListCreate();
//尾插
void ListPushBack(ListNode* phead, ListData x);
//尾删
void ListPopBack(ListNode* phead);
//头插
void ListPushFront(ListNode* phead, ListData x);
//头删
void ListPopFront(ListNode* phead);
//查找
ListNode* ListFind(ListNode* phead, ListData x);
//中间插
void ListInsert(ListNode* pos,ListData x);
//中间删
void ListErase(ListNode* pos);
//显示
void ListPrint(ListNode* phead);
//清空链表
void ListClear(ListNode** phead);
//销毁链表
void ListDestory(ListNode* phead);

2.2.2 创建新节点 


ListNode* BuyListNode(ListData x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
node->data = x;
node->next = NULL;
node->prev = NULL;
return node;
}

2.2.3 双链表创建

ListNode* ListCreate() {
ListNode* phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}

2.2.4 尾插 

//尾插
void ListPushBack(ListNode* phead, ListData x)
{
assert(phead);
ListNode* tail = phead->prev;
ListNode* newNode = BuyListNode(x);
tail->next = newNode;
newNode->prev = tail;
newNode->next = phead;
phead->prev = newNode;
}

 2.2.5 尾删

//尾删
void ListPopBack(ListNode* phead)
{
assert(phead->next != phead);
/*ListNode* tail = phead->prev;
ListNode* tailPrev = tail->prev;
tailPrev->next = phead;
phead->prev = tailPrev;*/

ListErase(phead->prev);
}

2.2.6 头插

//头插
void ListPushFront(ListNode* phead, ListData x)
{
ListNode* newNode = BuyListNode(x);
ListNode* pheadNext = phead->next;
phead->next = newNode;
newNode->prev = phead;
newNode->next = pheadNext;
pheadNext->prev = newNode;
}

2.2.7 头删 

//头删
void ListPopFront(ListNode* phead)
{
ListNode* pheadNext = phead->next;
phead->next = pheadNext->next;
pheadNext->next->prev = phead;
free(pheadNext);
}

2.2.8 查找

//查找
ListNode* ListFind(ListNode* phead, ListData x)
{
ListNode* pos = NULL;
ListNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
pos = cur;
break;

}
cur = cur->next;
}
return pos;
}

2.2.9 中间插

//中间插
void ListInsert(ListNode* pos, ListData x)
{
assert(pos);
/*ListNode* newNode = BuyListNode(x);
ListNode* posPrev = pos->prev;
newNode->next = pos;
pos->prev = newNode;
posPrev->next = newNode;
newNode->prev = posPrev;*/

ListPushFront(pos->prev, x);
}

2.3 中间删

//中间删
void ListErase(ListNode* pos)
{
ListNode* posPrev = pos->prev;
ListNode* posNext = pos->next;
posPrev->next = posNext;
posNext->prev = posPrev;
free(pos);
}

2.3.1 显示

//显示
void ListPrint(ListNode* phead)
{
assert(phead);
ListNode* tail = phead->next;

while (tail != phead)
{
printf("%d ", tail->data);
tail = tail->next;
}

printf("\n");
}

2.3.2 清空链表

void ListClear(ListNode** phead)
{
ListNode* cur = (*phead)->next;
while (cur != *phead)
{
ListNode* next = cur->next;
free(cur);
cur = next;
}
(*phead)->next = *phead;
(*phead)->prev = *phead;
}

2.3.3 销毁链表

//销毁链表
void ListDestory(ListNode** phead)
{
ListClear(phead);
free(*phead);
}

 3 顺序表和链表的区别

不同点顺序表链表
存储空间上物理上一定连续逻辑上连续,物理上不一定连续
随机访问支持O(1)不支持O(N)
任意位置插入或者删除元素可能需要搬移元素,效率低O(N)只需修改指针指向
插入动态顺序表,空间不够时需要扩容没有容量概念
应用场景元素高效存储+频繁访问任意位置插入和删除频繁
缓存利用率
举报

相关推荐

0 条评论