链表例题及二维数组例题

链表例题

链表解题需要经过手工推导,如下
手推一:

//手推 
#include<stdio.h>
#include<string.h>
struct student
{
	int sno;
	char sname[20];
	char sex;
	int age;
	student * next;
};
int main()
{
	student *phead,s[100];
	phead = NULL;
	s[0].sno = 111;
	strcpy(s[0].sname ,"AAA");
	s[0].sex = 'F';
	s[0].age = 18;
	s[0].next = NULL;
	s[0].next = &s[1];

	s[1].sno = 222;
	strcpy(s[1].sname ,"BBB");
	s[1].sex = 'M';
	s[1].age = 19;
	s[1].next = NULL;
	s[1].next = &s[2];

	s[2].sno = 333;
	strcpy(s[2].sname ,"AAA");
	s[2].sex = 'M';
	s[2].age = 20;
	s[2].next = NULL;
	s[2].next = &s[3];

	s[3].sno = 444;
	strcpy(s[3].sname ,"CCC");
	s[3].sex = 'F';
	s[3].age = 18;
	s[3].next = NULL;
	s[3].next = &s[4];

	s[4].sno = 555;
	strcpy(s[4].sname ,"DDD");
	s[4].sex = 'M';
	s[4].age = 20;
	s[4].next = NULL;
	
	phead = &s[0];
	for(int i = 0;i<5;i++)
	{
		printf("\n%d %s %c %d",phead->sno ,phead->sname ,phead->sex ,phead->age );
		phead = phead->next ;
	}
	return 0;	
} 

从手推中找出规律,形成循环,如下
例一运用了数组。

例一:

#include<stdio.h>
#include<string.h>
struct student
{
	int sno;
	char sname[20];
	char sex;
	int age;
	student * next;
};
int main()
{
	int n ;
	student s[100],*phead;
	phead = NULL;

	printf("请输入需录入信息的学生人数:\n");
	scanf("%d",&n);

	for(int i = 0;i<n;i++)
	{
		scanf("%d %s %c %d",&s[i].sno ,&s[i].sname ,&s[i].sex ,&s[i].age );
		//链接
		s[i].next = NULL;
		if(i==0)
		{
			phead = &s[0];
		} 
		else
		{
			s[i-1].next = &s[i]; 
		}

	}
	phead = &s[0];
	for(int i = 0;i<5;i++)
	{
		printf("\n%d %s %c %d",phead->sno ,phead->sname ,phead->sex ,phead->age );
		phead = phead->next ;
	}
	return 0;
}

手推二:

#include<stdio.h>
#include<string.h>
struct student
{
	int sno;
	char sname[20];
	char sex;
	int age;
	student * next;
};
int main()
{
	//建立节点
	student s[100],*phead,*p;
	phead = NULL;
	p = NULL;
	//赋值

	scanf("%d %s %c %d",&s[0].sno ,&s[0].sname ,&s[0].sex ,&s[0].age );
	phead = &s[0];
	
	scanf("%d %s %c %d",&s[1].sno ,&s[1].sname ,&s[1].sex ,&s[1].age );
	phead->next = &s[1];
	
	scanf("%d %s %c %d",&s[2].sno ,&s[2].sname ,&s[2].sex ,&s[2].age );
	phead->next->next = &s[2];
	
	scanf("%d %s %c %d",&s[3].sno ,&s[3].sname ,&s[3].sex ,&s[3].age );
	phead->next->next->next = &s[3];
	
	scanf("%d %s %c %d",&s[4].sno ,&s[4].sname ,&s[4].sex ,&s[4].age );
	phead->next->next->next->next = &s[4];	 
	
	
	printf("\n%d %s %c %d",phead->sno ,phead->sname ,phead->sex ,phead->age );
	printf("\n%d %s %c %d",phead->next->sno ,phead->next->sname ,phead->next->sex ,phead->next->age );	
	printf("\n%d %s %c %d",phead->next->next->sno,phead->next->next->sname ,phead->next->next->sex ,phead->next->next->age );	
	printf("\n%d %s %c %d",phead->next->next->next->sno ,phead->next->next->next->sname ,phead->next->next->next->sex ,phead->next->next->next->age );	
	printf("\n%d %s %c %d",phead->next->next->next->next->sno ,phead->next->next->next->next->sname ,phead->next->next->next->next->sex ,phead->next->next->next->next->age );


	
	return 0;
} 

例二用到了两个指针。
例二:

#include<stdio.h>
#include<string.h>
struct student
{
	int sno;
	char sname[20];
	char sex;
	int age;
	student * next;
};
int main()
{
	int n;
	printf("请输入需录入信息的学生人数:\n");
	scanf("%d",&n);
	student s[100],*ptail,*phead;
	phead = NULL;
	ptail = NULL;
	
	for(int i = 0;i<n;i++)
	{//赋值 
		scanf("%d %s %c %d",&s[i].sno ,&s[i].sname ,&s[i].sex ,&s[i].age );

		if(i == 0)
		{	
			phead = &s[0];
			ptail = phead;	
		}	
		else
		{
			ptail->next = &s[i];
			ptail = ptail->next ;
		}		
			
	}
	phead = &s[0];
	for(int i = 0;i<n;i++)
	{
		printf("\n%d %s %c %d",phead->sno ,phead->sname ,phead->sex ,phead->age );
		phead = phead->next ;	
	}
	

	return 0;
}

对于链表:主要是要勤于动手,多画图才能理解的更好,要将手推过程中重复的部分放在循环里
即注意手工,找出规律。

在例子中出现了链表的遍历
可通过以下链接学习关于遍历的思想及方法:
原创
C语言:循环单链表的创建,遍历,插入

原创
循环链表的创建、遍历

二维数组例题

例:

#include<stdio.h>
int main()
{
	int a[2][3],i,j;
	printf("line:\n");
	for(i = 0;i<2;i++)
		for(j = 0;j<3;j++)
		scanf("%d",&a[i][j]);
		printf("array:\n");
		for(j = 0;j<2;j++)
		{
			for(i = 0;i<3;i++)
			printf("%4d",a[j][i]);
		printf("\n");
		}
		
	return 0;
} 

本例表示了引用二维数组元素,对二维数组进行输入输出的基本方法,其中
i为行下标,j为列下标。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值