隊列代碼

#include
#include <stdlib.h>
//隊列特點:先進先出(FIFO)
//隊列:隊首 隊尾
//隊列:線性隊列  鏈表隊列


//定義鏈表隊列的節點
typedef struct node
{
int data;
struct node *next;
}node;


//定義隊列
typedef struct queue
{
node * front;
node * tail;
}queue;


void CreateQueue(queue *q)
{
   node* head=(node*)malloc(1*sizeof(node));
   head->data=-1;
   head->next=NULL;
   q->front=q->tail=head;
}


int empty(queue *q)
{
if(q->front->next==NULL)
return 1;
else
return 0;
}


void push(queue *q,int _data)
{
   node* tmp=(node*)malloc(1*sizeof(node));
   tmp->data=_data;
   tmp->next=NULL;


   q->tail->next=tmp;
   q->tail=tmp;
}


void pop(queue *q)
{
if(!empty(q))
{
 node *tmp=q->front->next;
 q->front->next=tmp->next;
 printf("%d\t",tmp->data);
 free(tmp);
 tmp=NULL;
}
}




int main(int argc, char *argv[])
{
queue q1;
CreateQueue(&q1);
int i;
for(i=0;i<5;i++)
{
       push(&q1,i+1);
}
pop(&q1);
pop(&q1);
pop(&q1);
pop(&q1);
pop(&q1);
pop(&q1);
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章