C语言---栈、队列、堆

栈实现

https://www.cnblogs.com/maluning/p/7979453.html

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*定义节点结构*/
typedef struct Node{
    int value;
    struct Node *next;
}NODE;

/*定义栈结构*/
typedef struct Stack{
    NODE *top;
    NODE *tail;
}STACK;

/*初始化栈*/
static void InitStack(struct Stack *s);
/*给栈增加元素*/
static void PushStack(struct Stack *s,int value);
/*弹出栈顶元素*/
static void PopStack(struct Stack *s,int *value);
/*栈是否为空*/
static int IsEmpty(struct Stack *s);
/*遍历栈*/
static void TraverseStack(struct Stack *s);
/*定义栈清空函数*/
static void ClearStack(struct Stack *s);

int main()
{
    int value = 0;
    STACK stack = {NULL};
    
    InitStack(&stack);
    PushStack(&stack,10);
    PushStack(&stack,20);
    PopStack(&stack,&value);
    PushStack(&stack,30);
    PushStack(&stack,40);
    PushStack(&stack,50);
    
    TraverseStack(&stack);
    ClearStack(&stack);
    PopStack(&stack,&value);
    
    return 0;
}

/*初始化栈*/
static void InitStack(struct Stack *s)
{
    NODE *tmp = (NODE *)malloc(sizeof(NODE));
    if (NULL == tmp){
        printf("apply memory fail!!!\n");
        return;
    }
    
    s->top = tmp;
    s->tail = tmp;
    tmp->next = NULL;
    printf("init stack success!!!\n");
}

/*定义入栈函数*/
static void PushStack(struct Stack *s,int value)
{
    NODE *tmp = (NODE *)malloc(sizeof(NODE));
    if (NULL == tmp){
        printf("apply memory fail!!!\n");
        return;
    }
    
    tmp->next = s->top;
    s->top = tmp;
    tmp->value = value;
}

/*定义出栈函数*/
static void PopStack(struct Stack *s,int *value)
{
    NODE *tmp = NULL;
    
    if (s->tail == s->top){
        printf("stack is empty!!!\n");
        return;
    }
    
    tmp = s->top;
    (*value) = tmp->value;
    s->top = s->top->next;
    free(tmp);
    tmp = NULL;
    return;
}

/*判断栈是否为空*/
static int IsEmpty(struct Stack *s)
{
    if (s->top == s->tail){
        return 0;
    }else{
        return 1;
    }
}

/*遍历栈*/
static void TraverseStack(struct Stack *s)
{
    if (!IsEmpty(s)){
        printf("stack is empty!!\n");
    }
    
    NODE *tmp = s->top;
    
    printf("value is ");
    while (tmp->next){
        printf("%d ",tmp->value);
        tmp=tmp->next;
    }
    printf("\n");
    
    return;
}

/*定义栈清空函数*/
static void ClearStack(struct Stack *s)
{
    if (!IsEmpty(s)){
        printf("stack is empty!!\n");
    }

    NODE *P = s->top;
    NODE *Q = NULL;
    
    while(P != s->tail){
        Q = P;
        P = P->next;
        free(Q);
    }
    
    s->top = s->tail;
    printf("clear success!!!\n");
}

队列实现

https://www.cnblogs.com/maluning/p/7979456.html

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node{
    int value;
    struct Node *next;
}NODE;

typedef struct Queue{
    struct Node *top;
    struct Node *tail;
}QUEUE;

/*定义队列初始化函数*/
void InitQueue(struct Queue *Q);
/*定义入队函数*/
void InsertQueue(struct Queue *Q,int value);
/*定义出队函数*/
void DeleteQueue(struct Queue *Q,int *value);
/*判断队列是否为空*/
int IsEmptyQueue(struct Queue *Q);
/*清空队列*/
void ClearQueue(struct Queue *Q);
/*遍历队列*/
void TraverseQueue(struct Queue *Q);

int main()
{
    int value = 0;
    struct Queue test = {NULL};
    
    InitQueue(&test);
    InsertQueue(&test,10);
    InsertQueue(&test,20);
    InsertQueue(&test,30);
    DeleteQueue(&test,&value);
    DeleteQueue(&test,&value);
    printf("v:%d\n",value);
    //ClearQueue(&test);
    //DeleteQueue(&test,&value);
    TraverseQueue(&test);
    //printf("the top is %d\n",test.top->next->value);
}

/*定义队列初始化函数*/
void InitQueue(struct Queue *Q)
{
    struct Node *tmp = NULL;
    tmp = (struct Node *)malloc(sizeof(struct Queue));
    if (NULL == tmp){
        printf("apply memory error!!!\n");
        return;
    }
    
    Q->top = Q->tail = tmp;
    tmp->next = NULL;
    return;
}

/*定义入队函数*/
void InsertQueue(struct Queue *Q,int value)
{
    struct Node *tmp = NULL;
    tmp = (struct Node *)malloc(sizeof(struct Queue));
    if (NULL == tmp){
        printf("apply memory error!!!\n");
        return;
    }
        
    tmp->value = value;
    tmp->next =NULL;
    Q->tail->next = tmp;
    Q->tail = tmp;
}

/*判断队列是否为空*/
int IsEmptyQueue(struct Queue *Q)
{
    if (Q->top == Q->tail){
        return 0;
    }else{
        return 1;
    }
}

/*定义出队函数*/
void DeleteQueue(struct Queue *Q,int *value)
{
    struct Node *tmp = NULL;
    
    if (!IsEmptyQueue(Q)){
        printf("Queue is empty!!!\n");
        return;
    }
    
    tmp = Q->top->next;
    Q->top->next=tmp->next;
    *value = tmp->value;
    if (tmp == Q->tail){
        Q->tail = Q->top;
    }
    free(tmp);
    tmp =NULL;
}

/*清空队列*/
void ClearQueue(struct Queue *Q)
{
    struct Node *tmp = NULL;
    struct Node *T = NULL;
    
    T = Q->top->next;
    
    if(T != NULL){
        tmp = T;
        T = T->next;
        free(tmp);
    }
    //尾指针指向头指针
    Q->tail = Q->top;
    //头指针的next指向为NULL
    Q->top->next = NULL;
}

/*遍历队列*/
void TraverseQueue(struct Queue *Q)
{
    if (!IsEmptyQueue(Q)){
        printf("Queue is empty!!!\n");
        return;
    }    
    
    struct Node *P = Q->top->next;
    while (P){
        printf("the value is %d\n",P->value);
        P=P->next;
    }
    return;
}

堆实现(最大堆)

堆排序

https://www.cnblogs.com/Java3y/p/8639937.html

堆实现参考

https://blog.csdn.net/carl_2018/article/details/90679149

图解堆操作过程

https://blog.csdn.net/u011068702/article/details/52712634

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Heap{
    int *array;
    int size;
    int capacity;
}HEAP;


/*堆初始化*/
void HeapInit(struct Heap *hp,int *a,int n);
/*给堆添加数据*/
void HeapPush(struct Heap *hp,int data);
/*堆中踢出数据*/
void HeapPop(struct Heap *hp);
/*打印*/
void HeapPrint(struct Heap* hp);
/*堆排序*/
void HeapSort(struct Heap* hp);
/*取堆头*/
int HeapTop(struct Heap *hp);
/*获取堆大小*/
int HeapSize(struct Heap *hp);
/*堆判空*/
int HeapEmpty(struct Heap *hp);
/*销毁堆*/
void HeapDestory(struct Heap *hp);


int main()
{
 	int a[10] = { 11, 14, 9, 23, 34, 16, 24, 65, 52, 45 };
	HEAP hp;
	HeapInit(&hp, a, sizeof(a) / sizeof(int));   
    HeapPush(&hp, 70);
    HeapPop(&hp);
    HeapPrint(&hp);
    
    HeapSort(&hp);
    HeapPrint(&hp);
    printf("\n");
    printf("the top is %d\n",HeapTop(&hp));
}

void HeapPrint(struct Heap* hp)
{
    int i = 0;
    
	for (i = 0; i < hp->size; ++i)
	{
		printf("%d ", hp->array[i]);
	}
	printf("\n");
}

void Swap(int *p1,int *p2)
{
    int tmp = *p1;
    
    *p1=*p2;
    *p2=tmp;
    
    return;
}

/*建堆*/
void AdjustDown(int *a,int n,int root)
{
    int parent = root;
    int child = parent*2+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 = parent*2+1;
        }else{
            break;
        }
    }
}

/*堆排序*/
void HeapSort(struct Heap* hp)
{
    int end = hp->size;
    int *a = hp->array;
    
    end--;
    while(end > 0){
        Swap(&a[0],&a[end]);
        AdjustDown(a,end,0);
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值