Home > Back-end >  Known to take the lead node singly linked lists, L design algorithm implementation: in the first ele
Known to take the lead node singly linked lists, L design algorithm implementation: in the first ele

Time:09-21

Requirements:
1, describe the singly linked list storage structure definition;
2, create a singly linked list of algorithms;
The algorithm of 3, complete the topic request;
4, the test subject tests executed correctly the main function main ();
5, design test cases to test data (input).
(6, test result can be output screenshots)
 # include & lt; Stdio. H> 
#include
#include

/* node structure definition */
Typedef struct Node {
The int data;
Struct Node * next;
} Node, * LinkList;

Establish a singly linked list *//*
LinkList creatList (LinkList L, int n) {
The Node * p, * q;
int i;
L=(LinkList) malloc (sizeof (Node));/* established head node */
L - & gt; Next=NULL;/* create empty table */
P=(LinkList) malloc (sizeof (Node));
Printf (" the first node value: ");
The scanf (" % d ", & amp; (p - & gt; Data));
P - & gt; Next=L - & gt; next;
L - & gt; Next=p;
Q=p;/* insert the first node, for statement into the rest of the node */
For (I=n - 1; i> 0; I -) {
P=(LinkList) malloc (sizeof (Node));
Printf (" % d node values: ", n + 1 - I);
The scanf (" % d ", & amp; (p - & gt; Data));
P - & gt; Next=q - & gt; next;
Q - & gt; Next=p;
Q=p;
}
return L;
}

Void show (LinkList L) {
Node * p;
For (p=L - & gt; next; P!=NULL; P=p - & gt; Next) {
Printf (" % d ", p - & gt; The data);
}
}
/*, according to the input of La in Lb
1, a2, a=b
3, a> B
*/
LinkList compare (LinkList La, LinkList Lb) {
The Node * p * q * r;
LinkList Lc;
P=La - & gt; next;
Q=Lb - & gt; next;
Lc=La;
Lc - & gt; Next=NULL;
R=Lc;
While (p!=NULL & amp; & Q!=NULL) {
If (p - & gt; Data<=q - & gt; Data) {
R - & gt; Next=p;
R=p;
P=p - & gt; next;
}
The else {
R - & gt; Next=q;
R=q;
Q=q - & gt; next;
}
If (p) {r - & gt; Next=p; }
The else {r - & gt; Next=q; }
//printf (" \ n ", Lc);
}
Return (Lc);
}

The main () {
LinkList La, Lb, Lc;
La=creatList (La, 3);
Printf (" La input values in the order: \ n ");
Show (La);
printf("\n");
Lb=creatList (Lb, 3);
Lb input values in the order: printf (" \ n ");
Show (Lb);
Lc=compare (La and Lb);
Printf (" \ n input values in the order: \ n ");
Show (Lc);
getch();
return 0;
}


CodePudding user response:

This is two sorted list combined into a sorted list...
Title mean how in a random sequence list,
Implemented on the basis of the initial list first element, big in front of the initial list first element, small in initial behind the first element on the list
  • Related