当曲高和寡不是一个结果的时候,它往往会成为一种错觉,……,若你的思考真的精妙到了只有你自己能理解,那你岂不是更应该将你精妙的思考分享给他人?

一个简单的链表实现队列

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

typedef int datatype;
typedef struct link_node{
	datatype info;
	struct link_node* next;
}node;
typedef node* linklist;
typedef struct link_node2{
	linklist front, rear;
}node2;
typedef node2* linkqueue;

void SetNull(linkqueue q){
	q->front = (linklist)malloc(sizeof(node));
	q->front->next = NULL;
	q->rear = q->front;
}

int Empty(linkqueue q){
	if(q->front == q->rear) return -1;
	else return 0;
}

int Front(linkqueue q){
	if(Empty(q)){
		//TODO
		printf("queue is empty!\n");
		return -1;
	}
	else return q->front->next->info;
}

void insert (linkqueue q, int x){
	linklist newnode = (linklist)malloc(sizeof(node2));
	newnode->info = x;
	q->rear->next = newnode;
	q->rear = newnode;
	q->rear->next = NULL;
}

int pop(linkqueue q){
	int tmp;
	linklist s;
	if(Empty(q)){
		printf("queue is empty!\n");
		return -1;
	}
	else {
		s = q->front->next;
		if(s->next == NULL){
			q->front->next = NULL;
			q->rear = q->front;
		}
		else q->front->next = s->next;
		tmp = s->info;
		return tmp;
	}
}


int main(){
	node2 q;
	SetNull(&q);
	insert(&q, 2);
	insert(&q, 3);
	insert(&q, 8);
	pop(&q);
	pop(&q);
	int a = 0;
	a = Front(&q);
	printf("%d\n",a);
}

原文:http://blog.csdn.net/tengweitw/article/details/17170743

作者:nineheadedbird

posted @ 2022-02-27 19:49  01am  阅读(75)  评论(0编辑  收藏  举报