Home > Back-end >  List problem
List problem

Time:09-18

#include
#include
Struct student {
int n;
Struct student * next;
};
Int number;
Struct student * (a)//create unknown list;
{
Struct student * p, * head, * q;
P=(student *) malloc (sizeof (student));
The head=p;
Char k;

For (int I=0;; I++)
{
If (I==0) & amp; & (k=getchar ())=='\ n'))
{
P=NULL;
break;
}
Else if (I==0)
{
P - & gt; N=(int) (k - '0');
Q=(student *) malloc (sizeof (student));
P - & gt; Next=q;
P=q;
}
The else
{
Scanf_s (" % d ", & amp; (p - & gt; N));
If (k=getchar ())=='\ n')
{
P - & gt; Next=NULL;
break;
}
The else
{
Q=(student *) malloc (sizeof (student));
P - & gt; Next=q;
P=q;
number++;
}
}
}
Return the head;
}

Int main ()
{
Struct student * p, * q;
Void print (struct student * p, struct student * q);//create a linked list and the ascending;
P=(a);
Q=(a);
If ((p==NULL) | | (q==NULL))
Printf (" NULL ");
The else
Print (p, q);
return 0;
}
Void print (struct student * p, struct student * q)//print the intersection of two linked list
{
Q1=q struct student *, * p1=p;
For (int I=0;; I++)
{
If (p - & gt; Next!=NULL)
{
If ((p - & gt; N)==(p - & gt; Next - & gt; N))
P=p - & gt; Next;
}
For (int j=0;; J++)
{
If (q - & gt; Next!=NULL)
{
If ((q - & gt; N)==(q - & gt; Next - & gt; N))
Q=q - & gt; Next;
}
If (p - & gt; N==q - & gt; N)
{

Printf (" % d ", p - & gt; n);
Q=q1;
break;
}
The else
{
If (q - & gt; Next==NULL)
{
Q=q1;
break;
}
The else
Q=q - & gt; Next;

}

}
If (p - & gt; Next==NULL)
break;
The else
P=p - & gt; Next;
}
}
This code when making one or two linked list is empty times wrong;

CodePudding user response:

For two at the intersection between the descending singly linked lists, such as single table 1 & gt; 2 - & gt; 2 - & gt; 3 and 2 - & gt; 2 - & gt; 3 - & gt; 3 - & gt; 5 - & gt; At the intersection between 7 to generate new singly linked list 2 - & gt; 3, can only output, changing data of two singly linked lists,

[form] input

The value of the first behavior first singly linked lists of each node, with a single space space,

The second act singly linked lists the second value of each node, with a single space space,

[] output form

Intersection singly linked lists to ascending order (no duplicate values), and values are separated by a single space,

CodePudding user response:

 # include 
#include

Struct student {
int n;
Struct student * next;
};

Int number;

Struct student * create_link ()//create unknown list;
{
Struct student * p, * head, * q;
P=(struct student *) malloc (sizeof (struct student));
The head=p;
The scanf (" % d ", & amp; P - & gt; n);

For (int I=0;; I++)
{
Q=(student *) malloc (sizeof (student));
P - & gt; Next=q;
P=q;
The scanf (" % d ", & amp; P - & gt; n);

/*
If (I==0) & amp; & (k=getchar ())=='\ n'))
{
//p=NULL;
P - & gt; Next=NULL;
break;
}
Else if (I==0)
{
P - & gt; N=(int) (k - '0');
Q=(struct student *) malloc (sizeof (struct student));
P - & gt; Next=q;
P=q;
}
The else
{
//scanf_s (" % d ", & amp; (p - & gt; N));
The scanf (" % d ", & amp; (p - & gt; N));
If (k=getchar ())=='\ n')
{
P - & gt; Next=NULL;
break;
}
The else
{
Q=(student *) malloc (sizeof (student));
P - & gt; Next=q;
P=q;
number++;
}
}
*/
If (getchar ()=='\ n')
break;
}
P - & gt; Next=NULL;
Return the head;
}

Void print_link (struct student * head)
{
Struct student * p=head;

While (p) {
Printf (" % d ", p - & gt; n);
P=p - & gt; Next;
}
printf("\n");
}

Int search (struct student * phead, int n)
{
Struct student * p=phead;

While (p) {
If (p - & gt; N==n)
return 1;
P=p - & gt; Next;
}

return 0;
}

Struct student * intersection computes (struct student * la, struct student * lb)
{
Struct student * lc=NULL;
Struct student * pa, pb, PC, * prev.

Pa=la;
Pb=lb;

While (pa & amp; & Pb) {
If (pa - & gt; N==pb - & gt; N) {
If (search (lc, pa - & gt; N)) {
Pa=pa - & gt; Next;
Pb=pb - & gt; Next;
continue;
}
PC=(struct student *) malloc (sizeof (struct student));
if (! PC)
exit(0);
PC - & gt; N=pa - & gt; n;
if (! Lc) {
Lc=PC;
Prev=PC;
} else {
Prev - & gt; Next=PC;
Prev=PC;
}

}
If (pa - & gt; N & gt; Pb - & gt; N)
Pb=pb - & gt; Next;
If (pa - & gt; N & lt; Pb - & gt; N)
Pa=pa - & gt; Next;
}
Prev - & gt; Next=NULL;

Return the lc;
}

Int main ()
{
Struct student * p, * q, * t;
//void print (struct student * p, struct student * q);//create a linked list and the ascending;
P=create_link ();
//print_link (p);
Q=create_link ();
//print_link (q);
/*
If ((p==NULL) | | (q==NULL))
Printf (" NULL ");
The else
Print (p, q);
*/
T=intersection computes (p, q);

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related