队列是一种特殊的线性表,只允许在一端进行操作
先进先出的特点:所以可以在尾端插入元素从开头输出
操作:
初始队列
入队
出队
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* initlist()
{
Node* head=(Node*)malloc(sizeof(Node));
head->data=0;
head->next=NULL;
}
void enqueue(Node* head,int data)
{
Node* node=(Node*)malloc(sizeof(Node));
int i=0;
for(i=0;i<head->data;i++)
{
head=head->next;
}
node->data=data;
node->next=head->next;
head->next=node;
}
int isempty(Node* head)
{
if(head->next==NULL||head->data==0)
return 0;
else
return 1;
}
int delete_queue(Node* head)
{
Node* node=(Node*)malloc(sizeof(Node));
while(1)
{
if(isempty(head))
return -1;
else
{
node=head->next;
head->next=node->next;
int data=node->data;
free(node);
return data;
}
}
}
void print_queue(Node* head)
{
Node* node=(Node*)malloc(sizeof(Node));
node=head->next;
while(node!=NULL)
{
printf("%d->",node->data);
node=node->next;
}
printf("NULL");
}
int main()
{
Node* list=initlist();
enqueue(list,1);
enqueue(list,2);
print_queue(list);
int i=delete_queue(list);
printf("出队元素:%d\n",i);
print_queue(list);
return 0;
}