链表练习

程序要求:
创建一个可以存储int类型的链表,以回车键表示输入结束。
目的:熟悉链表
思路:
1.在主函数调用一个自定义的输入函数,用以输入数据,根据该函数的返回值判断是否继续输入。
2.关于自定义的输入函数:以字符的形式获取用户输入(可以用另一个函数将输入的字符"123"转换为数字123)由于事先不知道用户会输入多少字符,所以这里就又可以用链表实现。

函数头:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
struct film{
	int count;//存储用户输入的数据
	int size;//类似数组下标
	struct film *next;
};
struct gets{
	int count;//记录输入的字符(字符'1'通过ASC11码,经过a=(int)('1'-'0')得到数字a)
	struct gets *next;
};
bool s_gets(int *fp);//自定义输入函数
int cover(struct gets *fp);将一串字符转换为数字的函数

主函数:

int main(void)
{
	int tally,immi,argu;/*tally用于存储用输入个数*/
	struct film *head=NULL;/*immi作为输入的中间值*/
	struct film *previous,*current;/*argue接受s_gets函数返回值*/
	for(;;)
	{
		argu=s_gets(&immi);/*输入一串数字并按下回车表示一次输入结束,进入下一轮循环*/
		if(argu)/*如果再次按下回车,,这次按下的回车符号被函数s_gets()视为用户打算退出循环*/
		{/*s_gets()函数返回false*/
			current=(struct film*)malloc(sizeof(struct film));
			if(head==NULL)
				head=current;
			else
				previous->next=current;
			current->next=NULL;
			current->count=immi;
			tally++;
			current->size=tally;
			previous=current;
		}
		else
			break;
	}

s_gets()函数:

bool s_gets(int *fp)
{
	int tally=0;
	char ch;
	struct gets *head_1,*head_2=NULL;
	struct gets *current,*previous;
	ch=getchar();
	if(ch=='\n')
		return false;
	else
	{
		head_1=(struct gets*)malloc(sizeof(struct gets));
		head_1->count=(int)(ch-'0');
		tally++;
		while((ch=getchar())!='\n')
		{
			current=(struct gets*)malloc(sizeof(struct gets));
			if(head_2==NULL)
			{
				head_2=current;
				head_1->next=head_2;
			}
			else
				previous->next=current;
			current->next =NULL;
			current->count =(int)(ch-'0');
			previous=current;
			tally++;
		}
		*fp=cover(head_1,tally);
		return true;
	}	
}

cover()函数:

int cover(struct gets *head,int tally)
{
	int evalue=0;
	struct gets *current;
	if(tally==1)
		return head->count;
	current=head;
	while(current!=NULL)
	{
		evalue=evalue+current->count*pow(10,tally-1);
		tally--;
		current=current->next;
	}
	return evalue;
}

完整代码如下:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<stdbool.h>
struct film{
	int count;
	int size;
	struct film *next;
};
struct gets{
	int count;
	struct gets *next;
};
bool s_gets(int *fp);
int cover(struct gets *head,int tally);
int main(void)
{
	int tally,immi,argu;
	struct film *head=NULL;;
	struct film *previous,*current;
	for(;;)
	{
		argu=s_gets(&immi);
		if(argu)
		{
			current=(struct film*)malloc(sizeof(struct film));
			if(head==NULL)
				head=current;
			else
				previous->next=current;
			current->next=NULL;
			current->count=immi;
			tally++;
			current->size=tally;
			previous=current;
		}
		else
			break;
	}
	current=head;
	while(current!=NULL)
	{
		printf("第%d个输入为%d\n",current->size ,current->count );
		current=current->next ;
	}
}
bool s_gets(int *fp)
{
	int tally=0;
	char ch;
	struct gets *head_1,*head_2=NULL;
	struct gets *current,*previous;
	ch=getchar();
	if(ch=='\n')
		return false;
	else
	{
		head_1=(struct gets*)malloc(sizeof(struct gets));
		head_1->count=(int)(ch-'0');
		tally++;
		while((ch=getchar())!='\n')
		{
			current=(struct gets*)malloc(sizeof(struct gets));
			if(head_2==NULL)
			{
				head_2=current;
				head_1->next=head_2;
			}
			else
				previous->next=current;
			current->next =NULL;
			current->count =(int)(ch-'0');
			previous=current;
			tally++;
		}
		
		*fp=cover(head_1,tally);
		return true;
	}	
}
int cover(struct gets *head,int tally)
{
	int evalue=0;
	struct gets *current;
	if(tally==1)
		return head->count;
	current=head;
	while(current!=NULL)
	{
		evalue=evalue+current->count*pow(10,tally-1);
		tally--;
		current=current->next;
	}
	return evalue;
}

运行效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值