#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;
}
}
這個代碼當使其中一個或兩個鏈表為空時報錯;
uj5u.com熱心網友回復:
求兩個非降序單鏈表的交集,例如將單鏈表1->2->2->3 和 2->2->3->3->5->7 的交集產生新單鏈表 2->3,只能輸出結果,不能修改兩個單鏈表的資料。【輸入形式】
第一行為第一個單鏈表的各結點值,以單個空格分隔。
第二行為第二個單鏈表的各結點值,以單個空格分隔。
【輸出形式】
交集的單鏈表,以升序排列(無重復值),值與值之間以單個空格分隔。
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
struct student {
int n;
struct student* next;
};
int number;
struct student *create_link() //創建未知鏈表;
{
struct student* p, * head,*q;
p = (struct student*)malloc(sizeof(struct student));
head = p;
scanf("%d", &p->n);
for (int i = 0;; i++)
{
q = (student*)malloc(sizeof(student));
p->next = q;
p = q;
scanf("%d", &p->n);
/*
if ((i == 0) && ((k = getchar()) == '\n'))
{
//p = NULL;
p->next = NULL;
break;
}
else if (i == 0)
{
p->n = (int)(k-'0');
q = (struct student*)malloc(sizeof(struct student));
p->next = q;
p = q;
}
else
{
//scanf_s("%d", &(p->n));
scanf("%d", &(p->n));
if ((k = getchar()) == '\n')
{
p->next = NULL;
break;
}
else
{
q = (student*)malloc(sizeof(student));
p->next = q;
p = q;
number++;
}
}
*/
if (getchar() == '\n')
break;
}
p->next = NULL;
return head;
}
void print_link(struct student *head)
{
struct student *p = head;
while (p) {
printf("%d ", p->n);
p = p->next;
}
printf("\n");
}
int search(struct student *phead, int n)
{
struct student *p = phead;
while (p) {
if (p->n == n)
return 1;
p = p->next;
}
return 0;
}
struct student * intersection(struct student *la, struct student *lb)
{
struct student *lc = NULL;
struct student *pa, *pb, *pc, *prev;
pa = la;
pb = lb;
while (pa && pb) {
if (pa->n == pb->n) {
if (search(lc, pa->n)) {
pa = pa->next;
pb = pb->next;
continue;
}
pc = (struct student *)malloc(sizeof(struct student));
if (!pc)
exit(0);
pc->n = pa->n;
if (!lc) {
lc = pc;
prev = pc;
} else {
prev->next = pc;
prev = pc;
}
}
if (pa->n > pb->n)
pb = pb->next;
if (pa->n < pb->n)
pa = pa->next;
}
prev->next = NULL;
return lc;
}
int main()
{
struct student *p, *q, *t;
//void print(struct student * p,struct student * q);//創建鏈表且為升序;
p = create_link();
//print_link(p);
q = create_link();
//print_link(q);
/*
if ((p == NULL) ||(q == NULL))
printf("NULL");
else
print(p, q);
*/
t = intersection(p, q);
print_link(t);
return 0;
}
#if 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;
}
}
#endif
供參考~
uj5u.com熱心網友回復:
我理解錯題的意思了轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/45977.html
標籤:C語言
上一篇:設計完整的程式實作以下功能:一個陣列有10個元素{1,8,10,2,-5,0,7,15,4,-5},利用指標作為函式引數,輸出陣列中最大和最小的元素值及其下標
