Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node {
- int data;
- node* nxt;
- };
- void customSort(node** head, int p) {
- node* cur=*head;
- while(cur->nxt) {
- if(cur->nxt->data % p == 0) {
- node* tmp = cur->nxt;
- cur->nxt=cur->nxt->nxt;
- tmp->nxt=*head;
- *head = tmp;
- } else
- cur = cur->nxt;
- }
- }
- void customSort(node** head) {
- customSort(head, 3);
- node* cur=*head;
- while(cur->nxt && cur->nxt->data % 3 == 0) {
- cur = cur->nxt;
- }
- if(! cur->nxt) return;
- node** t=&(cur->nxt);
- customSort(t, 2);
- cur->nxt = *t;
- }
- void push(node* head, int x) {
- node* cur=head;
- while(cur->nxt)
- cur=cur->nxt;
- cur->nxt = new node;
- cur->nxt->data=x;
- cur->nxt->nxt = NULL;
- }
- void print(node* head) {
- node* cur=head;
- while(cur) {
- cout<<cur->data<<" ";
- cur=cur->nxt;
- }
- cout<<endl;
- }
- int main() {
- node* head = new node;
- head->data = 5;
- head->nxt=NULL;
- push(head, 3);
- push(head, 4);
- push(head, 6);
- push(head, 7);
- push(head, 1);
- push(head, 8);
- node** x=&head;
- customSort(x);
- print(*x);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement