您的位置:首页 > 其它

线性结构(队列和栈)

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  队列