目录
由于之前链表是在考试前学的,考试完就忘记了好多,今天复习了链表的知识点,终于把链表题组的任务完成了,受益匪浅,对链表也有了更深入的认识,直接看题目吧。
链表注意事项:
1.链表是动态变化的,比如两个链表head=cur,(那么他们代表的就是同一内存空间,会相互影响)cur将链表的某结点进行修改后相应head的此结点也进行了改变
2、注意此链表有没有空的头节点
1、 链表插入
题目:
(线性表)已知一单链表,从第二个结点至表尾递增有序,(设a1<x<an)如下图(“第二个结点至表尾”指a1..an )。试编写程序,将第一个结点删除并插入表中适当位置,使整个链表递增有序。
输入格式
输入长度n:7
输入数据:4 1 2 3 6 8 9
输出格式
1 2 3 4 6 8 9
样例输入content_copy
5 11 7 8 9 10
样例输出content_copy
7 8 9 10 11
代码实现:
#include<stdio.h>
#include<stdlib.h>
struct node{//定义链表
int ch;
struct node *next;
};
struct node *creat(int n)//创建链表
{
struct node *temp,*head;
head=temp=(struct node * )malloc(sizeof(struct node));//申请空间
head=temp;
for(int i=0;i<n;i++)
{
struct node *p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->ch);
temp->next =p;
temp=p;
}
temp->next=NULL;
return head;
}
struct node *fun(struct node *head,int n)//将第一个结点删除并插入表中适当位置
{
struct node *previous,*current,*key;
key=head->next ; //记录插入结点(第一个结点)
current=head->next->next ; //当前结点(从第二个开始)
head=current; //首结点后移(第二个变成第一个,以此类推)
if(current->ch>=key->ch)//如果线性表本来就有序
{
return key;
}
while(current!=NULL&¤t->ch<=key->ch)//找到比第一个结点大的结点
{
previous=current;//记录要找结点的前一个结点
current=current->next;
}
if(current==NULL) //结点均不大于首结点,将插入结点挂在表尾
{
previous->next= key;
key->next = NULL;
}
else //将第一个结点插入在previous和current之间
{
previous->next=key;
key->next=current;
}
return head;
}
int main()
{
int n;
struct node *head;
scanf("%d",&n);
head=creat(n);
head=fun(head,n);
while (head!= NULL) {
printf("%d ", head->ch);
head = head->next;
}
return 0;
}
2、链表合并
题目:
线性表)假设有两个按元素值递增次序排列的线性表,均以单链表形式存储。请编写算法将这两个单链表归并为一个按元素值递减次序排列的单链表,并要求利用原来两个单链表的结点存放归并后的单链表。
输入格式
输入长度n:5
输入数据:1 2 5 6 8
输入长度m:5
输入数据:3 4 7 9 10
输出格式
10 9 8 7 6 5 4 3 2 1
样例输入content_copy
4 7 9 10 11 4 8 12 13 14
样例输出content_copy
14 13 12 11 10 9 8 7
代码实现:
#include<stdio.h>
#include<stdlib.h>
struct node{
int ch;
struct node *next;
};
struct node *creat(int n)
{
struct node *temp,*head;
head=temp=(struct node * )malloc(sizeof(struct node));
head=temp;
for(int i=0;i<n;i++)
{
struct node *p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->ch);
temp->next =p;
temp=p;
}
temp->next=NULL;
return head;
}
struct node *fun(struct node *head,int n)//冒泡排序
{
struct node *p,*q;
p=q=head->next ;
int temp;
for(int j=0;j<n-1;j++) //外循环
{
for(int i=0;i<n-1-j;i++)//内循环
{
if(p->ch < p->next->ch)
{
temp=p->ch;
p->ch=p->next->ch;
p->next->ch=temp;
}
p=p->next;//指向下一个结点
}
//q=q->next;
p=q;
}
return head;
}
int main()
{
int n,m;
struct node *head1,*head2,*p;
scanf("%d",&n);
head1=creat(n);//输入链表head1
scanf("%d",&m);
head2=creat(m);//输入链表head2
p=head1->next ;
while(p->next !=NULL)
{
p=p->next;
}
p->next =head2->next ;//将head2接到head1的后面
head1=fun(head1,m+n);
head1=head1->next;
while(head1!=NULL) //输出
{
printf("%d ",head1->ch);
head1 = head1->next;
}
return 0;
}
3、链表快速合并
题目描述
知L1、L2分别为两循环单链表的头结点指针,m,n分别为L1、L2表中数据结点个数。要求设计一算法,用最快速度将两表合并成一个带头结点的循环单链表。
输入格式
m=5
3 6 1 3 5
n=4
7 10 8 4
输出格式
3 6 1 3 5 7 10 8 4
样例输入content_copy
m=7 3 5 1 3 4 6 0 n=5 5 4 8 9 5
样例输出content_copy
3 5 1 3 4 6 0 5 4 8 9 5
代码实现:
#include<stdio.h>
#include<stdlib.h>
struct node{
int ch;
struct node *next;
};
struct node *creat(int n)
{
struct node *temp,*head;
head=temp=(struct node * )malloc(sizeof(struct node));
head=temp;
for(int i=0;i<n;i++)
{
struct node *p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->ch);
temp->next =p;
temp=p;
}
temp->next=NULL;
return head;
}
int main()
{
int n,m;
struct node *head1,*head2,*p;
p=(struct node *)malloc(sizeof(struct node));
scanf("m=%d",&m);
head1=creat(m);//输入链表head1
getchar();
getchar();
scanf("n=%d",&n);
head2=creat(n);//输入链表head2
p=head1->next ;
while(p->next !=NULL)
{
p=p->next;
}
p->next =head2->next ;//将head2接到head1的后面
head1=head1->next ;
while(head1!=NULL) //输出
{
printf("%d ",head1->ch);
head1 = head1->next;
}
return 0;
}
4、链表排序
题目描述
(线性表)已知不带头结点的线性链表list,链表中结点构造为(data、link),其中data为数据域,link为指针域。请写一算法,将该链表按结点数据域的值的大小从小到大重新链接。要求链接过程中不得使用除该链表以外的任何链结点空间。
输入格式
自定义链表节点数
m=5
3 1 5 4 6
输出格式
1 3 4 5 6
样例输入content_copy
8 10 1 5 14 32 55 67 6
样例输出content_copy
1 5 6 10 14 32 55 67
代码实现:
#include<stdio.h>
#include<stdlib.h>
struct node{//定义链表
int ch;
struct node *next;
};
struct node *creat(int n)//创建链表
{
struct node *temp,*head;
head=temp=(struct node * )malloc(sizeof(struct node));//申请空间
head=temp;
for(int i=0;i<n;i++)
{
struct node *p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->ch);
temp->next =p;
temp=p;
}
temp->next=NULL;
return head;
}
struct node *fun(struct node *head,int n)//冒泡排序
{
struct node *p,*q;
p=q=head->next ;
int temp;
for(int j=0;j<n-1;j++) //外循环
{
for(int i=0;i<n-1-j;i++)//内循环
{
if(p->ch > p->next->ch)
{
temp=p->ch;
p->ch=p->next->ch;
p->next->ch=temp;
}
p=p->next;//指向下一个结点
}
q=q->next;
p=q;
}
return head;
}
int main()
{
int n;
struct node *head;
scanf("%d",&n);
head=creat(n);
head=fun(head,n);
head=head->next ;
while(head!=NULL)
{
printf("%d ",head->ch);
head = head->next;
}
return 0;
}