链式队列的实现
来源:互联网 发布:电路仿真软件psim 编辑:程序博客网 时间:2024/05/16 19:46
typedef struct node{
int data;
struct node *next;
}linklist;
typedef struct{
linklist *front;
linklist *rear;
}linkqueue;
void setnull(linkqueue *p){ //建立空队
linklist *p,*r;
p=q->front;
while(p){
r=p;
p=p->next;
free(r);
}
q->front=q->rear=(linklist *)malloc(sizeof(linklist));
q->front->next=NULL;
return;
}
int empty_queue(linkqueue *q){ //置空队
return (q->front==q->rear);
}
void en_queue(linkqueue *q,int value){ //入队
q->rear->next=(linklist *)malloc(sizeof(linklist));
q->rear=q->rear->next;
q->rear->data=value;
q->rear->next=NULL;
return;
}
int de_queue(linkqueue *q,int *x){ //出队
linklist *r;
if(empty_queue(q)){
printf("link queue is empty!/n");
return -1;
}
r=q->front;
q->front=r->next;
free(r);
*x=q->front->data;
return 0;
}
void visit_queue(linkqueue *q){ //遍历队
linklist *p;
printf("queue:");
p=q->front->next;
while(p){
printf("%d",p->data);
p=p->next;
}
printf("/n");
return;
}
第二种定义:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include "myhead.h"
void perror_exit(const char *info)
{
perror(info);
exit(-1);
}
/* set a empty queue, front and rear
pointer both point to a head node*/
void queue_init(linkqueue *pq)
{
*pq = (linkqueue)malloc(sizeof(linknode) * 2);
(*pq)->front = (*pq)->rear = (linklist)malloc(sizeof(linknode));
(*pq)->rear->next = NULL;
}
/* check the queue is empty or not */
bool isempty(linkqueue q)
{
assert(q);
if(q->front == q->rear)
return true;
return false;
}
/* delete elements from the queue */
bool out_queue(linkqueue q, int *element)
{
assert(q);
if(isempty(q)){
fprintf(stderr, "the queue is empty./n");
return false;
}
linklist p = q->front;
q->front = q->front->next;
free(p);
*element = q->front->data;
return true;
}
/* add element into the queue */
bool en_queue(linkqueue q, int element)
{
assert(q);
linklist pnew;
pnew = (linklist)malloc(sizeof(linknode));
if(pnew == NULL){
perror("malloc failed");
return false;
}
pnew->data = element;
pnew->next = NULL;
q->rear->next = pnew;
q->rear = pnew;
return true;
}
/* show all elements in the current queue */
void show(linkqueue q)
{
assert(q);
if(isempty(q)){
fprintf(stderr, "the queue is empty./n");
return;
}
linklist p;
p = q->front->next;
int i = 1;
do{
fprintf(stderr, "[%d]: %d/n", i++, p->data);
p = p->next;
}while(p != NULL);
return;
}
/* thread handler function */
/*void *tfn(void *arg)
{
assert(arg);
linkqueue q = (linkqueue)arg;
int element;
while(1){
sleep(5);
if(out_queue(q, &element)){
fprintf(stderr, "%d has been deleted"
" from the queue/n", element);
show(q);
}
}
}
*/
/* safely scanf */
bool s_scanf(const char *format, int *ele_loc)
{
int ret1, ret2;
while((ret1=scanf(format, ele_loc)) != 1 || /
(ret2=getchar()) != '/n'){
if((ret1 != 1) && (errno == EINTR))
continue;
while(getchar() != '/n'); // discards invalid inputs
fprintf(stderr, "input invalid!/n");
//fprintf(stderr, "pls insert an integer: ");
}
return true;
}
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现。
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 队列的链式实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 队列的链式实现
- 队列的链式实现
- 链式队列的实现
- 【分享】【养生】消灭小肚腩
- IP转换 int型IP与字符型的ip的互换
- Android: 屏蔽屏幕旋转响应
- NAT基本原理
- java基础-继承关系
- 链式队列的实现
- How To Add a New Disk(s) to An Existing Diskgroup on RAC (Best Practices).
- 桌面切换想法的demo
- 未解问题 网上邻居访问账号缓存怎样查看、删除
- javaScript--alert使用方法
- 和MX控件一起使用Spark项目渲染器
- smarty缓存控制
- C语言中,函数不申明也能使用,但会出现warning: implicit declaration of function
- 原来放弃才能真正的超越