Using namespace std


Download 142.05 Kb.
Pdf ko'rish
Sana27.11.2020
Hajmi142.05 Kb.
#153861
Bog'liq
2-lab


13. Transpozitsiya usulidan foydalanib ro‘yhatda qo‘shni elementlari bo‘linmasi juft son 

bo‘lgan elementni toping. Agar bunday elementlar ko‘p bo‘lsa, u holda ularning eng kattasi 

yoki eng kichigini toping; agar bunday element mavjud bo‘lmasa – shunga mos ma’lumot 

chiqaring. 

 

#include   



#include  

using namespace std;  

   

// structure for self organizing list  



struct node {  

    int value;  

    struct node* next;  

};  


   

// head and rear pointing to start and end of list resp.  

node *head = NULL, *rear = NULL;  

   


// function to insert an element  

void insert_node(int number)  

{  

    // creating a node  



    node* temp = (node*) malloc (sizeof(node));  

   


    // assigning value to the created node;  

    temp->value = number;  

    temp->next = NULL;  

   


    // first element of list  

    if (head == NULL)  

        head = rear = temp;  

   


    // rest elements of list  

    else {  



        rear->next = temp;  

        rear = temp;  

    }  

}  


   

  

  



int* search_even_div()  

{  


    // pointer to current node  

    node* current = head;  

   

    // pointer to previous node  



    node* prev = NULL;  

     


   

    // pointer to previous of previous  

    node* prev_prev = NULL;  

   


   

node* prev_prev_prev = NULL; 

   

 

   



int lastValue = -1; 

   


int maxValue = -1; 

   


bool maxFlag = false; 

   


int arr[2]; 

    // searching for the key  

    while (current != NULL) {  

   


        // if key found  

        if (prev_prev != NULL && prev != NULL && prev_prev->value % current->value == 0 && (prev_prev-

>value / current->value) % 2 == 0) {  

   


 

 

lastValue = prev->value; 



 

 

 



if (maxFlag){ 

   

 

 



 

if (lastValue>maxValue){ 

   

 

 



 

 

maxValue = lastValue; 



 

 

 



 

  } 


 

 

 



} else { 

 

 



 

   


maxValue = lastValue; 

 

 



 

   


maxFlag = true; 

 

 



 

            // if key is neither the first element  



            // and nor the second element  

            if (prev_prev_prev != NULL&& true) {  

 

 

 



 

 

 



                /* re-arranging the elements */ 

                prev_prev_prev->next = prev;  

                prev_prev->next = prev->next;  

                prev->next = prev_prev;  

            }  

        }  

        prev_prev_prev=prev_prev; 

        prev_prev = prev;  

        prev = current;  

        current = current->next;  

    }  

 

 



 

   


    arr[0]=lastValue; 

    arr[1]=maxValue; 

    return arr;  

}  


   

// function to display the list  

void display()  

{  


    if (head == NULL) {  

        cout << "List is empty" << endl;  

        return;  

    }  


   

    // temporary pointer pointing to head  

    node* temp = head;  

    cout << "List: ";  

   

    // sequentially displaying nodes  



    while (temp != NULL) {  

        cout << temp->value;  

        if (temp->next != NULL)  

            cout << " --> ";  

   

        // incrementing node pointer.  



        temp = temp->next;  

    }  


    cout << endl  

         << endl;  

}  

 

void checkResult(){ 



 

int *arr; 

 

arr = search_even_div(); 



 

if (arr[1]!=-1){ 

 

 

cout<<"Bunday elementlarning eng kattasi "<

 

 



else if (arr[0]!=-1) { 

 

 



cout<<"Bunday element navjud "<

 



 

else { 


 

 

cout<<"Bunday element mavjud emas"; 



 



   

 

int main()  



{  

    /* inserting five values */ 

    insert_node(4); 

 

insert_node(6);  



    insert_node(2);  

    insert_node(1); 

 

insert_node(3); 



 

insert_node(8);  

    insert_node(2);  

    insert_node(1);  

    insert_node(4);  

    insert_node(5);  

    insert_node(2);  

    // Display the list  

    display();  

    // solution 

   

checkResult(); 



   

// Display the list  

   

display(); 



    return 0;  



 



 

Download 142.05 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling