其他分享
首页 > 其他分享> > 列表的链表实现

列表的链表实现

作者:互联网

放假了放假了

列表的链式存储

用链表写列表,非常怪味儿,很简单,但总感觉不对劲,但确实又是对的。

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

typedef struct Node{
	int data;
	struct Node* next;
}node,*pnode;

typedef struct Queue {
	pnode front, rear;
}queue,*pqueue;

pqueue createqueue()
{
	pqueue Q = (pqueue)malloc(sizeof(queue));
	return Q;
}

int isempty(pqueue Q)
{
	return (Q->front == NULL);
}


int deleteQ(pqueue Q)
{
	pnode temp;
	if (isempty(Q))
	{
		printf("空的");
		return 0;
	}
	else
	{
		temp = Q->front;
		if (Q->front == Q->rear)
		{
			Q->front = Q->rear = NULL;
		}
		else
		{
			Q->front = Q->front->next;
		}
		free(temp);
		return 1;
	}
}

int addQ(pqueue Q, int elem)
{
	pnode a = (node*)malloc(sizeof(node));
	a->data = elem;
	a->next = NULL;
	if (Q->rear == NULL)
	{
		Q->front = Q->rear = a;
		return 1;
	}
	else
	{
		Q->rear->next = a;
		Q->rear = a;
		return 1;
	}
}


void print(pqueue Q)
{
	pnode temp;
	temp = Q->front;
	for (;temp->next!=NULL;)
	{
		printf("%d ", temp->data);
		temp = temp->next;
	}
	printf("%d ", temp->data);
	printf("\n");
}

int main()
{
	pqueue Q = createqueue();
	Q->front = Q->rear = NULL;
	addQ(Q, 1);
	print(Q);
	addQ(Q, 2);
	print(Q);
	addQ(Q, 3);
	print(Q);
	addQ(Q, 4);
	print(Q);
	deleteQ(Q);
	print(Q);
	deleteQ(Q);
	print(Q);
}

标签:pqueue,temp,实现,列表,链表,print,int,front,rear
来源: https://www.cnblogs.com/huigugu/p/16073523.html