Guest User

Untitled

a guest
Aug 9th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  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)
  30. struct Node* function(struct Node *a,struct Node *b)
  31. {
  32.     Stack<struct Node *> stack;
  33.     struct Node *result;
  34.     struct Node *oldResult;
  35.     while(a && b)
  36.     {
  37.         if(a->data <= b->data)
  38.         {
  39.             stack.push(a);
  40.             a=a->next;
  41.         }
  42.         else
  43.         {
  44.             stack.push(b);
  45.             b=b->next;
  46.         }
  47.     }
  48.     result= a ? a : b;
  49.     while(!stack.empty())
  50.     {
  51.         oldResult=result;
  52.         result=stack.pop();
  53.         result->next=oldResult;
  54.     }
  55.     return result;
  56. }
Add Comment
Please, Sign In to add comment