本部分内容是队列和栈的示例,为对数据结构感兴趣的朋友提供一些参考。
以下代码在VC++6.0中调试通过。
一、Queue 队列: 循环队列,先进先出 FIFO
#include <stdio.h>
#include <string.h>
#define QUEUE_MAX 5 // usual max为 32.
typedef struct
{
int data;
}queue_data_type;
typedef struct
{
int head;
int tail;
int length;
int buf[QUEUE_MAX];
}queue_type;
static queue_type my_ueue,*ptr_queue;
static void init_queue(queue_type *queue);
static void enqueue(queue_type *queue,int *data); // add item
static void dequeue(queue_type *queue,int *data); // delete item
static void peek_queue(queue_type *queue,int *data); // get first item
//static int lengch_queue(queue_type *queue);
//static int empty_queue(queue_type *queue);
static void clear_queue(queue_type *queue);
void main()
{
int data=0;
int i=0;
ptr_queue=&my_ueue;
init_queue(ptr_queue);
for(i=0;i<QUEUE_MAX;i++)
{
if(ptr_queue->length >= QUEUE_MAX)
{
break;
}
data=i;
enqueue(ptr_queue,&data);
}
// Test
dequeue(ptr_queue,&data);
dequeue(ptr_queue,&data);
data=11;
enqueue(ptr_queue,&data);
data=12;
enqueue(ptr_queue,&data);
data=13;
enqueue(ptr_queue,&data);
data=14;
enqueue(ptr_queue,&data);
data=15;
enqueue(ptr_queue,&data);
if(ptr_queue->length > 0)
{
peek_queue(ptr_queue,&data);
printf("Peek item is %d./n",data);
}
i=1;
printf("Queue length is %d./n",ptr_queue->length);
while (ptr_queue->length > 0)
{
dequeue(ptr_queue,&data);
printf("item %d is %d./n",i,data);
i++;
}
}
static void init_queue(queue_type *queue)
{
memset(queue,0x00,sizeof(queue_type));
}
static void clear_queue(queue_type *queue)
{
memset(queue,0x00,sizeof(queue_type));
}
static void enqueue(queue_type *queue,int *data)
{
if(queue->length < QUEUE_MAX)
{
queue->buf[queue->tail++]=*data;
queue->length++;
}
else
{
printf("Queue is full!/n");
}
}
static void dequeue(queue_type *queue,int *data)
{
if(queue->length > 0)
{
*data=queue->buf[queue->head++];
queue->length--;
}
else
{
printf("Queue is empty!/n");
}
}
static void peek_queue(queue_type *queue,int *data)
{
if(queue->length > 0)
{
*data=queue->buf[queue->head];
}
}
二、Stack 栈示例: 后进先出 LIFO。
#include <stdio.h>
#include <string.h>
#define STACK_MAX 5 // usual max 32.
typedef struct
{
int data;
}stack_data_type;
typedef struct
{
int top;
int buf[STACK_MAX];
}stack_type;
static stack_type my_stack,*ptr_stack;
static void init_stack(stack_type *stack);
static void push_stack(stack_type *stack,int *data); // add item
static void pop_stack(stack_type *stack,int *data); // delete item
static void peek_stack(stack_type *stack,int *data); // get first item
static void clear_stack(stack_type *stack);
void main()
{
int data=0;
int i=0;
ptr_stack=&my_stack;
init_stack(ptr_stack);
for(i=0;i<STACK_MAX;i++)
{
if(ptr_stack->top > STACK_MAX - 1)
{
break;
}
data=i+1;
push_stack(ptr_stack,&data);
}
// Test
push_stack(ptr_stack,&data);
pop_stack(ptr_stack,&data);
pop_stack(ptr_stack,&data);
data=11;
push_stack(ptr_stack,&data);
data=12;
push_stack(ptr_stack,&data);
data=13;
push_stack(ptr_stack,&data);
data=14;
push_stack(ptr_stack,&data);
if(ptr_stack->top > -1)
{
peek_stack(ptr_stack,&data);
printf("Peek item is %d./n",data);
}
printf("stack length is %d./n",ptr_stack->top+1);
while (ptr_stack->top > -1)
{
pop_stack(ptr_stack,&data);
printf("item %d is %d./n",ptr_stack->top+2,data);
}
}
static void init_stack(stack_type *stack)
{
memset(stack,0x00,sizeof(stack_type));
stack->top=-1;
}
static void clear_stack(stack_type *stack)
{
memset(stack,0x00,sizeof(stack_type));
stack->top=-1;
}
static void push_stack(stack_type *stack,int *data)
{
if(stack->top < STACK_MAX - 1)
{
stack->buf[++stack->top]=*data;
}
else
{
printf("stack is full!/n");
}
}
static void pop_stack(stack_type *stack,int *data)
{
if(stack->top > -1)
{
*data=stack->buf[stack->top--];
}
else
{
printf("stack is empty!/n");
}
}
static void peek_stack(stack_type *stack,int *data)
{
if(stack->top > -1)
{
*data=stack->buf[stack->top];
}
else
{
printf("stack is empty!/n");
}
}