标签:
栈和队列是两种不同的存储结构。栈是后进先出(LIFO),队列是先进先出(FIFO)。
栈的基本操作:
1 #define STACK_INIT_SIZE 100 2 #define STACK_INCREMENT 10 3 /*结点定义*/ 4 typedef struct 5 { 6 SElemType * base; 7 SElemType * top; 8 int stacksize; 9 } SqStack; 10 /*初始化*/ 11 int InitStack(SqStack &stack) 12 { 13 stack.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); 14 if(!stack.base) return 0; 15 stack.top=stack.base; 16 stack.stacksize=STACK_INIT_SIZE; 17 return 1; 18 }; 19 /*销毁处理*/ 20 int DestoryStack(SqStack& stack) 21 { 22 if(stack.top==stack.base) return 1; 23 free(stack.base); 24 stack.base=stack.top=NULL; 25 }; 26 /*获取首元素*/ 27 int GetTop(SqStack & stack,SElemType & data) 28 { 29 if(stack.top==stack.base) return 0; 30 data = *(stack.top-1); 31 return 1; 32 } 33 /*添加元素*/ 34 int Push(SqStack & stack,SElemType data) 35 { 36 if((stack.top-stack.base)>=stack.stacksize) //判断栈是否已经满了 37 { 38 stack.base=(SElemType *)realloc(stack.base,(stack.stacksize+STACK_INCREMENT)*sizeof(SElemType)); 39 if(!stack.base) return 0; 40 stack.top=stack.base+stack.stacksize; 41 stack.stacksize+=STACK_INCREMENT; 42 } 43 *stack.top++ = data; 44 return 1; 45 }; 46 /*栈顶元素出栈*/ 47 int Pop(SqStack & stack,SElemType &data) 48 { 49 if(stack.top==stack.base) return 0; //判断是否栈空 50 data = *(--stack.top); 51 return 1; 52 }; 53 /*判断栈空*/ 54 int isEmpty(SqStack & stack) 55 { 56 if(stack.top==stack.base) return 1; 57 return 0; 58 };
队列的基本操作:
1 /*结构定义*/ 2 typedef int ElemType; 3 typedef struct QNode 4 { 5 ElemType data; 6 struct QNode *next; 7 }QNode,*QueuePtr; 8 /*头尾指针*/ 9 typedef struct 10 { 11 QueuePtr front; 12 QueuePtr rear; 13 }LinkQueue; 14 /*初始化*/ 15 int InitQueue(LinkQueue &Q) 16 { 17 Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); 18 if(!Q.front) return 0; 19 Q.front->next = NULL; 20 return 1; 21 } 22 /*销毁处理*/ 23 int DestoryQueue(LinkQueue &Q) 24 { 25 while(Q.front) 26 { 27 Q.rear = Q.front->next; 28 free(Q.front); 29 Q.front = Q.rear; 30 } 31 return 1; 32 } 33 /*在队尾插入元素*/ 34 int EnQueue(LinkQueue &Q,ElemType e) 35 { 36 QNode *p; 37 p = (QNode *)malloc(sizeof(QNode)); 38 if(!p) return 0; 39 p->data = e; 40 p->next = NULL; 41 Q.rear->next = p; 42 Q.rear = p; 43 return 1; 44 } 45 /*队首元素出队列*/ 46 int DeQueue(LinkQueue &Q,ElemType &e) 47 { 48 if(Q.front == Q.rear) return 0; //判断队列为空 49 QNode *p; 50 p = Q.front->next; 51 e = p->data; 52 Q.front->next = p->next; 53 if(Q.rear == p) Q.rear = Q.front; 54 free(p); 55 return 1; 56 } 57 /*遍历*/ 58 int TraverseQueue(LinkQueue Q) 59 { 60 QNode * p; 61 p = Q.front->next; 62 while(p) 63 { 64 cout<<p->data<<" "; 65 p = p->next; 66 } 67 return 1; 68 }
还有一种特殊的队列,循环队列(队列的顺序存储)。
1 /* 2 *少用一个空间 3 *若Q.front = Q.rear,表示队列为空 4 *若Q.front = Q.rear+1,表示队列已满 5 * 6 */ 7 typedef struct 8 { 9 ElemType *base; 10 int front; 11 int rear; 12 }SQueue; 13 14 int InitQueue(SQueue &Q) 15 { 16 Q.base = (ElemType *)malloc(MAXSIZE*sizeof(ElemType)); 17 if(!Q.base) return 0; 18 Q.front = Q.rear = 0; 19 return 1; 20 } 21 22 int QueueLength(SQueue Q) 23 { 24 return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; 25 } 26 27 int EnQueue(SQueue &Q,ElemType e) 28 { 29 if((Q.rear+1)%MAXSIZE == Q.front) return 0; //判断队列已满 30 Q.base[Q.rear] = e; 31 Q.rear = (Q.rear+1) % MAXSIZE; 32 return 1; 33 } 34 35 int DeQueue(SQueue &Q,ElemType &e) 36 { 37 if(Q.front == Q.rear) return 0; //判断队列为空 38 e = Q.base[Q.front]; 39 Q.front = (Q.front+1) % MAXSIZE; 40 return 1; 41 } 42 43 int TraverseQueue(SQueue &Q) 44 { 45 if(Q.front == Q.rear) return 0; 46 for(int i = Q.front; i<Q.rear; i++) 47 cout<<Q.base[i]<<" "; 48 return 1; 49 }
标签:
原文地址:http://www.cnblogs.com/tingyu951217/p/4563362.html