[ create a new paste ] login | about

Link: http://codepad.org/4T1abSPO    [ raw code | output | fork ]

C++, pasted on Oct 11:
#include <iostream>
using std::cout;


struct tqueue {
   int    val;
   tqueue* next;
};


void push(tqueue** head, tqueue** tail, int val) {
    tqueue* ptr= new tqueue();
    ptr->val   = val;
    ptr->next  = NULL;

    if(*head == NULL)
          *head = *tail = ptr;
    else {
         (*tail)->next = ptr;
         *tail         = ptr;
    }
}


// поместить мaксимальный элемент в начало
void max_first(tqueue** head) {
   tqueue* tmp = *head;
   for(tqueue* iter = *head; iter != NULL; iter = iter->next) {
        if(iter->val > tmp->val)
           tmp = iter;
   }

   int  val = tmp->val;
   tmp->val = (*head)->val;
  (*head)->val = val;
}






int main(void){
   tqueue* head = NULL, *tail = NULL;
   
   //random
   for(int i = 0; i < 100; i++)
       push(&head, &tail, 1 + std::rand()%100);

   max_first(&head);

   tqueue* tmp;
   while(head != NULL){
       cout << head->val << '\n';

       tmp  = head;
       head = head->next;
       delete tmp;
   }
   return 0;
}


Output:
100
87
78
16
94
36
87
93
50
22
63
28
91
60
64
27
41
27
73
37
12
69
68
30
83
31
63
24
68
36
30
3
23
59
70
68
94
57
12
43
30
74
22
20
85
38
99
25
16
71
14
27
92
81
57
74
63
71
97
82
6
26
85
28
37
6
47
30
14
58
25
96
83
46
15
68
35
65
44
51
88
9
77
79
89
85
4
52
55
84
33
61
77
69
40
13
27
87
95
40


Create a new paste based on this one


Comments: