数据结构12.6

作业

栈的进制转换:

代码:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef int DT;

typedef struct link
{
	union
	{
		DT data;
		int len;
	}txt;
	struct link* next;
}LT;


LT* create_linklist()
{
	LT* head=(LT*)malloc(sizeof(LT));
	if(NULL==head)
	{
		printf("创建失败,请稍后重试\n");
		return NULL;
	}
	head->txt.len=0;
	head->next=NULL;
	return head;
}

//入栈
void ruzhan(LT* head,int i)
{
	LT* temp =(LT*)malloc(sizeof(LT*));
	if(NULL==temp)
	{
		printf("创建失败\n");
		return ;
	}
	temp->txt.data=i;
	temp->next=NULL;

	if(NULL==head->next)
	{
		head->next=temp;
		head->txt.len++;
	}
	else
	{
		temp->next=head->next;
		head->next=temp;
		head->txt.len++;
	}
	return ;
}


//出栈
void chuzhan(LT* head)
{

	if(NULL==head->next)
	{
		printf("无数据\n");
		return ;
	}
	LT* temp =(LT*)malloc(sizeof(LT*));
	if(NULL==temp)
	{
		printf("创建失败\n");
		return ;
	}
	temp->txt.data=0;
	temp->next=NULL;
	if(NULL==head->next->next)
	{
		temp=head->next;
		printf("%d ",temp->txt.data);
		free(temp);
		head->next=NULL;
		head->txt.len--;
	}
	else
	{
		temp=head->next;
		printf("%d ",temp->txt.data);
		head->next=head->next->next;
		free(temp);
		head->txt.len--;
	}
	return ;
}


int main(int argc, const char *argv[])
{
	printf("请输入>>>");
	int j;
	scanf("%d",&j);
	LT* head=create_linklist();
	int k=0;
	while(j>1)
	{
		int a=j%2;
		j=j/2;
		ruzhan(head,a);
		k++;
	}
	printf("%d\n",j);
	ruzhan(head,j);
	for(int n=0;n<=k;n++)
	{
		chuzhan(head);

	}
	printf("\n");

	return 0;
}

运行结果:

作业2:栈实现判断括号是否匹配

代码:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef char DT;

typedef struct link
{
	union
	{
		DT data;
		int len;
	}txt;
	struct link* next;
}LT;


LT* create_linklist()
{
	LT* head=(LT*)malloc(sizeof(LT));
	if(NULL==head)
	{
		printf("创建失败,请稍后重试\n");
		return NULL;
	}
	head->txt.len=0;
	head->next=NULL;
	return head;
}


//入栈
void ruzhan(LT* head,char i)
{
	LT* temp =(LT*)malloc(sizeof(LT*));
	if(NULL==temp)
	{
		printf("创建失败\n");
		return ;
	}
	temp->txt.data=i;
	temp->next=NULL;

	if(NULL==head->next)
	{
		head->next=temp;
		head->txt.len++;
	}
	else
	{
		temp->next=head->next;
		head->next=temp;
		head->txt.len++;
	}
	return ;
}


//出栈
void chuzhan(LT* head)
{

	if(NULL==head->next)
	{
		printf("无数据\n");
		return ;
	}
	LT* temp =(LT*)malloc(sizeof(LT*));
	if(NULL==temp)
	{
		printf("创建失败\n");
		return ;
	}
	temp->txt.data=0;
	temp->next=NULL;
	if(NULL==head->next->next)
	{
		temp=head->next;

		free(temp);
		head->next=NULL;
		head->txt.len--;
	}
	else
	{
		temp=head->next;

		head->next=head->next->next;
		free(temp);
		head->txt.len--;
	}
	return ;
}












int main(int argc, const char *argv[])
{
	LT* head=create_linklist();
	char a[100];
	char i;
	printf("请输入字符串:");
	scanf("%s",a);
	int len=strlen(a);
	for(int i=0;i<len;i++)
	{
		if(a[i]=='(')
		{
			ruzhan(head,a[i]);
		}
		if(a[i]==')')
		{
			if(NULL==head->next)
			{
				printf("括号匹配不成功\n");
				return 0;
			}
			chuzhan(head);
		}
	}
	if(NULL==head->next)
	{
		printf("括号匹配成功\n");
	}
	else
	{
		printf("括号匹配不成功\n");
	}
	return 0;
}

运行结果:

作业3:链式队列

代码:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "./3_func.h"


int main(int argc, const char *argv[])
{
	Lkq_pos* pos=create_list();
	while(1)
	{
		printf("1.入队\n");
		printf("2.出队\n");
		printf("3.遍历\n");
		printf("4.退出\n");
		printf("选择>>>");
		int xz;
		scanf("%d",&xz);
		switch(xz)
		{
		case 1:
			rudui(pos);
			break;
		case 2:
			chudui(pos);
			break;
		case 3:
			bianli(pos);
			break;
		case 4:
			goto END;
		default:
			printf("输入错误\n");
		}
	}
END:
	return 0;
}

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "./3_func.h"


Lkq_pos* create_list()
{
	Lkqueue* head = (Lkqueue*)malloc(sizeof(Lkqueue));
	if(NULL==head)
	{
		printf("创建链表失败\n");
		return NULL;
	}
	head->txt.len=0;
	head->next=NULL;
	Lkq_pos* pos = (Lkq_pos*)malloc(sizeof(Lkq_pos));
	if(NULL==pos)
	{
		printf("创建指向失败\n");
		return NULL;
	}
	pos->front=head;
	pos->rear=head;
	return pos;
}


//入队
void rudui(Lkq_pos* pos)
{
	printf("请输入要入队的数据>>>");
	int sj;
	scanf("%d",&sj);
	Lkqueue* temp=(Lkqueue*)malloc(sizeof(Lkqueue));
	if(NULL==temp)
	{
		printf("创建失败\n");
	}
	temp->txt.data=sj;
	temp->next=NULL;

	pos->rear->next=temp;
	pos->rear=temp;
	pos->front->txt.len++;
	return ;

}


//遍历
void bianli(Lkq_pos* pos)
{
	if(pos->front->next == NULL)
	{
		printf("队列无数据\n");
		return ;
	}
	Lkqueue* p=pos->front;
	while(p->next!=NULL)
	{
		p=p->next;
		printf("%d ",p->txt.data);
	}
	printf("\n");
	return ;
}


//出队
void chudui(Lkq_pos *pos)
{
	if(NULL==pos->front->next)
	{
		printf("无数据可出队\n");
		return ;
	}
	Lkqueue* temp=(Lkqueue*)malloc(sizeof(Lkqueue));
	if(NULL==temp)
	{
		printf("创建失败\n");
	}
	temp->txt.data=0;
	temp->next=NULL;

	if(NULL==pos->front->next->next)
	{
		temp=pos->front->next;
		printf("%d\n",temp->txt.data);
		pos->front->next=NULL;
		free(temp);
		pos->front->txt.len++;

	}
	else
	{
		temp=pos->front->next;
		printf("%d\n",temp->txt.data);
		pos->front->next=pos->front->next->next;
		free(temp);
		pos->front->txt.len++;
	}
	return ;
}



#ifndef __FUNC_H__
#define __FUNC_H__

typedef int dataType;    //链表中每个结点的类型
typedef struct linkqueue
{
	union{
		int len;              //头结点使用
		dataType data;        //有 效数据结点使用
	}txt;                     //数据域
	struct linkqueue* next ;  //指针域
}Lkqueue;

typedef struct 
{
	Lkqueue* front;    //指向头结点,存储头结点地址
	Lkqueue* rear;    //指向尾结点,存储尾结点地址
}Lkq_pos;

Lkq_pos* create_list();

void rudui(Lkq_pos* pos);

void chudui(Lkq_pos *pos);

void bianli(Lkq_pos* pos);



#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值