[数据结构]程杰《大话数据结构》循环链表代码
2013-10-14 10:42
429 查看
#include "stdio.h" #include "stdlib.h" #include "math.h" #include "time.h" #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define MAXSIZE 20 /* 存储空间初始分配量 */ typedef int Status; typedef int QElemType; /* QElemType 类型根据实际情况而定,这里假设为 int */ /* 循环队列的顺序存储结构 */ typedef struct { QElemType data[MAXSIZE]; int front;/* 头指针 */ int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */ } SqQueue; /* 打印 */ Status visit(QElemType c) { printf("%d ", c); return OK; } /* 初始化一个空队列 Q */ /* 存储栈的数组已经定义,所以这里只操作指针。 */ Status InitQueue(SqQueue *Q) { Q->front = 0; Q->rear = 0; return OK; } /* 将 Q 清为空队列 */ Status ClearQueue(SqQueue *Q) { Q->front = Q->rear = 0; return OK; } /* 若队列 Q 为空队列,则返回 TRUE,否则返回 FALSE */ Status QueueEmpty(SqQueue Q) { if (Q.front == Q.rear) /* 队列空的标志 */ return TRUE; else return FALSE; } /* 返回 Q 的元素个数,也就是队列的当前长度 */ int QueueLength(SqQueue Q) { return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; /* c语言中的%可用于模运算,即求余数。 表达式 a % b 就是计算 a 除以 b 得出的余数。 例如: 4 % 3 的结果是 1 */ } /* 若队列不空,则用 e 返回 Q 的队头元素,并返回 OK,否则返回 ERROR */ Status GetHead(SqQueue Q, QElemType *e) { if (Q.front == Q.rear) /* 队列空 */ return ERROR; *e = Q.data[Q.front]; return OK; } /* 若队列未满,则插入元素 e 为 Q 新的队尾元素 */ Status EnQueue(SqQueue *Q, QElemType e) { if ((Q->rear + 1) % MAXSIZE == Q->front) /* 队列满的判断 */ return ERROR; Q->data[Q->rear] = e; /* 将元素 e 赋值给队尾 */ Q->rear = (Q->rear + 1) % MAXSIZE;/* rear 指针向后移一位置, */ /* 若到最后则转到数组头部 */ return OK; } /* 若队列不空,则删除 Q 中队头元素,用 e 返回其值 */ Status DeQueue(SqQueue *Q, QElemType *e) { if (Q->front == Q->rear) return ERROR; /* 队列空的判断 */ *e = Q->data[Q->front]; /* 将队头元素赋值给 e */ Q->front = (Q->front + 1) % MAXSIZE; /* front 指针向后移一位置, */ /* 若到最后则转到数组头部 */ return OK; } /* 从队头到队尾依次对队列 Q 中每个元素输出 */ Status QueueTraverse(SqQueue Q) { int i; i = Q.front; while ((i + Q.front) != Q.rear) { visit(Q.data[i]); i = (i + 1) % MAXSIZE; } printf("\n"); return OK; } int main() { Status j; int i = 0, l; QElemType d; SqQueue Q; InitQueue(&Q); printf("初始化队列后,队列空否?%u(1:空 0:否)\n", QueueEmpty(Q)); printf("请输入整型队列元素(不超过%d 个),-1 为提前结束符: ", MAXSIZE - 1); do { /* scanf("%d",&d); */ d = i + 100; if (d == -1) break; i++; EnQueue(&Q, d); } while (i < MAXSIZE - 1); printf("队列长度为: %d\n", QueueLength(Q)); printf("现在队列空否?%u(1:空 0:否)\n", QueueEmpty(Q)); printf("连续%d 次由队头删除元素,队尾插入元素:\n", MAXSIZE); for (l = 1; l <= MAXSIZE; l++) { DeQueue(&Q, &d); printf("删除的元素是%d,插入的元素:%d \n", d, l + 1000); /* scanf("%d",&d); */ d = l + 1000; EnQueue(&Q, d); } l = QueueLength(Q); printf("现在队列中的元素为: \n"); QueueTraverse(Q); printf("共向队尾插入了%d 个元素\n", i + MAXSIZE); if (l - 2 > 0) printf("现在由队头删除%d 个元素:\n", l - 2); while (QueueLength(Q) > 2) { DeQueue(&Q, &d); printf("删除的元素值为%d\n", d); } j = GetHead(Q, &d); if (j) printf("现在队头元素为: %d\n", d); ClearQueue(&Q); printf("清空队列后, 队列空否?%u(1:空 0:否)\n", QueueEmpty(Q)); return 0; }
相关文章推荐
- [数据结构]程杰《大话数据结构》单链表代码
- [数据结构]程杰《大话数据结构》链栈代码
- [数据结构]程杰《大话数据结构》静态链表代码(
- [数据结构]程杰字符串的基本操作代码
- 《数据结构》 循环链表和双向链表常用操作代码集合
- [数据结构]程杰 线索二叉树代码
- [数据结构]程杰《大话数据结构》书中的顺序表源码
- 数据结构之线性表代码实现顺序存储,链式存储,静态链表(选自大话数据结构)
- C语言-数据结构-循环链表实例-维吉尼亚(vigener)密码源代码
- [数据结构]程杰队列的链式存储结构及实现代码
- [数据结构]程杰模式串匹配、KMP算法及其改进(代码)
- [数据结构]程杰《大话数据结构》书中的顺序栈源码(
- Android JNI 使用的数据结构JNINativeMethod详解 ||建立Android SDK下的JNI、JAVA应用完整步骤---Android JAVA调用C++代码
- 数据结构 -- 双向循环链表
- 线性表_双向循环链表(Caesar加密变换结点顺序 代码实现 )
- 代码整洁之道--对象和数据结构体会
- 链表数据结构图解 和 代码实现
- 重构改善既有代码设计-----重新组织数据结构
- 数据结构(Data structure):用循环链表解决约瑟夫问题(C语言)
- 数据结构代码1