线性结构(队列和栈)
2015-08-19 16:39
381 查看
队列和栈都是线性结构,分配连续内存。只不过:
队列是先进先出,栈是先进后出。
队列有头有尾,从尾部进入,从头部出来;而栈只有栈顶,入栈需要先压入数据再栈顶上移动一位,出栈是先栈顶下移动一位再弹出数据。
两者的处理很类似,代码如下:
队列:
栈:
队列是先进先出,栈是先进后出。
队列有头有尾,从尾部进入,从头部出来;而栈只有栈顶,入栈需要先压入数据再栈顶上移动一位,出栈是先栈顶下移动一位再弹出数据。
两者的处理很类似,代码如下:
队列:
#include<iostream> #include<stdio.h> #include<malloc.h> #include<string.h> //#define NDEBUG #include<assert.h> using namespace std; /** memory allocate; * free memory---in case of leak; * push data into queue; * pop data out of queue; * count the data in queue; * check the init length of queue */ #define FALSE 0 #define TRUE 1 #define STATUS int struct QUEUE_NODE { int* data; //as an array int head, tail, length, count; }; QUEUE_NODE* Allocate(int num) { if(num<=0) //invalid num return NULL; QUEUE_NODE *qnode; qnode = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE)*num); if(qnode==NULL) return NULL; memset(qnode,0,sizeof(QUEUE_NODE)*num); qnode->data=(int*)malloc(sizeof(int)*num); if(qnode->data==NULL) { free(qnode); return NULL; } memset(qnode->data,0,sizeof(int)*num); qnode->length=num; qnode->count=0; qnode->head=0; qnode->tail=0; return qnode; } STATUS Delete(QUEUE_NODE *p) { if(p==NULL) return FALSE; if(p->data==NULL) free(p); else { free(p->data); free(p); } return TRUE; } QUEUE_NODE* Push(QUEUE_NODE *p, int value) { if(p==NULL) return NULL; if(p->length==p->count) return NULL; p->data[p->tail]=value; p->tail = (p->tail+1)%p->length; p->count++; return p; } int Pop(QUEUE_NODE *p) { int value; if(p==NULL) return -1; if(p->head==p->tail) return -1; value = p->data[p->head]; p->data[p->head] = 0; p->head = (p->head+1)%p->length; p->count--; return value; } int Count(QUEUE_NODE *p) { if(p==NULL) return -1; else return p->count; } int Length(QUEUE_NODE *p) { if(p==NULL) return -1; else return p->length; } int main() { QUEUE_NODE *p=Allocate(4); assert(p!=NULL); cout<<"Queue is allocated in memory!\n"<<endl; Push(p,1); assert(1==p->count); Push(p,2); assert(2==p->count); printf("Now in queue, length= %d, count=%d\n",Length(p), Count(p)); printf(" the numbers are: %d,",Pop(p)); printf("%d\n",Pop(p)); Delete(p); cout<<"Everything is deleted!"<<endl; return 0; }
栈:
#include<iostream> #include<malloc.h> #include<string.h> #define NDEBUG #include<assert.h> using namespace std; struct STACK{ int* data; int length, top; }; STACK* Allocate(int num) { if(num<=0) return NULL; STACK* stack=(STACK*)malloc(sizeof(STACK)*num); if(stack==NULL) return NULL; memset(stack, 0, sizeof(stack)*num); //as 0 stack->data = (int*)malloc(sizeof(int)*num); if(stack->data==NULL) { free(stack); return NULL; } memset(stack->data, 0, sizeof(int)*num); stack->length=num; stack->top=0; return stack; } int Delete(STACK* p) { if(p==NULL) return -1; if(p->data==NULL) free(p); else { free(p->data); free(p); } return 1; } int Length(STACK *p) { if(p==NULL) return -1; return p->length; } void Push(STACK *p, int value) { if(p==NULL) return; if(p->top==p->length) return; p->data[p->top] = value; p->top++; } int Pop(STACK *p) { if(p==NULL) return -1; if(p->top==0) return -1; p->top--; int value=p->data[p->top]; p->data[p->top]=0; return value; } int main() { STACK *s=Allocate(4); assert(NULL!=s); cout<<"The length="<<Length(s)<<endl; Push(s,1); assert(1==s->top); Push(s,2); assert(2==s->top); cout<<"The numbers are: "<<Pop(s); cout<<", "<<Pop(s)<<endl; assert(1==Delete(s)); cout<<"The Stack is Deleted!"<<endl; return 0; }
相关文章推荐
- 算法系列15天速成 第九天 队列
- C++基于栈实现铁轨问题
- C语言单链队列的表示与实现实例详解
- C语言栈的表示与实现实例详解
- C语言实现颠倒栈的方法
- 算法系列15天速成 第十天 栈
- mysql 队列 实现并发读
- C#队列Queue用法实例分析
- C#多线程处理多个队列数据的方法
- C语言循环队列的表示与实现实例详解
- C++循环队列实现模型
- C#内置队列类Queue用法实例
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- Array栈方法和队列方法的特点说明
- C#通过链表实现队列的方法
- java数据结构之java实现栈
- C#队列Queue多线程用法实例
- C#数据结构与算法揭秘五 栈和队列
- 浅析栈区和堆区内存分配的区别
- JAVA 虚拟机 栈