Let's go!!!!!
分类: C/C++
2013-01-01 10:39:23
//链队列
#include
#include
#include
typedef struct{
char name[10];
int age;
}QElemType;
struct Qnote{
QElemType data;
struct Qnote *next;
};
typedef struct Qnote Note;
typedef struct{
Note *front; //队头指针
Note *rear; //队尾指针
}LinkQueue;
void initQueue(LinkQueue *queue)
{
queue->front=queue->rear=(Note *)malloc(sizeof(Note));
if(!queue->front){
printf("Memety allocate fail\n");
exit(1);
}
queue->front->next=NULL;
}
void EnQueue(LinkQueue *queue,QElemType element)
{
struct Qnote *temp;
temp=(Note *)malloc(sizeof(Note));
if(!temp){
printf("Memety allocate fail\n");
exit(1);
}
temp->data=element;
temp->next=NULL;
queue->rear->next=temp;
queue->rear=temp;
}
void queueTravese(LinkQueue queue)
{
struct Qnote *p=queue.front->next;
if(queue.front->next!=NULL){
while(p){
printf("name:%s,age:%d\n",p->data.name,p->data.age);
p=p->next;
}
}
}
void DeHead(LinkQueue *queue)
{
struct Qnote *temp;
QElemType element;
if(queue->front!=queue->rear){
temp=queue->front->next;
element=temp->data;
queue->front->next=temp->next;
if(queue->rear==temp)
queue->front=queue->rear;
free(temp);
}
printf("element.name=%s,element.age=%d\n",element.name,element.age);
}
QElemType GetHead(LinkQueue queue)
{
QElemType element;
if(queue.front!=queue.rear){
element=queue.front->next->data;
}
return element;
}
int queueEmpty(LinkQueue queue)
{
if(queue.front->next==NULL)
return 1;
else
return 0;
}
int queueLength(LinkQueue queue)
{
int i=0;
struct Qnote *temp;
temp=queue.front->next;
while(temp){
i++;
temp=temp->next;
}
return i;
}
void destoryQueue(LinkQueue *queue)
{
while(queue->front){
queue->rear=queue->front->next;
free(queue->front);
queue->front=queue->rear;
}
}
void clearQueue(LinkQueue *queue)
{
struct Qnote *temp;
temp=queue->front->next;
while(temp){
queue->rear=temp->next;
free(temp);
temp=queue->rear;
}
queue->front->next=NULL;
queue->rear=queue->front;
}
int main()
{
int i;
QElemType element;
LinkQueue queue;
initQueue(&queue);
printf("Put element into the queue:\n");
for(i=0;i<4;i++)
{
printf("%dst person's name:",i+1);
scanf("%s",element.name);
printf("%dst person's age:",i+1);
scanf("%d",&element.age);
EnQueue(&queue,element);
}
printf("The is queue is empty?%s\n",queueEmpty(queue)?"yes":"no");
printf("\nLength of the queue:%d\n",queueLength(queue));
printf("\nGet the head element\n");
element=GetHead(queue);
printf("element.name=%s,element.age=%d\n",element.name,element.age);
printf("\nTraverse the queue:\n");
queueTravese(queue);
printf("\nDelete the head element:\n");
DeHead(&queue);
printf("\nTraverse the queue:\n");
queueTravese(queue);
clearQueue(&queue);
printf("\nThe is queue is empty?%s\n",queueEmpty(queue)?"yes":"no");
destoryQueue(&queue);
return 0;
}
//循环队列
#include
#include
#include
#define MAX 6
typedef struct{
char name[10];
int age;
}QElemType;
typedef struct{
QElemType *base;
int front;
int rear;
}Queue;
void initQueue(Queue *queue)
{
queue->base=(QElemType *)malloc(MAX * sizeof(QElemType));
if(!queue->base){
printf("Memery allocate fail.\n");
exit(1);
}
queue->front=queue->rear=0;
}
void EnQueue(Queue *queue,QElemType element)
{
queue->base[queue->rear]=element;
queue->rear=(queue->rear+1) % MAX;
}
void queueTraverse(Queue queue)
{
QElemType element;
while(queue.front!=queue.rear){
element=queue.base[queue.front];
printf("name=%s,age=%d\n",element.name,element.age);
queue.front=(queue.front+1) % MAX;
}
}
int queueFull(Queue queue)
{
if((queue.rear+1) % MAX == queue.front)
return 1;
else
return 0;
}
int queueEmpty(Queue queue)
{
if(queue.front == queue.rear)
return 1;
else
return 0;
}
int queueLength(Queue queue)
{
return (queue.rear - queue.front + MAX) % MAX;
}
void DeQueue(Queue *queue)
{
if(queue->front == queue->rear)
return;
else{
queue->front=(queue->front + 1)%MAX;
}
}
int main()
{
int i;
QElemType element;
Queue queue;
initQueue(&queue);
printf("Put element into the queue:\n");
for(i=0;i<8;i++)
{
if(1==queueFull(queue)){
printf("Queue is full!\n");
break;}
printf("%dst person's name:",i+1);
scanf("%s",element.name);
printf("%dst person's age:",i+1);
scanf("%d",&element.age);
EnQueue(&queue,element);
}
printf("\nLength of queue:%d\n",queueLength(queue));
printf("\nTraverse the queue!\n");
queueTraverse(queue);
DeQueue(&queue);
printf("\nTraverse the queue!\n");
queueTraverse(queue);
return 0;
}