链式队列初始化,创建,插入,删除,查找等基本算法(出自李春葆数据结构)

//文件名:algo3-4.cpp
#include 
#include 
typedef char ElemType;
typedef struct qnode
{
	ElemType data;
	struct qnode *next;
} QNode;
/*typedef struct
{
	QNode *front;
	QNode *rear;
} LiQueue;*/
void InitQueue(QNode *&rear)	
{	rear=NULL;
}
void DestroyQueue(QNode *&rear)
{	QNode *p,*q;
	if(rear!=NULL)
	{p=rear->next;
	while (p!=rear)		
	{	q=p->next;
		rear->next=q;
		free(p);
		p=q;
	}
	free(p);}
}
bool QueueEmpty(QNode *rear)
{	
	return(rear==NULL);
}
void enQueue(QNode *&rear,ElemType e)
{
  QNode *p;
  p=(QNode *)malloc(sizeof(QNode));
  p->data=e;
  if(rear==NULL)
  {
	  p->next=p;
	  rear=p;
  }
  else
  {
	  p->next=rear->next;
	  rear->next=p;
	  rear=p;
  }
}
bool deQueue(QNode *&rear,ElemType &e)	
{	
	QNode *q;
	if(rear==NULL)
		return false;
	else if(rear->next==rear)
	{e=rear->data;
	 free(rear);
	 rear=NULL;
	}
	else
	{ q=rear->next;
      e=q->data;
	  rear->next=q->next;
	  free(q);
	}
	return true;
}
//文件名:exp3-4.cpp
#include 
#include 
#define MaxSize 5
typedef char ElemType;
typedef struct qnode
{
	ElemType data;
	struct qnode *next;
} QNode;
typedef struct
{
	QNode *front;
	QNode *rear;
} LiQueue;
extern void InitQueue(QNode *&rear);
extern void DestroyQueue(QNode *&rear);
extern bool QueueEmpty(QNode *rear);
extern void enQueue(QNode *&rear,ElemType e);
extern bool deQueue(QNode *&rear,ElemType &e);
void main()
{
	ElemType e;
	QNode *q;
	printf("链队的基本运算如下:\n");
	printf("  (1)初始化链队q\n");
	InitQueue(q);
	printf("  (2)依次进链队元素a,b,c\n");
	enQueue(q,'a');
	enQueue(q,'b');
	enQueue(q,'c');
	printf("  (3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));
	if (deQueue(q,e)==0) 
		printf("\t提示:队空,不能出队\n");
	else
		printf("  (4)出队一个元素%c\n",e);
	printf("  (5)依次进链队元素d,e,f\n");
	enQueue(q,'d');
	enQueue(q,'e');
	enQueue(q,'f');
	printf("  (6)出链队序列:");
	while (!QueueEmpty(q))
	{	deQueue(q,e);
		printf("%c ",e);
	}
	printf("\n");
	printf("  (7)释放链队\n");
	DestroyQueue(q);
}

你可能感兴趣的:(数据结构,C语言,算法)