1. 编写程序实现顺序栈的各种基本运算:初始化、销毁、清空、判断是否为空栈、求栈的长度、取栈顶元素、进栈、出栈。在此基础上设计一个主程序完成如下功能:
(1)初始化栈s;
(2)判断栈s是否为空;
(3)依次进栈元素a,b,c,d;
(4)判断栈s是否为空;
(5)输出栈s的长度;
(6)栈里元素依次出栈,并输出;
(7)销毁栈s。
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; typedef char SElemType; #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACKINCREMENT 10 //存储空间分配增量 typedef struct { SElemType *base; //栈底指针 SElemType *top; //栈顶指针 int stacksize; //当前已分配的存储空间 } SqStack; Status InitStack(SqStack &S) { //构造一个空栈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; }//InitStack Status StackLength(SqStack S) { return S.top - S.base; }//StackLength Status DestoryStack(SqStack &S) { S.top = S.base; free(S.base); //若base的值为NULL,则表明栈结构不存在 S.base = NULL; S.top = NULL; S.stacksize = 0; return OK; } Status StackEmpty(SqStack S) { if (S.top == S.base) return 1; else return 0; }//StackEmpty Status GetTop(SqStack S, SElemType &e) { if (S.top == S.base) return ERROR; e = *(S.top - 1); return OK; }//GetTop Status Push(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; }//Push Status Pop(SqStack &S, SElemType &e) { //判断栈是否为空 if (S.base == S.top) return ERROR; e = *(S.top - 1); S.top--; return OK; }//Pop void main() { SqStack s; SElemType e; printf("(1)初始化栈 "); InitStack(s); printf("(2)The stack is "); if (StackEmpty(s)) printf("empty. "); else printf("not empty. "); printf("(3)依次进栈元素a,b,c,d "); Push(s, 'a'); Push(s, 'b'); Push(s, 'c'); Push(s, 'd'); printf("(4)The stack is "); if (StackEmpty(s)) printf("empty. "); else printf("not empty. "); printf("(5)The length of the stack is %d ", StackLength(s)); printf("(6)The stack is "); while (!StackEmpty(s)) { Pop(s, e); printf("%c ", e); } printf("(7)销毁栈s"); DestoryStack(s); }
运行结果:
2. 编写程序实现链队列的各种基本运算:初始化、销毁、清空、判断是否为空队列、求队列的长度、取队列的头元素、入队、出队。在此基础上设计一个主程序完成如下功能:
(1)初始化链队列q;
(2)判断链队列q是否为空;
(3)依次入队元素a,b,c;
(4)出队一个元素,并输出该元素;
(5)输出链队列q的长度;
(6)依次入队元素d,e,f;
(7)输出链队列q的长度;
(8)出队所有元素,并输出出队序列;
(9)销毁链队列q。
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int Status; typedef char QElemType; typedef struct QNode { //链队列结点的类型定义 int data; struct QNode *next; }QNode, *QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; Status InitQueue(LinkQueue &Q) { //建一个空队列Q Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); if (!Q.front) exit(0); Q.front->next = NULL; return OK; } //InitQueue_L Status EmptyQueue(LinkQueue &Q) { //判断是否为空 if (Q.front == Q.rear) return OK; else return ERROR; } Status EnQueue(LinkQueue &Q, QElemType a) { //在链队列Q中插入新的队尾结点a b c QueuePtr p; p = (QueuePtr)malloc(sizeof(QNode)); if (!p) exit(0); p->data = a; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; }// EnQueue_L Status DeQueue(LinkQueue &Q, QElemType &e) { //若队列不空,则删除Q的队头元素结点 输出的元素是e QueuePtr p; if (Q.front == Q.rear) return 0; p = Q.front->next; e = p->data; Q.front->next = p->next; if (Q.rear == p) Q.rear = Q.front; return OK; }// DeQueue_L Status LengthQueue(LinkQueue &Q) { //输出队列长度 QueuePtr p; int length = 0; while (Q.front->next) { Q.front = Q.front->next; length++; } return length; } void DestroyQueue(LinkQueue &Q) { //释放链队列 while (Q.front) { Q.rear = Q.front->next; delete Q.front; Q.front = Q.rear; } } void main() { LinkQueue Q; QElemType e; printf("(1)初始化链队列Q "); InitQueue(Q); printf("(2)链队列Q为%s ", (EmptyQueue(Q) ? "空" : "非空")); printf("(3)依次进队元素a,b,c; "); EnQueue(Q, 'a'); EnQueue(Q, 'b'); EnQueue(Q, 'c'); DeQueue(Q, e); printf("(4)出队一个元素,该元素=%c ", e); printf("(5)输出链队列的长度=%d ", LengthQueue(Q)); printf("(6)依次进队元素d,e,f; "); EnQueue(Q, 'd'); EnQueue(Q, 'e'); EnQueue(Q, 'f'); printf("(7)输出链队列的长度=%d ", LengthQueue(Q) + 2); printf("(9)释放链队列Queue "); DestroyQueue(Q); }
运行结果:
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2