2024/03/14作业

一、链表的操作

1.重定义int和定义一个结构体

typedef int datatype;
typedef struct node
{
	union
	{
		datatype data;
		int len;
	};
	struct node *next;
}node,*node_p;

2.创建单链表

node_p create_link_list()
{
	node_p H=(node_p)malloc(sizeof(node));
	if(H==NULL)
		return NULL;
	H->len=0;
	H->next=NULL;
	return H;
}

3.创建节点

node_p create_node(datatype data)
{
	node_p new=(node_p)malloc(sizeof(node));
	if(new==NULL)
		return NULL;
	new->data=data;
	new->next=NULL;
	return new;
}

4.头插

void insert_head(node_p H,datatype data)
{
	if(H==NULL)
		return;
	node_p new=create_node(data);
	new->next=H->next;
	H->next=new;
	H->len++;
}

5.判空

int empty_link(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return -1;
	}
	return H->next==NULL?1:0;
}

6.头删

void dele_head(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return ;
	}
	if(empty_link(H))
	{
		printf("该链表为空,无需删除\n");
		return;
	}
	node_p dele=H->next;
	H->next=dele->next;//H->next=H->next->next;
	free(dele);
	H->len--;
}

7.输出

void show_link(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return ;
	}
	node_p p=H->next;
	while(p!=NULL)
	{
		printf("%d->",p->data);
		p=p->next;
	}
	printf("NULL\n");
}

8.尾插

void insert_tail(node_p H,datatype data)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return ;
	}
	node_p p = H->next;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	node_p new =create_node(data);
	p->next=new;
	new->next=NULL;
	H->len++;
}

9.尾删

void dele_tail(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return ;
	}
	node_p p=H;
	while(p->next->next!=0)
	{
		p=p->next;
	}
	node_p dele = p->next;
	free(dele);
	p->next=NULL;
	H->len--;
}

10.按位置插入

void insert_pos(node_p H,int pos,datatype data)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return ;
	}
	if(pos<1||pos>H->len+1)
	{
		printf("位置不合理\n");
		return;
	}
	node_p p = H;
	for(int i=0;i<pos-1;i++)
	{
		p=p->next;
	}
	node_p new = create_node(data);
	new->next=p->next;
	p->next = new;
	H->len++;
}

11.按位置删除

void dele_pos(node_p H,int pos)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return ;
	}
	if(empty_link(H))
	{
		printf("表已空,无需删除\n");
		return ;
	}
	if(pos<1||pos>H->len)
	{
		printf("位置不合理\n");
		return;
	}
	node_p p = H;
	for(int i=0;i<pos-1;i++)
	{
		p=p->next;
	}
	node_p dele = p->next;
	p->next = dele->next;
	free(dele);
	H->len--;
}

12.按值查找,返回值位置

int search_value(node_p H,datatype key)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return -1;
	}
	node_p p = H;
	int count=0;
	while(p->data!=key)
	{
		count++;
		p = p->next;
	}
	return count;
}

13.按位置查找,返回值

datatype search_pos(node_p H,int pos)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return -1;
	}
	if(pos<1||pos>H->len)
	{
		printf("位置不合理\n");
		return -2;
	}
	node_p p = H;
	for(int i = 0;i<pos;i++)
	{
		p = p->next;
	}
	return p->data;
}

14.链表逆置

void invert_link(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return ;
	}
	node_p temp = H;
	for(int i = H->len;i>=1;i--)
	{
		temp = H->next;
		H->next = H->next->next;
		insert_pos(H,i,temp->data);
		H->len--;
	}
}

15.释放单链表

void free_link(node_p H)
{
	if(H==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	node_p p;
	while(H->next!=0)
	{
		p=H->next;
		H->next=H->next->next;
		free(p);
	}
}

二、练习题

1.题目

2.代码

#include<stdio.h>
int main(int argc, char *argv[])
{
	printf("请输入行和列\n");
	int n,m;
	scanf("%d %d",&n,&m);
	printf("请输入矩阵\n");
	int a[n][m];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	int b[n],c[m];
	for(int i=0;i<n;i++)
	{	
		b[i]=1;
		for(int k=0;k<m;k++)
		{
			b[i]*=a[i][k];
		}
	}
	for(int i=0;i<m;i++)
	{
		c[i]=1;
		for(int l=0;l<n;l++)
			{
				c[i]*=a[l][i];
			}
	}
	int d[n*m];
	int count=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{	
			d[count]=b[i]*c[j]/a[i][j]/a[i][j];
			count++;
		}
	}
	int result=d[0];
	for(int i=1;i<m*n;i++)
	{
		if(result<d[i])
			result=d[i];
	}
	printf("%d\n",result);
      return 0;
}

 3.运行结果

ubuntu@ubuntu:2$ gcc 2.c
ubuntu@ubuntu:2$ ./a.out
请输入行和列
3 5
请输入矩阵
5 1 8 5 2
1 3 10 3 3
7 8 5 5 16
560
358400

三、思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值