队列

 

//C++ 对列的基本预算

#include <iostream>
using namespace std;

typedef struct node
{
	int data;
	struct node *next;
}node;


typedef struct quene
{
	node *head;
	node *end;
}quene;


/*
创建一个空链式队列
*/
int Create(struct quene *s)
{
	s->head = (node*)malloc(sizeof(node));//申请头节点
	if (s->head == NULL)
	{
		cout<<"Initial Failure"<<endl;
		return (0);
	}
	else
	{
		 s->end = s->head;
		 s->head->next = NULL;
		 return(1);
	}

}
/*
清空队列
*/
int DestoryQuene(struct quene *s)
{
	while(s->head!=NULL)
	{
		s->end = s->head->next;
		free(s->head);
		s->head = s->end;
	}
	return (1);
}

/*
队列的入队操作
*/
quene* InsertQuene(struct quene *s,int x)
//x为要插入的元素
{
	struct node *p;
	p = (node*)malloc(sizeof(node)); //为插入的值分配一个内存
	p->data =x;
	p->next =NULL;

	s->end->next =p;
	s->end = p;
	
	return s;
}


/*
队列的出队操作
*/
quene* DeleteQuene(struct quene *s)
//x为要删除的元素
{
	node *p;
	p = s->head->next;
	s->head = p;
	free(p);
	return (s);
}

/*
求队列的长度
*/
int LengthQuene(struct quene *s)
{
	int i = 0
	node *p;
	p = s->head->next;
	while(p != NULL)
	{
		i++;
		p= p->next;
	}
	return (i);
}

 

posted @ 2012-08-06 22:17  CBDoctor  阅读(253)  评论(0编辑  收藏  举报