两个有序链表序列的交集(c语言)
发布日期:2021-05-06 03:53:29 浏览次数:14 分类:技术文章

本文共 1249 字,大约阅读时间需要 4 分钟。

两个有序链表序列的交集

题目

在这里插入图片描述

答案

#include
#include
struct Array{
int data; struct Array *next;};int main(){
struct Array *head,*head1,*pa,*pb,*head2,*p,*temp; head1=(struct Array *)malloc(sizeof(struct Array)); head1->next=NULL; p=head1; int t; while(1) {
scanf("%d",&t); if(t==-1) break; else {
temp=(struct Array *)malloc(sizeof(struct Array)); temp->data=t; temp->next=NULL; p->next=temp; p=p->next; } } head2=(struct Array *)malloc(sizeof(struct Array)); head2->next=NULL; p=head2; while(1) {
scanf("%d",&t); if(t==-1) break; else {
temp=(struct Array *)malloc(sizeof(struct Array)); temp->data=t; temp->next=NULL; p->next=temp; p=p->next; } } head=(struct Array *)malloc(sizeof(struct Array)); head->next=NULL; p=head; pa=head1->next; pb=head2->next; while(pa&&pb) {
if(pa->data==pb->data) {
p->next=pa; p=p->next; pa=pa->next; pb=pb->next; } else if(pa->data
data) pa=pa->next; else if(pa->data>pb->data) pb=pb->next; } p->next=NULL; int flag=0; if(head->next==NULL) {
printf("NULL"); return 0; } while(head->next) {
if(flag==0) {
printf("%d",head->next->data); flag=1; } else printf(" %d",head->next->data); head=head->next; }}
上一篇:两个有序序列的中位数(使用sort函数)
下一篇:海盗分赃(8行代码搞定!)

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月29日 02时41分46秒