一个简单的链表实现队列

#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

上一篇:linux中UTC转换为CST时间


下一篇:Linux nohup命令(终端其他操作不打断当前进度)