链式存储:
struct.h
#ifndef _STURCT_H
#define _STRUCT_H
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct queuenode{
int data;
struct queuenode *next;
}queue, *pqueue;
typedef struct linknode{
pqueue prev;
pqueue behind;
}link, *plink;
void init(pqueue *head, plink *p);
void add(plink p, int data);
int out(plink p);
bool is_empty(plink p);
bool is_empty(plink p);
#endif
func.c
#include"struct.h"
//初始化
void init(pqueue *head, plink *p)
{
(*head) = (pqueue)malloc(sizeof(queue));
if((*head) == NULL)
{
perror("in the init malloc failed");
exit(1);
}
(*head)->next = NULL;
(*p) = (plink)malloc(sizeof(link));
if(*p == NULL)
{
perror("in the init malloc falied");
exit(1);
}
(*p)->prev = *head;
(*p)->behind = *head;
}
//入队
void add(plink p, int data)
{
pqueue new = (pqueue)malloc(sizeof(queue));
if(new == NULL)
{
perror("add malloc failed");
exit(1);
}
new->data = data;
p->prev->next = new;
p->prev = new;
new->next = NULL;
}
//出队
int out(plink p)
{
int data;
pqueue temp;
if(is_empty(p))
{
printf("in the queue not any data\n");
return 0;
}
data = p->behind->next->data;
temp = p->behind;
p->behind = p->behind->next;
free(temp);
return data;
}
//判空
bool is_empty(plink p)
{
if(p->prev == p->behind)
return true;
else
return false;
}
//遍历
void show(plink p)
{
pqueue p1 = p->prev;
pqueue p2 = p->behind;
if(is_empty(p))
{
printf("not any data in the queue\n");
return ;
}
while(p1 != p2)
{
p2 = p2->next;
printf("%d ", p2->data);
}
printf("\n");
}
main.c
#include"struct.h"
int main(void)
{
int i;
pqueue head;
plink p;
int a[10] = {12,23,123,213,123,12,12,22,455,2424};
init(&head, &p);
show(p);
for(i = 0; i< 10; i++)
add(p,a[i]);
printf("the first add:");
show(p);
printf("out the data = %d\n",out(p));
printf("after out a data : ");
show(p);
return 0;
}