#include链式存储实现#include #include #define MAXSTACKSIZE 128 typedef int ElementType; //栈及其操作集 struct StackNode { ElementType data[MAXSTACKSIZE]; int top; }; typedef struct StackNode* Stack; Stack CreateStack() { Stack s = (Stack)malloc(sizeof(struct StackNode)); if (s != NULL) s->top = -1; return s; } void DestoryStack(Stack s) { if (s != NULL) free(s); return; } bool Push(Stack s, ElementType x) { if (s == NULL || s->top == MAXSTACKSIZE - 1) return false; s->data[++s->top] = x; return true; } bool Pop(Stack s, ElementType* x) { if (s == NULL || s->top == -1) return false; *x = s->data[s->top--]; return true; } bool Top(Stack s, ElementType* x) { if (s == NULL || s->top == -1) return false; *x = s->data[s->top]; return true; } bool StackEmpty(Stack s) { return (s == NULL) || (s->top == -1); } bool StackFull(Stack s) { return (s == NULL) || (s->top == MAXSTACKSIZE - 1); }
采用一个带头结点的单链表。
#include队列 顺序存储实现#include typedef int ElementType; struct SNode { ElementType Data; struct SNode* Next; }; typedef struct SNode SNode; typedef struct SNode* Stack; Stack CreateStack() { Stack s = (Stack)malloc(sizeof(SNode)); if (s != NULL) { s->Next = NULL; } return s; } bool StackEmpty(Stack s) { return (s != NULL) && (s->Next == NULL); } bool StackFull(Stack s) { return false; } bool Push(Stack s, ElementType x) { if (s == NULL) return false; SNode* t = (SNode*)malloc(sizeof(SNode)); t->Data = x; t->Next = s->Next; s->Next = t; return true; } bool Pop(Stack s, ElementType* x) { if (s == NULL || StackEmpty(s)) return false; SNode* firstCell = s->Next; s->Next = firstCell->Next; *x = firstCell->Data; free(firstCell); return true; } bool Top(Stack s, ElementType* x) { if (s == NULL || StackEmpty(s)) { return false; } else { *x = s->Next->Data; return true; } } void DestoryStack(Stack s) { int t; if (s != NULL) { while (Pop(s, &t)) ; free(s); } return; }
#include链式存储实现#include #include #define MAXQUEUESIZE 64 typedef int ElementType; //队列及其操作集 struct QueueNode { ElementType data[MAXQUEUESIZE]; int front; int rear; }; typedef struct QueueNode* Queue; Queue CreateQueue() { Queue q = (Queue)malloc(sizeof(struct QueueNode)); if (q != NULL) { q->front = 0; q->rear = 0; } return q; } void DestoryQueue(Queue q) { if (q != NULL) free(q); return; } bool EnQueue(Queue q, ElementType x) { if ((q == NULL) || (q->front == (q->rear + 1) % MAXQUEUESIZE)) return false; q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXQUEUESIZE; return true; } bool DeQueue(Queue q, ElementType* x) { if ((q == NULL) || (q->front == q->rear)) return false; *x = q->data[q->front]; q->front = (q->front + 1) % MAXQUEUESIZE; return true; } bool QueueEmpty(Queue q) { return (q != NULL) && (q->front == q->rear); } bool QueueFull(Queue q) { return (q != NULL) && (q->front == (q->rear + 1) % MAXQUEUESIZE); }
采用一个不带头结点的单链表,队列结构体内保存有链表的头指针和尾指针。
#include#include //链式存储队列 typedef int ElementType; struct linkListNode { ElementType data; struct linkListNode* next; }; struct QueueNode { struct linkListNode* front; struct linkListNode* rear; }; typedef struct QueueNode* Queue; Queue CreateQueue() { Queue q = (Queue)malloc(sizeof(struct QueueNode)); if (q != NULL) { q->front = q->rear = NULL; } return q; } bool EnQueue(Queue q, ElementType x) { struct linkListNode* newNode = (struct linkListNode*)malloc(sizeof(struct linkListNode)); newNode->data = x; if (q->front == NULL) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } return true; } bool DeQueue(Queue q, ElementType* x) { struct linkListNode* frontCell; if (q->front == NULL) { return false; } frontCell = q->front; if (q->front == q->rear) q->front = q->rear = NULL; else q->front = q->front->next; *x = frontCell->data; free(frontCell); return true; } void DestoryQueue(Queue q) { int t; if (q != NULL) { while (DeQueue(q, &t)) ; free(q); } return; } bool QueueEmpty(Queue q) { return (q != NULL) && (q->front == NULL); } //跟顺序队列保持一致而已 bool QueueFull(Queue q) { return false; }



