列表的链表实现

放假了放假了

列表的链式存储

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

#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);
}
posted @ 2022-03-29 19:52  huigugu  阅读(36)  评论(0编辑  收藏  举报