链表 C语言

题目 1052: [编程入门]链表合并]

//学习链表记录
#include<stdio.h>
#include<stdlib.h>

//结构体,然后区分结构体是内部的最后一行
//struct Student *next;
typedef struct Student{
	int num;
	int grade;
	struct Student *next; 
}Node,*node;

//https://blog.csdn.net/endeavor_g/article/details/80552680
//链表理论学习
node creat(int n);//创建链表 
void unite(node head1,node head2);// 结合链表 
//这个有点像union联合函数
void order(node head,int sum);//排序链表 
void output(node head);//打印链表

//主函数
int main(){
	int a,b;
	scanf("%d%d",&a,&b);//输入的两个链表元素数量 
	node head1=creat(a);//创造两个链表 ,head1,2为头节点 
	node head2=creat(b);
	unite(head1,head2);//结合两个链表 
	order(head1,a+b);//排序两个链表 
	return 0;
} 

//创建链表
/*
LinkList *creat(int n){
	LinkList *head, *node, *end;//定义头节点,普通节点,尾部节点;
	head = (LinkList*)malloc(sizeof(LinkList));//分配地址
	end = head;         //若是空链表则头尾节点一样
	for (int i = 0; i < n; i++) {
		node = (LinkList*)malloc(sizeof(LinkList));
		scanf("%d", &node->score);
		end->next = node;
		end = node;
	}
	end->next = NULL;//结束创建
	return head;
}
*/
node creat(int n){
	node head=(node)malloc(sizeof(Node));//head为头结点 ,尾插创造 
	head->next=NULL;
	node p=head;//p为头指针 
	for(int i=0;i<n;i++){
		node q=(node)malloc(sizeof(Node));
		q->next=NULL;//q是结尾节点
		p->next=q;
		p=q;
		scanf("%d%d",&(*q).num,&(*q).grade);
	}
	return head;
}

//结合两个链表
void unite(node head1,node head2){
	node p;
	p=head1;
	while(p->next!=NULL)
	p=p->next;
	p->next=head2->next;
	head2->next=NULL;
}

//排序两个链表
void order(node head,int sum){
	node p=head->next;//p指向首结点 
	int t1,t2;//t1学号中间变量,t2成绩中间变量
	int cnt=0;//cnt为链表长度,这里选用冒泡排序 
	while(cnt<sum){
		node q=p;
		while(q->next!=NULL){
			if((q->num)>(q->next->num)){
				t1=q->num;
				q->num=q->next->num;
				q->next->num=t1;
				t2=q->grade;
				q->grade=q->next->grade;
				q->next->grade=t2;
			}
			q=q->next;
		}
		cnt++;
	} 
	output(head);
}

//输出两个链表
void output(node head){
	node p=head->next;
	node q;
	while(p!=NULL){
	printf("%d %d\n",p->num,p->grade);
	q=p;
	p=p->next;
	free(q);
	}
}

c语言链表详解(超详细)_Mr.Gzj的博客-CSDN博客_链表c语言

题目 1052: [编程入门]链表合并_海琴烟Sunshine的博客-CSDN博客

这个题目在我做之前,我是不会使用链表去运行的,因为我不太好使用链表,同时在我看了链表的相关代码后,太过于复杂,所以我直接使用结构去进行运行,代码如下:

#include<stdio.h>
typedef struct student{
    int hao;
    int mark;
}stu;
    stu a[100001];
int main()
{
    int i,j,k,n,m;
    int te,temp;
    scanf("%d %d\n",&n,&m);
    int s=n+m;
    for(i=0;i<s;i++)
    {
    scanf("%d %d\n",&a[i].hao,&a[i].mark);   
    }
    
    //采用冒泡排序法
    for(j=1;j<i;j++)
    {
        for(k=0;k<i-j;k++)//从第一个数开始,一直到倒数第二个数,比较大小
        //然后是第一个数开始,一直到倒数第三个数,比较大小,依次
        {
            if(a[k+1].hao<a[k].hao)
            {
                temp=a[k].hao;
                a[k].hao=a[k+1].hao;
                a[k+1].hao=temp;
                
                te=a[k].mark;
                a[k].mark=a[k+1].mark;
                a[k+1].mark=te;
            }
        }
}
    for(i=0;i<s;i++)
    {
    printf("%d %d\n",a[i].hao,a[i].mark);   
    }
    return 0;
}

题目 1585: 

蓝桥杯算法训练VIP-链表数据求和操作

#include<stdio.h>
#include<stdlib.h>
int snum=0,sgra=0;
typedef struct Student{
	int num;//假设num=实数
	int grade;//假设grade=虚数的前面数字
	struct Student *next; 
}Node,*node;
node creat(int n);//创建链表 
//void unite(node head1,node head2);// 结合链表 
void order(node head,int sum);//排序链表 
void output(node head);//打印链表
int main(){
	int a,b;
	node head=creat(10);//创造两个链表 ,head1,2为头节点 
	//node head2=creat(b);
	//unite(head1,head2);//结合两个链表 
	order(head,a+b);//排序两个链表
	//output(head1);
	return 0;
} 
node creat(int n){
	node head=(node)malloc(sizeof(Node));//head为头结点 ,尾插创造 
	head->next=NULL;
	node p=head;//p为头指针 
	for(int i=0;i<n;i++){
		node q=(node)malloc(sizeof(Node));
		q->next=NULL;
		p->next=q;
		p=q;
		scanf("%d %d",&(*q).num,&(*q).grade);
	}
	return head;
}

/*
void unite(node head1,node head2){
	node p;
	p=head1;
	while(p->next!=NULL)
	p=p->next;
	p->next=head2->next;
	head2->next=NULL;
}*/
void order(node head,int sum){
	node p=head->next;//p指向首结点 
	int t1,t2;//t1学号中间变量,t2成绩中间变量
	int cnt=0;//cnt为链表长度,这里选用冒泡排序 
/*	while(cnt<sum){
		node q=p;
		while(q->next!=NULL){
			if((q->num)>(q->next->num)){
				t1=q->num;
				q->num=q->next->num;
				q->next->num=t1;
				t2=q->grade;
				q->grade=q->next->grade;
				q->next->grade=t2;
			}
			q=q->next;
		}
		cnt++;
	} */
	output(head);
}
void output(node head){
	node p=head->next;
	node q;
	while(p!=NULL){
	snum=snum+p->num;
	sgra=sgra+p->grade;
	//printf("%d %d\n",p->num,p->grade);
	q=p;
	p=p->next;
	free(q);
	}
	printf("%d+%di",snum,sgra);
}

下面这道求和的题目,直接在上面的程序上进行修改就可以,区别在于不需要排序和整合两个链表,只需要进行链表求和就可以,此处要注意修改的是输入和输出,然后把不需要的注释掉就可以了

题目 1676: 

数据结构-链表的基本操作

#include<stdio.h>
#include<stdlib.h>
#include<string.h>                                                                                           
typedef struct LN{                                                                                       
	int x;
	struct LN *next;                                                                                          
}lnode,*Lnode;
Lnode creat(int);//创建链表 
void show(Lnode);//展示链表中的元素 
void get(Lnode,int);//得到链表中的元素 
void delete_(Lnode,int);//删除链表中的元素 
void insert(Lnode,int,int);//插入
int main(){
	int n;
	scanf("%d",&n);//列表内数量
	getchar();//输入字符
	Lnode head=creat(n);//创建链表
	
	int m;
	scanf("%d",&m);//m是第二行下面输入的行数
	getchar();
	char str[20];
	for(int i=0;i<m;i++){
		scanf("%s",str);
		getchar();
		
		
		if(strcmp(str,"show")==0){//strcmp对比str和show,如果相等则为0
			show(head);
		}
		if(strcmp(str,"get")==0){
			int a;
			scanf("%d",&a);
			getchar();
			get(head,a);
		}
		if(strcmp(str,"delete")==0){
			int b;
			scanf("%d",&b);
			getchar();
			delete_(head,b);
		}
		if(strcmp(str,"insert")==0){
			int c,d;
			scanf("%d%d",&c,&d);
			getchar();
			insert(head,c,d);
		}
	}
	return 0;
}
//创建链表
Lnode creat(int n){
	Lnode head=(Lnode)malloc(sizeof(lnode));
	head->next=NULL;
	for(int i=0;i<n;i++){//采用头插法 
		Lnode p=(Lnode)malloc(sizeof(lnode));
		scanf("%d",&(*p).x);
		getchar();
		p->next=head->next;
		head->next=p;
	}
	return head;
}

//show
void show(Lnode head){
	Lnode p=head->next;
	if(p==NULL){
		printf("Link list is empty\n");
	}
	else{
		while(p!=NULL){
			printf("%d ",p->x);
			p=p->next;
		}
		printf("\n");
	}
}
//get
void get(Lnode head,int a){
	Lnode p=head->next;
	int j=1;
	while(p&&j<a){
		p=p->next;
		j++;
	}
	if(!p||j>a){
		printf("get fail\n");
	}
	else{
		printf("%d\n",p->x);
	}
}

//delete_
void delete_(Lnode head,int b){
	Lnode p=head;
	int j=0;
	while(p->next&&j<b-1){
		p=p->next;
		j++;
	}
	if(!(p->next)||(j>b-1)){
		printf("delete fail\n");
	}
	else{
		Lnode q=p->next;
		p->next=q->next;
		printf("delete OK\n");
		free(q);
	}
}
//insert
void insert(Lnode head,int c,int d){
	Lnode p=head;
	int j=0;
	while(p&&j<c-1){
		p=p->next;
		j++;
	}
	if(!p||(j>c-1)){
		printf("insert fail\n");
	}
	else{
		Lnode q=(Lnode)malloc(sizeof(lnode));
		q->next=p->next;
		p->next=q;
		q->x=d;
		printf("insert OK\n");
	}
}

题目 1676: 数据结构-链表的基本操作_海琴烟Sunshine的博客-CSDN博客

最后一个基本操作,里面get delete  还有插入三个算法,主要看题目给的算法,里面的一些细节,一定要注意,比如说+1或者硕士p开头是什么

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值