数据结构周三

1.链式队列
1)头文件
#ifndef __LD_FUNC_H__
#define __LD_FUNC_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int dataType;
typedef struct Linkqueue
{
	union
	{
		dataType data;
		int len;
	} txt;
struct	Linkqueue* next;
}Linkqueue;

struct pos
{
	Linkqueue* front;
	Linkqueue* real;
};
struct pos* create_lk();
//尾插
void insert_head(struct pos* pos);
//头删
void delate_tail(struct pos*pos);
//遍历
void show(struct pos* pos);


#endif
2)功能函数
#include"ld_func.h"

struct pos* create_lk()
{
	Linkqueue* head =(Linkqueue*)malloc(sizeof(Linkqueue));
	if(head == NULL)
	{
		printf("头结点创建失败!!\n");
	}
	memset(head,0,sizeof(Linkqueue));
	struct pos* pos = (struct pos*)malloc(sizeof(struct pos));
	if(pos == NULL)
	{
		printf("位置指针创建失败!!\n");
		free(head);
	}
	pos->front = head;
	pos->real = head;
	return pos;
}
//尾插
void insert_head(struct pos* pos)
{
	printf("请输入要插入的数据:\n");
	dataType data;
	scanf("%d",&data);
	Linkqueue *temp = (Linkqueue*)malloc(sizeof(Linkqueue));
	temp->txt.data = data;
	temp->next = NULL;
	pos->real->next = temp;
	pos->real = pos->real->next;
	pos->front->txt.len++;
	return;
}
//头删
void delate_tail(struct pos*pos)
{
	dataType data = pos->front->next->txt.data;
	Linkqueue* temp = pos->front->next;
	pos->front->next = pos->front->next->next;
	free(temp);
		pos->front->txt.len--;
	printf("数据%d出队成功!\n",data);
	return;
}
//遍历
void show(struct pos* pos)
{
	Linkqueue* p = pos->front;
	while(p->next!= NULL)
	{
		p=p->next;
		printf("%d  ",p->txt.data);
	}
	printf("\n");
}
3)主函数
#include"ld_func.h"
int main(int argc, const char *argv[])
{
	struct pos* pos = create_lk();
	insert_head(pos);
	insert_head(pos);
	insert_head(pos);
	insert_head(pos);
	insert_head(pos);
	show(pos);
	delate_tail(pos);
	delate_tail(pos);
	show(pos);
	return 0;
}
2.栈的进制转换,将十进制换成二进制【链栈实现】
//进制转换
void turn(struct Lz* head)
{
	printf("请输入需要转换的数字:\n");
	int data;
	scanf("%d",&data);
	int len=0;
	while(data/2 != 0)
	{
		push(head,data%2);
		data = data/2;
		len++;
	}
	push(head,data%2);
	len++;
	for(int i =0;i<len;i++)
	{
		pop(head);
	}
	return;
}
3.判断括号是否匹配

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值