赞
踩
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
-
- void heapify(int arr[], int i, int n)//建堆算法
- {
- int nchild;
- int tmp;
- for (; 2 * i + 1< n; i = nchild)
- {
- nchild = 2 * i + 1;
- if (nchild<n - 1 && arr[nchild + 1]>arr[nchild])
- nchild++;
- if (arr[i] < arr[nchild])
- {
- tmp = arr[i];
- arr[i] = arr[nchild];
- arr[nchild] = tmp;
- }
- }
- }
- void heapsort(int arr[], int n)
- {
- int i;
- for (i = (n - 1) / 2; i >= 0; i--)
- {
- heapify(arr, i, n);
- }
- for (i = n - 1; i > 0; i--)
- {
- //把第一个元素与当前最后一元素交换
- arr[i] = arr[0] ^ arr[i];
- arr[0] = arr[0] ^ arr[i];
- arr[i] = arr[0] ^ arr[i];
- //不断缩小调整heap的范围,每一次调整完毕保证第一个元素是当前序列的最大值
- heapify(arr, 0, i);
- }
- }
- 栈的顺序存储
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define SIZE 100
- typedef struct stack
- {
- int data[100];
- int top;
- }lstack;
- int is_empty(lstack* p)//判断栈是否为空
- {
- if (p->top == -1)
- {
- return 1;
- }
- return 0;
- }
- int get_top_lstack(lstack* p)//返回栈顶元素
- {
- if (!is_empty(p))
- {
- return p->data[p->top];
- }
- return false;
- }
-
- int pop(lstack* p)//弹出栈顶元素
- {
- if (!is_empty(p)) { return p->data[p->top--]; }
- return false;
- }
-
- lstack* push(lstack* p,int val)//元素入栈
- {
- if (p->top >= SIZE - 1)
- {
- return NULL;
- }
- p->top++;
- p->data[p->top] = val;
- return p;
- }
- void destory(lstack* p)//销毁顺序栈
- {
- if (!is_empty(p))
- {
- free(p);
- }
- }
- void print(lstack* p)//打印顺序栈
- {
- for (int j = 0; j <=p->top; j++)
- {
- printf("%d ", p->data[j]);
- }
- }
- int main()
- {
- lstack* t = (lstack*)malloc(sizeof(lstack));
- t->top = -1;
- for (int i = 0; i < 7; i++)
- {
- int val;
- scanf_s("%d", &val);
- push(t, val);
- }
- printf("顺序栈中的元素从栈顶到栈底依次是:\n");
- print(t);
- printf("\n弹出顺序栈顶元素:\n");
- int y = pop(t);
- printf("%d\n", y);
- return 0;
-
-
- }
-
- 栈的链式存储
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- typedef struct node
- {
- int data;
- struct node* next;//下级节点
- }nodes;
- typedef struct head
- {
- int length;
- nodes* next;//栈顶元素指针
- }heads;
- heads* create()//创建头节点
- {
- heads* phead = (heads*)malloc(sizeof(heads));
- if (phead != NULL)
- {
- phead->next = NULL;
- phead->length = 0;
- }
- return phead;
- }
- int is_empty(heads* phead) //判断栈是否为空
- {
- if (phead->length == 0 || phead->next == NULL)
- {
- return 1;
- }
- return 0;
- }
- int get_size(heads* phead)//返回栈的大小
- {
- return phead->length;
- }
- heads* push(heads* phead, int val)//进栈操作
- {
- nodes* p = (nodes*)malloc(sizeof(nodes));
- if (p != NULL) { p->data = val; phead->length++; p->next = phead->next; phead->next = p; }
- return phead;
- }
-
- nodes* pop(heads* phead)//弹出栈顶元素
- {
- nodes* t = (nodes*)malloc(sizeof(nodes));
- t=phead->next;
- phead->next = phead->next->next;
- phead->length--;
- return t;
- }
-
- void destory(heads* phead)//销毁链栈
- {
- if (!is_empty(phead))
- {
- nodes* p = pop(phead);
- free(p);
- }
- }
-
- void print(heads* phead)//打印链栈
- {
- nodes* t = phead->next;
- for (int i = 0; i < phead->length; i++)
- {
- printf("%d ", t->data);
- t = t->next;
- }
- }
- int main()
- {
- heads* t = create();
- for (int i = 0; i < 7; i++)
- {
- int val;
- scanf_s("%d", &val);
- push(t, val);
- }
- printf("栈中的元素从栈顶到栈底依次是:\n");
- print(t);
- printf("\n弹出栈顶元素:\n");
- nodes* y = pop(t);
- printf("%d\n", y->data);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。