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)初始化棧\n"); InitStack(s); printf("(2)The stack is "); if (StackEmpty(s)) printf("empty.\n"); else printf("not empty.\n"); printf("(3)依次進棧元素a,b,c,d\n"); Push(s, 'a'); Push(s, 'b'); Push(s, 'c'); Push(s, 'd'); printf("(4)The stack is "); if (StackEmpty(s)) printf("empty.\n"); else printf("not empty.\n"); printf("(5)The length of the stack is %d\n", StackLength(s)); printf("(6)The stack is "); while (!StackEmpty(s)) { Pop(s, e); printf("%c \n", 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\n"); InitQueue(Q); printf("(2)鏈隊列Q為%s\n", (EmptyQueue(Q) ? "空" : "非空")); printf("(3)依次進隊元素a,b,c;\n"); EnQueue(Q, 'a'); EnQueue(Q, 'b'); EnQueue(Q, 'c'); DeQueue(Q, e); printf("(4)出隊一個元素,該元素=%c\n", e); printf("(5)輸出鏈隊列的長度=%d\n", LengthQueue(Q)); printf("(6)依次進隊元素d,e,f;\n"); EnQueue(Q, 'd'); EnQueue(Q, 'e'); EnQueue(Q, 'f'); printf("(7)輸出鏈隊列的長度=%d\n", LengthQueue(Q) + 2); printf("(9)釋放鏈隊列Queue\n"); DestroyQueue(Q); }
運行結果:
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2