顺序栈、链栈已及队列的实现
2017-12-17 12:32
309 查看
#include<iostream> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define QUEUE_INIT_SIZE 100 #define MAXSIZE 100 typedef int SElemType; typedef int QElemType; //typedef int Status; enum Status{ERROR = 0,OK = 1}; typedef struct { SElemType * base; SElemType * top; int stacksize; }SqStack; //=======顺序栈===== Status SInitStack(SqStack &S) { S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if (!S.base)exit(OVERFLOW); S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } Status SGetTop(SqStack S, SElemType&e) { if (S.top == S.base)return ERROR; e = *(S.top - 1); return OK; } Status SPush(SqStack &S, SElemType e) { if (S.top - S.base >= S.stacksize) { S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType)); if (!S.base)exit(OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; return OK; } Status SPop(SqStack &S, SElemType &e) { if (S.top == S.base)return ERROR; e = *--S.top; return OK; } //==========链式栈======== typedef struct LElem { SElemType data; LElem *next; }; typedef struct LStack { LElem *top; int stacksize; }; Status LInitStack(LStack &S) { S.stacksize = 0; S.top = NULL; return OK; } Status LPush(LStack &S, SElemType &e) { LElem *p = (LElem *)malloc(sizeof(LElem)); if(!p) exit(OVERFLOW); p->data = e; p->next = S.top; S.top = p; S.stacksize++; return OK; } Status LPop(LStack &S, SElemType &e) { LElem *p = S.top; S.top = S.top->next; e = p->data; free(p); S.stacksize--; return OK; } Status LGetTop(LStack &S, SElemType &e) { if (!S.top) { printf("栈已空\n"); return ERROR; } e = S.top->data; return OK; } //============顺序队列============ typedef struct SqQ { int front; int rear; int len; QElemType b94f E[QUEUE_INIT_SIZE]; }; Status Init_SqQ(SqQ &Q) { Q.front = 0; Q.rear = 0; Q.len = 0; return OK; } Status SEnQ(SqQ &Q,QElemType e) { if (Q.len == QUEUE_INIT_SIZE) { printf("队列已满\n"); return ERROR; } Q.E[Q.rear] = e; Q.rear++; Q.len++; return OK; } Status SGetQ(SqQ &Q, QElemType &e) { if (Q.front == Q.rear) { printf("队列为空\n"); return OK; } e = Q.E[Q.front]; return OK; } Status SDeQ(SqQ &Q, QElemType &e) { if (Q.front == Q.rear) { printf("队列为空\n"); return ERROR; } e = Q.E[Q.front]; Q.front++; Q.len--; return OK; } //==========链队列========== typedef struct QNode { QElemType data; struct QNode * next; }QNode,* QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; Status InitQueue(LinkQueue &Q) { Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); if (!Q.front)exit(OVERFLOW); Q.front->next = NULL; return OK; } Status EnQueue(LinkQueue &Q, QElemType e) { QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if (!p)exit(OVERFLOW); p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; } Status DeQueue(LinkQueue &Q, QElemType e) { if (Q.front == Q.rear)return ERROR; QueuePtr p = Q.front->next; e = p->data; Q.front->next = p->next; if (Q.rear == p)Q.rear = Q.front; free(p); return OK; } Status GetHead(LinkQueue Q, QElemType &e) { if (Q.front == Q.rear)return ERROR; e = Q.front->next->data; return OK; } //=========循环队列======== typedef struct { QElemType *base; int front; int rear; }SqQueue; Status InitQueue(SqQueue &Q) { Q.base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType)); if (!Q.base)exit(OVERFLOW); Q.front = Q.rear = 0; return OK; } Status EnQueue(SqQueue &Q, QElemType e) { if (Q.rear == Q.front+MAXSIZE)return ERROR; Q.base[Q.rear%MAXSIZE] = e; Q.rear++; return OK; } Status DeQueue(SqQueue &Q, QElemType &e) { if (Q.front == Q.rear)return ERROR; e = Q.base[Q.front%MAXSIZE]; Q.front++; return OK; } Status GetHead(SqQueue Q, QElemType &e) { if (Q.rear == Q.front)return ERROR; e = Q.base[Q.front%MAXSIZE]; return OK; } int GetLength(SqQueue Q) { return Q.rear - Q.front; } //指针循环队列 typedef struct { QElemType *base; QElemType *front; QElemType *rear; int len; }pQueue; Status InitQueue(pQueue &Q) { Q.base = (QElemType*)malloc(MAXSIZE * sizeof(QElemType)); if (!Q.base)exit(OVERFLOW); Q.front = Q.rear = Q.base; Q.len = 0; return OK; } Status EnQueue(pQueue &Q, QElemType e) { if (Q.len == MAXSIZE)return ERROR; *Q.rear = e; Q.rear = Q.base + (Q.rear - Q.base+1) % MAXSIZE; Q.len++; return OK; } Status DeQueue(pQueue &Q, QElemType e) { if (Q.len == 0)return ERROR; Q.front = Q.base + (Q.front - Q.base + 1) % MAXSIZE; return OK; } Status GetHead(pQueue &Q, QElemType &e) { if (Q.len == 0)return ERROR; e = *Q.front; return OK; } int main() { //顺序栈 /*printf("=========顺序栈============\n"); SqStack s1; SElemType e; SInitStack(s1); for (int i = 0; i < 3; i++) { scanf("%d", &e); SPush(s1, e); } printf("stacksize: %d\n", s1.stacksize); for (int i = 0; i < 3; i++) { SGetTop(s1, e); printf("%d ", e); SPop(s1, e); } printf("\n"); //链式栈 printf("==========链栈==========\n"); LStack s2; LInitStack(s2); for (int i = 0; i < 3; i++) { scanf("%d", &e); LPush(s2, e); } printf("stacksize: %d\n", s2.stacksize); for (int i = 0; i < 3; i++) { LGetTop(s2, e); printf("%d ", e); LPop(s2, e); } printf("\n");*/ //========顺序队列========= QElemType t; /*printf("============顺序队列==========\n"); SqQ q1; Init_SqQ(q1); for (int i = 0; i < 3; i++) { scanf("%d", &t); SEnQ(q1, t); } printf("len: %d\n",q1.len); for (int i = 0; i < 3; i++) { SGetQ(q1, t); printf("%d ", t); SDeQ(q1, t); } printf("\n"); //=======链式队列========== printf("========链式队列=======\n"); LinkQueue q2; InitQueue(q2); for (int i = 0; i < 3; i++) { scanf("%d", &t); EnQueue(q2, t); } for (int i = 0; i < 3; i++) { GetHead(q2, t); printf("%d ", t); DeQueue(q2, t); } printf("\n"); //=========循环队列========== printf("==========循环队列=========\n"); SqQueue q3; InitQueue(q3); for (int i = 0; i < 3; i++) { scanf("%d", &t); EnQueue(q3, t); } printf("len: %d\n", GetLength(q3)); for (int i = 0; i < 3; i++) { GetHead(q3, t); printf("%d ", t); DeQueue(q3, t); } printf("\n");*/ printf("========指针循环队列========\n"); pQueue q4; InitQueue(q4); for (int i = 0; i < 3; i++) { scanf("%d", &t); EnQueue(q4, t); } printf("len: %d\n", q4.len); for (int i = 0; i < 3; i++) { GetHead(q4, t); printf("%d ", t); DeQueue(q4, t); } printf("\n"); return 0; }
相关文章推荐
- 链栈,顺序栈,单链队列,循环队列,非循环队列的简单实现
- 数据结构栈的实现 顺序栈,链栈
- 采用顺序存储实现循环队列的初始化、入队、出队操作。
- 顺序队列(数组实现)
- 队列 -- 顺序队列和链式队列类的实现
- C++模板实现顺序队列
- 《循环队列---队列的顺序表示和实现》
- 剑指offer 01-06解答思路以及代码(顺序数组找特定数字,替换空格字符,链表反转输出,重建二叉树,两个栈实现队列效果,旋转数组最小元素)
- 队列的顺序表示与实现
- 队列(顺序存储)C++模板实现
- 顺序队列模板--c++实现
- 队列的顺序存储C实现
- Android Executor 如何实现任务Runnable队列顺序执行
- 迎接2012之集合和泛型(5)------队列的顺序结构基本实现
- 顺序循环队列的c语言实现
- 数据结构回顾之顺序存储结构中的线性表(栈与队列顺序线性表实现)
- 数据机构(10)--顺序循环队列的实现及相关操作
- 顺序队列类实现(C++)
- 队列的顺序实现(循环数组)与链式实现
- 使用C#实现顺序队列