本章会将常用的数据结构以及基础排序算法一一实现,方便更加深刻的理解算法与数据结构的魅力所在
单链表
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode* next;
}ListNode;
void Print(ListNode* plist)
{
ListNode* cur = plist;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
ListNode* BuyListNode(DataType x)
{
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (node == NULL)
{
printf("malloc fail\n");
exit(-1);
}
node->data = x;
node->next = NULL;
return node;
}
void PushFront(ListNode** pplist, DataType x)
{
ListNode* newnode = BuyListNode(x);
newnode->next = *pplist;
*pplist = newnode;
}
void PushBack(ListNode** pplist, DataType x)
{
ListNode* newnode = BuyListNode(x);
if (*pplist == NULL)
{
*pplist = newnode;
}
else
{
ListNode* tail = *pplist;
while (tail != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void InsertAfter(ListNode* pos, DataType x)
{
assert(pos);
ListNode* newnode = BuyListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void InsertBefor(ListNode**pplist, ListNode* pos, DataType x)
{
assert(pos);
ListNode* newnode = BuyListNode(x);
if (pos == *pplist)
{
newnode->next = pos;
*pplist = newnode;
}
else
{
ListNode* prev = *pplist;
while (prev->next != pos)
{
prev = prev->next;
}
newnode->next = prev->next;
prev->next = newnode;
}
}
void PopFront(ListNode** pplist)
{
if (*pplist == NULL)
{
return;
}
else
{
ListNode* tmp = *pplist;
*pplist = (*pplist)->next;
free(tmp);
tmp = NULL;
}
}
void PopBack(ListNode** pplist)
{
if (*pplist == NULL)
{
return;
}
else if ((*pplist)->next == NULL)
{
free(*pplist);
*pplist = NULL;
}
else
{
ListNode* prev = *pplist;
ListNode* tail = (*pplist)->next;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prev->next = NULL;
}
}
void ErasetAfter(ListNode* pos)
{
assert(pos);
if (pos->next == NULL);
return;
ListNode* after = pos->next;
pos->next = after->next;
free(after);
after = NULL;
}
void ErasetCur(ListNode** pplist, ListNode* pos)
{
assert(pos);
if (pos == *pplist)
{
*pplist = pos->next;
free(pos);
pos = NULL;
}
else
{
ListNode* prev = *pplist;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}
ListNode* Find(ListNode* plist, DataType x)
{
ListNode* cur = plist;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Modify(ListNode* pos, DataType x)
{
pos->data = x;
}
栈
typedef int DataType;
typedef struct Stack
{
DataType* a;
int top;
int capacity;
}Stack;
void Init(Stack* pst)
{
assert(pst);
pst->a = (DataType*)malloc(sizeof(DataType) * 4);
pst->top = 0;
pst->capacity = 4;
}
void Destory(Stack* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = 0;
pst->capacity = 0;
}
void Push(Stack* pst, DataType x)
{
assert(pst);
if (pst->top == pst->capacity)
{
DataType* tmp = (DataType*)realloc(pst->a, sizeof(DataType)*pst->capacity * 2);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
pst->a = tmp;
pst->capacity *= 2;
}
pst->a[pst->top] = x;
pst->top++;
}
void Pop(Stack* pst)
{
assert(pst);
assert(!Empty(pst));
pst->top--;
}
DataType Top(Stack* pst)
{
assert(pst);
assert(!Empty(pst));
return pst->a[pst->top - 1];
}
bool Empty(Stack* pst)
{
assert(pst);
return pst->top == 0;
}
int Size(Stack* pst)
{
assert(pst);
return pst->top;
}
队列
typedef int DataType;
typedef struct QListNode
{
struct QListNode* next;
DataType data;
}QListNode;
typedef struct Queue
{
QListNode* head;
QListNode* taile;
}Queue;
void Init(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->taile = NULL;
}
void Destroy(Queue* pq)
{
assert(pq);
QListNode* cur = pq->head;
while (cur)
{
QListNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = NULL;
pq->taile = NULL;
}
void Push(Queue* pq, DataType x)
{
assert(pq);
QListNode* newnode = (QListNode*)malloc(sizeof(QListNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
if (pq->head = NULL)
{
pq->head = pq->taile = newnode;
}
else
{
pq->taile->next = newnode;
pq->taile = newnode;
}
}
void Pop(Queue* pq)
{
assert(pq);
assert(!Empty()(pq));
if(pq->head->next == NULL)
{
free(pq->head);
pq->head = NULL;
pq->taile = NULL;
}
else
{
QListNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
DataType Front(Queue* pq)
{
assert(pq);
assert(!Empty(pq));
return pq->head->data;
}
DataType Back(Queue* pq)
{
assert(pq);
assert(!Empty(pq));
return pq->taile->data;
}
bool Empty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
int Size(Queue* pq)
{
assert(pq);
QListNode* cur = pq->head;
int count = 0;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}
堆解topk
void Swap(int* x, int* y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
void AdjustDown(int* a, int n, int parent)
{
int child = 2 * parent + 1;
while (child < n)
{
if (child + 1 < n&&a[child + 1] < a[child])
{
child++;
}
if (a[child] < a[parent])
{
Swap(&a[child], &a[parent]);
parent = child;
child = 2 * parent + 1;
}
else
{
break;
}
}
}
int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize)
{
*returnSize = k;
if (k == 0)
return NULL;
int i = 0;
int* retArr = (int*)malloc(sizeof(int)*k);
for (i = 0; i < k; i++)
{
retArr[i] = arr[i];
}
for (i = (k - 1 - 1) / 2; i >= 0; i--)
{
AdjustDown(retArr, k, i);
}
for (i = k; i < arrSize; i++)
{
if (arr[i]>retArr[0])
{
retArr[0] = arr[i];
}
AdjustDown(retArr, k, 0);
}
return retArr;
}