SHOW:
|
|
- or go back to the newest paste.
| 1 | struct Node | |
| 2 | {
| |
| 3 | int data; | |
| 4 | struct Node *next; | |
| 5 | } | |
| 6 | ||
| 7 | struct Node* function(struct Node *a,struct Node *b) | |
| 8 | {
| |
| 9 | struct Node *result=NULL; | |
| 10 | if(a==NULL) | |
| 11 | return b; | |
| 12 | else if(b==NULL) | |
| 13 | return a; | |
| 14 | if(a−>data<=b−>data) | |
| 15 | {
| |
| 16 | result=a; | |
| 17 | result−>next=function(a−>next,b); | |
| 18 | } | |
| 19 | else | |
| 20 | {
| |
| 21 | result=b; | |
| 22 | result−>next=function(a,b−>next); | |
| 23 | } | |
| 24 | return result; | |
| 25 | } | |
| 26 | ||
| 27 | ||
| 28 | // Wersja iteracyjna z użyciem stosu: | |
| 29 | - | // (trochę pseudo-kod, bo stosu tu nie implementowałem) |
| 29 | + | // (trochę pseudo-kod, bo stosu tu nie implementowałem/importowałem) |
| 30 | // (czyt. nie testowane) | |
| 31 | struct Node* function(struct Node *a,struct Node *b) | |
| 32 | {
| |
| 33 | Stack<struct Node *> stack; | |
| 34 | struct Node *result; | |
| 35 | struct Node *oldResult; | |
| 36 | while(a && b) | |
| 37 | {
| |
| 38 | if(a->data <= b->data) | |
| 39 | {
| |
| 40 | stack.push(a); | |
| 41 | a=a->next; | |
| 42 | } | |
| 43 | else | |
| 44 | {
| |
| 45 | stack.push(b); | |
| 46 | b=b->next; | |
| 47 | } | |
| 48 | } | |
| 49 | result= a ? a : b; | |
| 50 | while(!stack.empty()) | |
| 51 | {
| |
| 52 | oldResult=result; | |
| 53 | result=stack.pop(); | |
| 54 | result->next=oldResult; | |
| 55 | } | |
| 56 | return result; | |
| 57 | } |