闲鱼帮别人写的一些链表题目,可以拿去用或者学习,请勿复制。
初识链表
题目描述
已知一个正整数序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后统计这个序列中元素的最大值与最小值,计算序列全部元素之和。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。
输入
一个正整数序列,正整数序列元素的个数未知,但以输入“-1”结束,输入“-1”前至少输入一个正整数。序列中的元素范围在1—999999999之间。
输出
三个正整数,即最大值、最小值、所有元素之和。 数据最多的测试用例节点数在1000这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
样例输入
1 4 99 21 50 61 32 4 -1
样例输出
The maximum,minmum and the total are:99 1 272
#include<stdio.h>
#include<malloc.h>
typedef struct LNode
{
int data;
struct LNode * next;
}LNode,*LinkList;
LinkList createlist(LinkList head)
{
int temp;
LinkList p;
while(scanf("%d",&temp)!=EOF)
{
if(temp==-1) break;
p=(LinkList)malloc(sizeof(LNode));
p->data=temp;
p->next=head->next;
head->next=p;
}
return head;
}
//void ListPint(LinkList head)
//{
// LinkList p;
// p=head->next;
// while(p!=NULL)
// {
// printf("%d\n",p->data);
// p=p->next;
// }
//}
void findAndSum(LinkList head)
{
LinkList p;
p=head->next;
int min = p->data,max=p->data;
int sum=0;
while(p!=NULL)
{
if(p->data<=min)
{
min=p->data;
}
if(p->data>max){
max=p->data;
}
sum+=p->data;
p=p->next;
}
printf("The maximum,minmum and the total are:%d %d %d",max,min,sum);
}
int main()
{
int i;
LinkList head;
head=(LinkList)malloc(sizeof(LNode));
head->next=NULL;
head = createlist(head);
findAndSum(head);
}
链表排序
题目描述
已知一个正整数组成的无序序列,个数未知,但至少有一个元素,你的任务是建立一个单链表,并使用该链表存储这个正整数序列,然后将这个链表进行排序,使得排序后的链表为递增序列。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在排序的过程中,你可以自己选择排序算法(冒泡排序、选择排序等),但必须是通过修改结点的指针域来进行排序,而不是对结点的数据域进行修改。程序结束后要释放所有节点占据的空间。
输入
一个元素个数未知的正整数序列,以输入“-1”结束,输入“-1”前至少输入一个正整数。
输出
经过排序后的链表,每个元素后有一个空格,注意最后一个元素后只有换行符。
数据最多的测试用例节点数在1000这个数量级,所有整数可以用int型存储。 请注意输入输出格式。
样例输入
49 38 65 97 76 13 27 49 -1
样例输出
The new list is:13 27 38 49 49 65 76 97
#include<stdio.h>
#include<malloc.h>
typedef struct LNode
{
int data;
struct LNode * next;
}LNode,*LinkList;
LinkList createlist(LinkList head)
{
int temp;
LinkList p;
while(scanf("%d",&temp)!=EOF)
{
if(temp==-1) break;
p=(LinkList)malloc(sizeof(LNode));
p->data=temp;
p->next=head->next;
head->next=p;
}
return head;
}
void ListPint(LinkList head)
{
LinkList p;
p=head->next;
printf("The new list is:");
while(p!=NULL)
{
if(p==head->next)
printf("%d",p->data);
else printf(" %d",p->data);
p=p->next;
}
}
void sortList(LinkList head)
{
LinkList p,q;
p=head->next;
while(p!=NULL){
q=p;
while(q!=NULL){
if(q->data<p->data){
int temp=p->data;
p->data=q->data;
q->data=temp;
}
q=q->next;
}
p=p->next;
}
}
int main()
{
int i;
LinkList head;
head=(LinkList)malloc(sizeof(LNode));
head->next=NULL;
head = createlist(head);
sortList(head);
ListPint(head);
}
链表匹配
题目描述
已知两个由正整数组成的无序序列A、B,每个序列的元素个数未知,但至少有一个元素。你的任务是判断序列B是否是序列A的连续子序列。假设B是“1 9 2 4 18”,A是“33 64 1 9 2 4 18 7”,B是A的连续子序列;假设B是“1 9 2 4 18”,A是“33 1 9 64 2 4 18 7”,B不是A的连续子序列。
要求:
建立两个单链表A、B用于存储两个正整数序列,然后按照题目的要求,判断链表B是否是链表A的连续子序列。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。在程序结束前要释放链表A、B中的所有节点。
输入
依次输入两个乱序的正整数序列A、B,序列中元素个数未知,但每个序列至少有一个元素,并以输入“-1”结束,每个序列占一行。
输出
如果序列B是序列A的连续子序列,则输出“ListB is the sub sequence of ListA.”,否则输出“ListB is not the sub sequence of ListA.”。 数据最多的测试用例节点数在100这个数量级,所有整数可以用int型存储。
请注意输入输出格式。
样例输入 Copy
Sample 1:
5 4 3 2 1 -1
3 2 1 -1
Sample 2:
1 2 3 4 5 6 7 8 9 -1
1 2 3 4 5 6 7 8 0 -1
样例输出
Sample 1:
ListB is the sub sequence of ListA.
Sample 2:
ListB is not the sub sequence of ListA.
#include<stdio.h>
#include<malloc.h>
typedef struct LNode {
int data;
struct LNode * next;
} LNode,*LinkList;
LinkList createlist(LinkList head) {
int temp;
LinkList p=head,q;
while(scanf("%d",&temp)!=EOF) {
if(temp==-1) break;
q=(LinkList)malloc(sizeof(LNode));
q->data=temp;
q->next=NULL;
p->next=q;
p=p->next;
}
return head;
}
void ansPint(int num) {
if(num==1){
printf("ListB is the sub sequence of ListA.");
}else{
printf("ListB is not the sub sequence of ListA.");
}
}
void print(LinkList head)
{
LinkList p;
p=head->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void judgeLists(LinkList headA,LinkList headB) {
LinkList p,q,g;
p=headA->next;
while(p!=NULL) {
q=headB->next;
if(p->data==q->data){
g=p;
while(q!=NULL&&g!=NULL){
if(g->data!=q->data) break;
g=g->next;
q=q->next;
}
if(q==NULL){
ansPint(1);
return;
}
}
p=p->next;
}
ansPint(0);
}
int main() {
int i;
LinkList headA,headB;
headA=(LinkList)malloc(sizeof(LNode));
headB=(LinkList)malloc(sizeof(LNode));
headA->next=NULL;
headB->next=NULL;
headA = createlist(headA);
headB = createlist(headB);
// print(headA);
// print(headB);
judgeLists(headA,headB);
}
链表交换
题目描述
已知一个正整数序列,序列元素个数未知,但至少有两个元素,你的任务是建立一个单链表用于存储这个正整数序列。然后实现交换此链表中任意指定的两段,第一段为[s1,t1],第二段[s2,t2]。s1、t1、s2、t2代表链表的第几个节点,且满足s1<=t1,s2<=t2,t1<s2,s2一定小于等于链表节点的总个数。正整数的输入用-1作为结束标志,注意-1不算这个正整数序列中的元素(不要统计-1)。最后将链表的全部节点释放。
输入
输入一个正整数序列,以输入“-1”结束,序列中元素个数未知,但输入“-1”前至少输入两个正整数。然后是四个整数,即为s1、t1、s2、t2。
输出
经过处理后的新链表,每个元素后有一个空格,注意最后一个元素后只有换行符。
数据最多的测试用例节点数在100这个数量级,所有整数可以用int型存储。 请注意输入输出格式。
样例输入
1 2 3 4 5 6 7 8 9 10 -1
1 1 4 7
样例输出
The new list is:4 5 6 7 2 3 1 8 9 10
#include<stdio.h>
#include<malloc.h>
typedef struct LNode {
int data;
struct LNode * next;
} LNode,*LinkList;
LinkList createlist(LinkList head) {
int temp;
LinkList p=head,q;
while(scanf("%d",&temp)!=EOF) {
if(temp==-1) break;
q=(LinkList)malloc(sizeof(LNode));
q->data=temp;
q->next=NULL;
p->next=q;
p=p->next;
}
return head;
}
void print(LinkList head)
{
LinkList p;
p=head->next;
printf("The new list is:");
while(p!=NULL){
if(p==head->next){
printf("%d",p->data);
}else{
printf(" %d",p->data);
}
p=p->next;
}
}
void changeLists(LinkList head) {
LinkList p,S1,S2,T1,T2,g;
p=head;
int s1,t1,s2,t2;
scanf("%d %d %d %d",&s1,&t1,&s2,&t2);
int k=0;
while(p!=NULL){
if(k==s1-1){
S1=p;
}
if(k==s2-1){
S2=p;
}
if(k==t1){
T1=p;
}
if(k==t2){
T2=p;
}
k++;
p=p->next;
}
g=S1->next;
S1->next=S2->next;
S2->next=g;
g=T1->next;
T1->next=T2->next;
T2->next=g;
}
int main() {
int i;
LinkList head;
head=(LinkList)malloc(sizeof(LNode));
head->next=NULL;
head = createlist(head);
changeLists(head);
print(head);
}