Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6.     int data;
  7.     node* nxt;
  8. };
  9.  
  10. void customSort(node** head, int p) {
  11.     node* cur=*head;
  12.     while(cur->nxt) {
  13.         if(cur->nxt->data % p == 0) {
  14.             node* tmp = cur->nxt;
  15.             cur->nxt=cur->nxt->nxt;
  16.             tmp->nxt=*head;
  17.             *head = tmp;
  18.         } else
  19.             cur = cur->nxt;
  20.     }
  21. }
  22.  
  23. void customSort(node** head) {
  24.     customSort(head, 3);
  25.     node* cur=*head;
  26.     while(cur->nxt && cur->nxt->data % 3 == 0) {
  27.         cur = cur->nxt;
  28.     }
  29.     if(! cur->nxt) return;
  30.     node** t=&(cur->nxt);
  31.     customSort(t, 2);
  32.     cur->nxt = *t;
  33. }
  34.  
  35. void push(node* head, int x) {
  36.     node* cur=head;
  37.     while(cur->nxt)
  38.         cur=cur->nxt;
  39.     cur->nxt = new node;
  40.     cur->nxt->data=x;
  41.     cur->nxt->nxt = NULL;
  42. }
  43. void print(node* head) {
  44.     node* cur=head;
  45.     while(cur) {
  46.         cout<<cur->data<<" ";
  47.         cur=cur->nxt;
  48.     }
  49.     cout<<endl;
  50. }
  51.  
  52. int main() {
  53.     node* head = new node;
  54.     head->data = 5;
  55.     head->nxt=NULL;
  56.     push(head, 3);
  57.     push(head, 4);
  58.     push(head, 6);
  59.     push(head, 7);
  60.     push(head, 1);
  61.     push(head, 8);
  62.     node** x=&head;
  63.    
  64.     customSort(x);
  65.     print(*x);
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement