8.13链式队 链式栈

链栈,节点存储不是整数,存储学生信息(年龄,分数,姓名)三级引用。
1、建立学生信息结构体,将data改为学生信息结构体类型。
2、循环入栈和入队。

链式队

#include<myhead.h>
typedef struct
{
	int age;
	int score;
	char name[20];
}stu;
typedef struct node
{
	stu data;
	struct node *next;
}Node;
typedef struct
{
	int len;
	Node *rear;
	Node *front;
}Queue,*Pqueue;
Pqueue creat_queue()
{
	Pqueue p =malloc(sizeof(Queue));
	if(p==NULL)
	{
		printf("申请队列失败\n");
		return NULL;
	}
	p->len = 0;
	p->rear = NULL;
	p->front = NULL;
	return p;
}
int in_queue(Pqueue L,stu e)
{
	if(L==NULL)
	{
		printf("队列不存在\n");
		return -1;
	}
	Node *p = malloc(sizeof(Node));
	p->data = e;

	if(L->rear == NULL)
	{
		L->rear = p;
		L->front = p;
	}
	else
	{
		L->rear->next = p;
		L->rear = p;
	}
	L->len++;
	printf("入队成功\n");
	return 0;
}
int out_queue(Pqueue L)
{
	if(L==NULL)
	{
		printf("队列不存在\n");
		return -1;
	}
	Node *Q = L->front;
	L->front = Q->next;
	printf("出队节点是:%d\t%d\t%s\n",Q->data.age,Q->data.score,Q->data.name);
	free(Q);
	Q=NULL;
	L->len--;
	return 0;
}
int output_queue(Pqueue L)
{
	if(L==NULL||L->len==0)
	{
		printf("队列不存在或为空\n");
		return -1;
	}
	int i;
	Node *t = L->front;
	for(i=0;i<L->len;i++)
	{
		printf("%d\t%d\t%s\n",t->data.age,t->data.score,t->data.name);
		t=t->next;
	}
	printf("\n");
	return 0;
}
int destroy_queue(Pqueue L)
{
	if(L!=NULL)
	{
		Node *t;
		while(t!=NULL)
		{
			t=L->front;
			L->front = L->front->next;
			free(t);
		}
		free(L);
		L=NULL;
		printf("销毁成功\n");
	}
}
int main(int argc, const char *argv[])
{
	Pqueue L = creat_queue();

	int i;
	stu a[4] = {{11,66,"aaaaa"},{12,77,"bbbbb"},{13,88,"ccccc"},{14,99,"ddddd"}};
	for(i=0;i<4;i++)
	{
		in_queue(L,a[i]);
	}
	output_queue(L);
	out_queue(L);
	output_queue(L);
	destroy_queue(L);
	return 0;
}

链式栈

#include<myhead.h>
typedef struct
{
	int age;
	int score;
	char name[20];
}stu;
typedef struct node
{
	stu data;
	struct node *next;
}Node;
typedef struct
{
	int len;
	Node *top;
}Stack,*Pstack;
Pstack creat_stack()
{
	Pstack p =malloc(sizeof(Stack));
	if(p==NULL)
	{
		printf("创建链栈失败\n");
		return NULL;
	}
	p->len = 0;
	p->top = NULL;
	return p;
}
int push_stack(Pstack L,stu e)
{
	if(L==NULL)
	{
		printf("链栈不存在\n");
		return -1;
	}
	Node *p = malloc(sizeof(Node));
	p->data = e;

	p->next = L->top;
	L->top = p;
	L->len++;
	printf("入栈成功\n");
	return 0;
}
int pop_stack(Pstack L)
{
	if(L==NULL)
	{
		printf("链栈不存在\n");
		return -1;
	}
	Node *Q = L->top;
	L->top = Q->next;
	printf("出栈节点是:%d\t%d\t%s\n",Q->data.age,Q->data.score,Q->data.name);
	free(Q);
	Q=NULL;
	L->len--;
	return 0;
}
int output_stack(Pstack L)
{
	if(L==NULL||L->len==0)
	{
		printf("队列不存在或为空\n");
		return -1;
	}
	int i;
	Node *t = L->top;
	for(i=0;i<L->len;i++)
	{
		printf("%d\t%d\t%s\n",t->data.age,t->data.score,t->data.name);
		t=t->next;
	}
	printf("\n");
	return 0;
}
int destroy_stack(Pstack L)
{
	Node *t;
	while(t!=NULL)
	{
		t=L->top;
		L->top = L->top->next;
		free(t);
	}
		free(L);
		L=NULL;
		printf("销毁成功\n");
}
int main(int argc, const char *argv[])
{
	Pstack L = creat_stack();

	int i;
	stu a[4] = {{11,66,"aaaaa"},{12,77,"bbbbb"},{13,88,"ccccc"},{14,99,"ddddd"}};
	for(i=0;i<4;i++)
	{
		push_stack(L,a[i]);
	}
	output_stack(L);
	pop_stack(L);
	output_stack(L);
	destroy_stack(L);
	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值