数据结构的简单应用
2011-08-08 13:13
323 查看
栈和队列解决球钟问题:
循环链表解决Joseph问题:
栈实现基本表达式求解:
#include <stdio.h> #include <stdlib.h> #define N 16 #define S1 4 #define S5 11 #define S60 11 typedef int datatype; typedef struct _node_ { datatype data; struct _node_ *next; } linknode, *linklist; typedef struct { linklist front, rear; } linkqueue; typedef struct { datatype data ; int top; } sqstack; sqstack *CreateEmptyStack() { sqstack *s; s = (sqstack *)malloc(sizeof(sqstack)); s->top = -1; return s; } int EmptyStack(sqstack *s) { return (-1 == s->top); } int FullStack(sqstack *s) { return (N-1 == s->top); } void PushStack(sqstack *s, datatype x) { s->data[++s->top] = x; return; } datatype PopStack(sqstack *s) { return s->data[s->top--]; } datatype GetTop(sqstack *s) { return s->data[s->top]; } linkqueue *CreateEmptyQueue() { linkqueue *lq; lq = (linkqueue *)malloc(sizeof(linkqueue)); lq->front = lq->rear = (linklist)malloc(sizeof(linknode)); lq->front->next = NULL; return lq; } int EmptyQueue(linkqueue *lq) { return (lq->front == lq->rear); } void EnQueue(linkqueue *lq, datatype x) { lq->rear->next = (linklist)malloc(sizeof(linknode)); lq->rear = lq->rear->next; lq->rear->data = x; lq->rear->next = NULL; return; } datatype DeQueue(linkqueue *lq) { linklist p; p = lq->front; lq->front = p->next; free(p); return (lq->front->data); } void ClearQueue(linkqueue *lq) { linklist p; while (lq->front != lq->rear) { p = lq->front; lq->front = p->next; free(p); } return; } int Order(linkqueue *q) { linklist p = q->front->next; while (p->next != NULL) { if (p->data > p->next->data) return 0; p = p->next; } return 1; } int main() { int i, count = 0; sqstack *s1, *s5, *s60; linkqueue *q; q = CreateEmptyQueue(); s1 = CreateEmptyStack(); s5 = CreateEmptyStack(); s60 = CreateEmptyStack(); for (i=1; i<=27; i++) { EnQueue(q, i); } while ( 1 ) { count++; i = DeQueue(q); /* if (s1->top != S1 - 1) { PushStack(s1, i); } else { while ( ! EmptyStack(s1) ) { EnQueue(q, PopStack(s1)); } if (s5->top != S5 - 1) { PushStack(s5, i); } else { while ( ! EmptyStack(s5) ) { EnQueue(q, PopStack(s5)); } if (s60->top != S60 - 1) { PushStack(s60, i); } else { while ( ! EmptyStack(s60) ) { EnQueue(q, PopStack(s60)); } EnQueue(q, i); if (Order(q)) break; } } }*/ if (s1->top != S1 - 1) { PushStack(s1, i); continue; } while ( ! EmptyStack(s1) ) { EnQueue(q, PopStack(s1)); } if (s5->top != S5 - 1) { PushStack(s5, i); continue; } while ( ! EmptyStack(s5) ) { EnQueue(q, PopStack(s5)); } if (s60->top != S60 - 1) { PushStack(s60, i); continue; } while ( ! EmptyStack(s60) ) { EnQueue(q, PopStack(s60)); } EnQueue(q, i); if (Order(q)) break; } printf("%d\n", count); return 0; }
循环链表解决Joseph问题:
#include <stdio.h> #include <stdlib.h> #define N 8 #define STEP 3 typedef int datatype; typedef struct _node_ { datatype data; struct _node_ *next; } linknode, *linklist; int main() { int i; linklist h, p; h = (linklist)malloc(sizeof(linknode)); h->data = 1; h->next = NULL; p = h; for (i=2; i<=N; i++) { p->next = (linklist)malloc(sizeof(linknode)); p = p->next; p->data = i; } p->next = h; for (i=0; i<=N; i++) { printf("%d ", p->data); p = p->next; } printf("\n"); while (h->next != h) { for (i=0; i<STEP-2; i++) h = h->next; p = h->next; h->next = p->next; printf("%d ", p->data); free(p); h = h->next; } printf("%d\n", h->data); return 0; }
栈实现基本表达式求解:
#include <stdio.h> #include <stdlib.h> typedef int datatype; typedef struct _node_ { datatype data; struct _node_ *next; } linknode, *linkstack; linkstack CreateEmptyStack() { linkstack s; s = (linkstack)malloc(sizeof(linknode)); s->next = NULL; return s; } int EmptyStack(linkstack s) { return (NULL == s->next); } void PushStack(linkstack s, datatype x) { linkstack p; p = (linkstack)malloc(sizeof(linknode)); p->data = x; p->next = s->next; s->next = p; return; } datatype PopStack(linkstack s) { datatype x; linkstack p = s->next; s->next = p->next; x = p->data; free(p); return x; } datatype GetTop(linkstack s) { return s->next->data; } void ClearStack(linkstack s) { linkstack p; while (s->next != NULL) { p = s->next; s->next = p->next; free(p); } return; } int GetPri(char op) { switch ( op ) { case '+' : case '-' : return 1; case '*' : case '/' : return 2; } } int Compute(int op1, int op2, char op) { switch ( op ) { case '+' : return op1 + op2; case '-' : return op1 - op2; case '*' : return op1 * op2; case '/' : return op1 / op2; } } void del_op(linkstack operator, linkstack operand, char op) { int op1, op2; char ch; while ( ! EmptyStack(operator) && GetPri(op)<= GetPri((char)GetTop(operator)) ) { ch = (char)PopStack(operator); op2 = PopStack(operand); op1 = PopStack(operand); PushStack(operand, Compute(op1, op2, ch)); } PushStack(operator, op); return; } int main() { char str[128], *p = str; int sum = 0, op1, op2; char ch; linkstack operator, operand; operator = CreateEmptyStack(); operand = CreateEmptyStack(); scanf("%s", str); while (*p != '\0') { if ((*p >= '0') && (*p <= '9')) { while ((*p >= '0') && (*p <= '9')) { sum = 10*sum + *p - '0'; p++; } PushStack(operand, sum); sum = 0; } else { del_op(operator, operand, *p); p++; } } while ( ! EmptyStack(operator) ) { ch = (char)PopStack(operator); op2 = PopStack(operand); op1 = PopStack(operand); PushStack(operand, Compute(op1, op2, ch)); } printf("%s = %d\n", str, GetTop(operand)); return 0; }
相关文章推荐
- 数据结构-C语言 栈的简单应用——括号匹配
- 【数据结构】c++Map的简单应用&& getline()细节
- 数据结构 二叉树常用简单应用
- 数据结构 No.6 java递归的简单应用
- 【数据结构】二叉树的简单操作及简单应用
- 数据结构--KMP简单应用
- 在游戏中数据结构的简单应用
- 2017年浙江工业大学大学生程序设计迎新赛预赛 D 简单的数据结构【纯 stl -> List 的应用】
- 数据结构在游戏中的简单应用
- 数据结构笔记——进制转换(栈的简单应用)(SDOJ 2131)
- 数据结构(1)-栈的特性以及简单应用
- poj 1611 The Suspects 初级->数据结构->简单并查集的应用
- 数据结构在游戏中的简单应用(1)
- 【郝斌数据结构自学笔记】70-74_已知两种遍历序列求原始二叉树概述_已知先序和中序求后序_已知中序和后序求先序_树的应用简单介绍
- 十二、Python简单数据结构应用(之…
- Java数据结构之队列的实现以及队列的应用之----简单生产者消费者应用
- 数据结构在游戏中的简单应用(2)
- 一步一步复习数据结构和算法基础-栈的简单应用(1)
- 数据结构-------树与二叉树的几个简单应用
- 数据结构--栈的两个简单应用