ADT栈
---------------
#include<stdio.h> #include<stdlib.h> //Stack CreatStack(MAX_STACK_SIZE) #define MAX_STACK_SIZE 10 typedef struct { int key; } element; element stack[MAX_STACK_SIZE]; int top = -1; void Add(int &top,int item) { if(top<MAX_STACK_SIZE-1) { stack[top].key = item; printf("%d\n",stack[top].key); top++; } else { printf("The stack is full.\n"); } } element Del(int &top) { if(top>=0) { top--; printf("top=%d\n",top); return stack[top]; } else { printf("The stack is empty.\n"); } } int main() { int i=0; Add(top,212); Del(top); return 0; }---------------------
Queue队列
---------------
#include<stdio.h> //Queue CreateQueue(max_queue_size) #define MAX_QUEUE_SIZE 10 typedef struct { int key; } element; element queue[MAX_QUEUE_SIZE]; int rear =-1; int front = -1; void addq(int &rear,element item) { if(rear >= MAX_QUEUE_SIZE) { printf("The queue is full.\n"); } else { rear++; queue[rear].key=item.key; printf("%d\n",queue[rear]); } } element deleteq(int &rear,int &front) { if(rear==-1) { printf("The queue is empty.\n"); } else { front++; return queue[front]; } } void main() { element item; item.key = 212; addq(rear,item); deleteq(rear,front); }-------------
循环队列
-----
#include<stdio.h> ////Queue CreateQueue(max_queue_size) #define MAX_QUEUE_SIZE 10 typedef struct { int key; } element; element queue[MAX_QUEUE_SIZE]; int rear =0; int front = 0; void addq(int &rear,int &front,element item) { if((rear-front==-1)||(rear-front==MAX_QUEUE_SIZE-1)) { printf("The queueCycle is full.\n"); } else { rear=(rear+1)%MAX_QUEUE_SIZE; queue[rear].key=item.key; printf("%d\n",int &front) { if(rear==front) { printf("The queue is empty.\n"); } else { front=(front+1)%MAX_QUEUE_SIZE; return queue[front]; } } void main() { element item; item.key = 212; while(1) { if(!((rear-front==-1)||(rear-front==MAX_QUEUE_SIZE-1))) addq(rear,front,item); else { printf("Full.\n"); break; } } while(1) { if(!(rear==front)) deleteq(rear,front); else { printf("Empty.\n"); break; } } }
----