|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
struct student {
int n;
struct student* next;
};
int number;
struct student *a() //创建未知链表;
{
struct student* p, * head,*q;
p = (student*)malloc(sizeof(student));
head = p;
char k;
for (int i = 0;; i++)
{
if ((i == 0) && ((k = getchar()) == '\n'))
{
p=NULL;
break;
}
else if (i == 0)
{
p->n = (int)(k-'0');
q = (student*)malloc(sizeof(student));
p->next = q;
p = q;
}
else
{
scanf_s("%d", &(p->n));
if ((k = getchar()) == '\n')
{
p->next = NULL;
break;
}
else
{
q = (student*)malloc(sizeof(student));
p->next = q;
p = q;
number++;
}
}
}
return head;
}
int main()
{
struct student*p,*q;
void print(struct student * p,struct student * q);
p = a();
q = a();
if ((p == NULL) ||(q == NULL))
printf("NULL");
else
print(p, q);
return 0;
}
void print(struct student* p,struct student* q)//打印两个链表的交集;
{
struct student* q1 = q, * p1 = p;
for (int i = 0;; i++)
{
if (p->next != NULL)
{
if ((p->n) == (p->next->n))
p = p->next;
}
for (int j = 0;; j++)
{
if (q->next != NULL)
{
if ((q->n) == (q->next->n))
q = q->next;
}
if (p->n == q->n)
{
printf("%d ", p->n);
q = q1;
break;
}
else
{
if (q->next == NULL)
{
q = q1;
break;
}
else
q = q->next;
}
}
if (p->next == NULL)
break;
else
p = p->next;
}
}
这个程序在使其中一个或两个链表为NULL时报错,不知道怎么回事,求大佬帮忙!O.O |
|