Stacks and queues

来源:互联网 发布:163邮箱数据生成 编辑:程序博客网 时间:2024/05/18 01:42

//============================================================// 使用数组实现的栈 20140613//============================================================#include <stdio.h>#include <stdlib.h>typedefint StackElem;typedef struct stack {int top;  // 指向栈顶元素int size; // 栈大小capacityStackElem *arr;} *STACK;#ifndef ERROR#define ERROR -1#endif//============================================================// 初始化栈//============================================================STACK InitializeStack(STACK S, int n){S = (STACK)malloc(sizeof(struct stack));if (!S) {printf("s malloc error\n");return NULL;}S->arr = (StackElem *)malloc(sizeof(StackElem) * n);if (!S->arr) {free(S);printf("s->arr malloc error\n");return NULL;}S->size = n;S->top = -1; // carefulreturn S;}//============================================================// 判断栈是否为空//============================================================bool StackEmpty(STACK S){return S->top == -1; // careful}//============================================================// 判断栈是否满//============================================================bool StackFull(STACK S){return S->top == S->size - 1;}//============================================================// 进栈(top为最近插入元素的索引)//============================================================void Push(STACK S, StackElem x){if (StackFull(S)) {printf("overflow\n");return ;} else {// S->top = S->top + 1;// S->arr[S->top] = x;S->arr[++S->top] = x;}}//============================================================// 出栈//============================================================StackElem Pop(STACK S){if (StackEmpty(S)) {printf("underflow\n");return ERROR;} else {// S->top = S->top - 1;// return S->arr[S->top + 1];return S->arr[S->top--];}}//============================================================// 输出栈中元素//============================================================void StackPrint(STACK S){if (StackEmpty(S))return ;for (int i = 0; i <= S->top; i++)printf("%d\t", S->arr[i]);printf("\n");}int main(){int arr[] = {45, 65, 56, 2, 542, 6, 89, 21, 97, 60};int len = sizeof(arr) / sizeof(arr[0]);STACK S = NULL;S = InitializeStack(S, len - 1);for (int i = 0; i < len; i++) {Push(S, arr[i]);printf("%d\t", arr[i]);}printf("\n");for (int i = 0; i < len; i++) {StackElem val = Pop(S);if (val != ERROR)printf("%d\t", val);}printf("\n");system("pause");return 0;}
//============================================================// 使用链表实现的栈 20140613//============================================================#include <stdio.h>#include <stdlib.h>typedefint StackElem;typedef struct snode {StackElem elem;struct snode *next;} SNode;typedef struct stack {struct snode *top;} *STACK;#ifndef ERROR#define ERROR -1#endif//============================================================// 初始化栈//============================================================STACK InitializeStack(STACK S, int n){S = (STACK)malloc(sizeof(struct stack));if (!S) {printf("s malloc error\n");return NULL;}S->top = NULL;return S;}//============================================================// 判断栈是否为空//============================================================bool StackEmpty(STACK S){return S->top == NULL;}//============================================================// 进栈//============================================================void Push(STACK S, StackElem x){struct snode *p = (struct snode *)malloc(sizeof(struct snode));if (!p) {printf("p malloc error\n");return ;}p->elem = x;p->next = NULL;p->next = S->top; // 在S->top后插入S->top = p;}//============================================================// 出栈//============================================================StackElem Pop(STACK S){if (StackEmpty(S)) {printf("underflow\n");return ERROR;} StackElem x = S->top->elem;struct snode *p = S->top; // 临时保存待删除的结点S->top = S->top->next;free(p);return x;}//============================================================// 输出栈中元素//============================================================void StackPrint(STACK S){struct snode *p = S->top;while (p) {printf("%d\t", p->elem);p = p->next;}printf("\n");}//============================================================// 获得栈的大小//============================================================int GetStackSize(STACK S){int size = 0;struct snode *p = S->top;while (p) {size++;p = p->next;}return size;}int main(){int arr[] = {45, 65, 56, 2, 542, 6, 89, 21, 97, 60};int len = sizeof(arr) / sizeof(arr[0]);STACK S = NULL;S = InitializeStack(S, len - 1);for (int i = 0; i < len; i++) {Push(S, arr[i]);printf("size = %d\n", GetStackSize(S));StackPrint(S);}for (int i = 0; i < len; i++) {StackElem val = Pop(S);if (val != ERROR) {printf("size = %d\n", GetStackSize(S));StackPrint(S);}}system("pause");return 0;}


//============================================================// 使用数组实现的循环队列 20140613//============================================================#include <stdio.h>#include <stdlib.h>typedefint QueueElem;typedef struct queue {int head;  // 队列头int tail;  // 队列尾int size;  // 队列大小capacityQueueElem *arr;} *QUEUE;#ifndef ERROR#define ERROR -1#endif//============================================================// 初始化队列//============================================================QUEUE InitializeQueue(QUEUE Q, int n){Q = (QUEUE)malloc(sizeof(struct queue));if (!Q) {printf("Q malloc error\n");return NULL;}Q->arr = (QueueElem *)malloc(sizeof(QueueElem) * (n + 1));if (!Q->arr) {printf("Q->arr malloc error\n");return NULL;}Q->size = n;Q->head = Q->tail = 0;return Q;}//============================================================// 判断队列是否为空//============================================================bool QueueEmpty(QUEUE Q){return Q->head == Q->tail;}//============================================================// 判断对垒是否满(careful)//============================================================bool QueueFull(QUEUE Q){return Q->head == (Q->tail + 1) % Q->size;}//============================================================// 进队列//============================================================void Enqueue(QUEUE Q, QueueElem x){if (QueueFull(Q)) {printf("overflow\n");return;}Q->arr[Q->tail] = x;if (Q->tail == Q->size) // 到数组的末端,卷绕Q->tail = 0;elseQ->tail++;}//============================================================// 出队列//============================================================QueueElem Dequeue(QUEUE Q){if (QueueEmpty(Q)) {printf("underflow\n");return ERROR;}int x = Q->arr[Q->head];if (Q->head == Q->size) // 到数组的末端,卷绕Q->head = 0;elseQ->head++;return x;}int main(){int arr[] = {45, 65, 56, 2, 542, 6, 89, 21, 97, 60};int len = sizeof(arr) / sizeof(arr[0]);QUEUE Q = NULL;Q = InitializeQueue(Q, len - 1);for (int i = 0; i < len; i++) {Enqueue(Q, arr[i]);printf("%d\t", arr[i]);printf("head:%d, tail:%d\n", Q->head, Q->tail);}for (int i = 0; i < len; i++) {QueueElem val = Dequeue(Q);if (val != ERROR)printf("%d\t", val);printf("head:%d, tail:%d\n", Q->head, Q->tail);}system("pause");return 0;}
//============================================================// 使用链表实现的循环队列 20140613//============================================================#include <stdio.h>#include <stdlib.h>typedefint QueueElem;typedef struct qnode {QueueElem elem;struct qnode *next;} QNode;typedef struct queue {struct qnode *front;struct qnode *rear;} *QUEUE;#ifndef ERROR#define ERROR -1#endif//============================================================// 初始化队列//============================================================QUEUE InitializeQueue(QUEUE Q){Q = (QUEUE)malloc(sizeof(struct queue));if (!Q) {printf("Q malloc error\n");return NULL;}Q->front = NULL;Q->rear = NULL;return Q;}//============================================================// 判断队列是否为空//============================================================bool QueueEmpty(QUEUE Q){return Q->front == NULL;}//============================================================// 进队列//============================================================void Enqueue(QUEUE Q, QueueElem x){struct qnode *p = (struct qnode *)malloc(sizeof(struct qnode));if (!p) {printf("q malloc error\n");return ;}p->elem = x;p->next = NULL;if (QueueEmpty(Q)) {Q->front = p;Q->rear = p;} else {Q->rear->next = p; // 将新元素插到链表尾部Q->rear = p;       // 更新Q->rear的位置}}//============================================================// 出队列//============================================================QueueElem Dequeue(QUEUE Q){if (QueueEmpty(Q)) {printf("underflow\n");return ERROR;}int x = Q->front->elem;     // Q->front != NULLstruct qnode *p = Q->front; // 临时保存待删除的结点Q->front = Q->front->next;  // if (Q->front == NULL) Q->rear = NULL;free(p);return x;}//============================================================// 获得队列大小//============================================================int GetQueueSize(QUEUE Q){int size = 0;struct qnode *p = Q->front;while (p) {size++;p = p->next;}return size;}//============================================================// 打印队列//============================================================void PrintQueue(QUEUE Q){struct qnode *p = Q->front;while (p) {printf("%d\t", p->elem);p = p->next;}printf("\n");}int main(){int arr[] = {45, 12, 56, 3, 542, 6, 89, 21, 97, 60};int len = sizeof(arr) / sizeof(arr[0]);QUEUE Q = NULL;Q = InitializeQueue(Q);for (int i = 0; i < len; i++) {Enqueue(Q, arr[i]);printf("size = %d\n", GetQueueSize(Q));PrintQueue(Q);}for (int i = 0; i < len; i++) {QueueElem val = Dequeue(Q);if (val != ERROR) {printf("size = %d\n", GetQueueSize(Q));PrintQueue(Q);}}system("pause");return 0;}


0 0
原创粉丝点击