链式队列基本操作总结

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct _QNODE
{
 int data;
 struct _QNODE *next;
}Qnode;

typedef struct _QUEUE
{
 Qnode *front;
 Qnode *rear;
}LinkQueue;

#define SIZE_OF_NODE sizeof(struct _QNODE)
#define SIZE_OF_QUEUE sizeof(struct _QUEUE)

void Init_LinkQueue(LinkQueue **QL);
LinkQueue *Insert_Queue(LinkQueue *QL, int key);
LinkQueue *Delete_Queue(LinkQueue *QL);
int Length_LinkQueue(LinkQueue *QL);
void Print_Queue(LinkQueue *QL);

int main()
{
 LinkQueue *QL = NULL;
 int array[] = {3,1,2,5,7,6,9};
 int i = 0, len, length;

 len = sizeof(array) / sizeof(int);
 Init_LinkQueue(&QL);

 for(i = 0; i < len; ++i)
 {
  QL = Insert_Queue(QL, array[i]);
 }
 printf("The LinkQueue you created is :/n");
 Print_Queue(QL);
 length = Length_LinkQueue(QL);
 printf("/nThe length of the queue is : %d/n", length);
 QL = Delete_Queue(QL);
 QL = Delete_Queue(QL);
 printf("/nAfter two times delete the queue is :/n");
 Print_Queue(QL);
 puts("");
 return 0;
}

void Init_LinkQueue(LinkQueue **QL)
{
 (*QL) = (LinkQueue *)malloc(SIZE_OF_QUEUE);
 if((*QL) == NULL)
 {
  printf("Memeory assign error!!/n");
  exit(1);
 }
 (*QL)->front = (*QL)->rear = NULL;
}

LinkQueue *Insert_Queue(LinkQueue *QL, int key)
{
 Qnode *s = NULL;
 
 s = (Qnode *)malloc(SIZE_OF_NODE);
 if(s == NULL)
 {
  printf("Memrory assign error!!/n");
  exit(1);
 }
 else
 {
  s->data = key;
  s->next = NULL;
  if(QL->rear == NULL) //queue is empty
  {
   QL->front = s;
   QL->rear = s;
  }
  else
  {
   QL->rear->next  = s;
   QL->rear = s;
  }
 }
 return QL;
}

LinkQueue *Delete_Queue(LinkQueue *QL)
{
 Qnode *node;
 int key;

 if(QL->front == NULL)
 {
  printf("Overflow of the linkqueue!!/n");
  exit(1);
 }
 else
 {
  key = QL->front->data ;
  node = QL->front ;
  if(QL->front == QL->rear )
  {
   QL->front = NULL;
   QL->rear = NULL;
  }
  else
  {
   QL->front = QL->front->next;
   free(node);
   node = NULL;
  }
 }
 return QL;
}

int Length_LinkQueue(LinkQueue *QL)
{
 int len = 0;
 Qnode *node;

 node = QL->front ;
 while(node != NULL)
 {
  node = node->next;
  len++;
 }
 return len;
}

void Print_Queue(LinkQueue *QL)
{
 Qnode *node = NULL;

 node = QL->front;
 while(node != NULL)
 {
  printf("/n %d-> ", node->data );
  node = node->next;
 }
}
/*
The LinkQueue you created is :

        3->
        1->
        2->
        5->
        7->
        6->
        9->
The length of the queue is : 7

After two times delete the queue is :

        2->
        5->
        7->
        6->
        9->
Press any key to continue
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值