Le code change la place des autres éléments, je veux que la sortie soit sans changer si taille = 6 éléments 1 2 3 4 5 0 la sortie devrait être 1 2 3 0 5 6 mais le code sort 1 3 5 0 2 4

Ci-dessous mon implémentation de codage en c++ je veux où exactement je me trompe

Le code change la place des autres éléments, je veux que la sortie soit sans changer si taille = 6 éléments 1 2 3 4 5 0 la sortie devrait être 1 2 3 0 5 6 mais le code sort 1 3 5 0 2 4

Ci-dessous mon implémentation de codage en c++ je veux où exactement je me trompe

  #include <iostream> 
#include<stdio.h>
using namespace std;

struct Node 
{
    int value;
    struct Node* next;
};

class LinkedList 
{
private:
    struct Node* head, * mid;
    int count;

public:
    LinkedList();
    void insertAtMiddle(int);
    void show();
};

LinkedList::LinkedList()
{
    head = NULL;
    mid = NULL;
    count = 0;
}

// Функция для вставки node в
// середина связанного списка
void LinkedList::insertAtMiddle(int n)
{
    struct Node* temp = new struct Node();
    struct Node* temp1;

    temp->next = NULL;
    temp->value = n;

    // Если количество элементов меньше 2
    if (count < 1) 
    {
        if (head == NULL)
        {
            head = temp;
        }
        else 
        {
            temp1 = head;
            temp1->next = temp;
        }
        count++;
        // mid points to first element 
        mid = head;
    }

    // If the number of elements already present 
    // are greater than 2 
    else
    {

        temp->next = mid->next;
        mid->next = temp;
        count++;

        // If number of e lements after insertion 
        // are odd 
        if (count %  2!= 0) 
        {

            // mid points to the newly 
            // inserted node

            mid = mid->next;
        }
    }
}


// Function to print the nodes 
// of the linked list 
void LinkedList::show()
{
    struct Node* temp;
    temp = head;
    // Initializing temp to head 
    // Iterating and printing till 
    // The end of linked list 
    // That is, till temp is null 
    while (temp != NULL) 
    {
        cout << temp->value << " -> ";
        temp = temp->next;

    }
    cout << "NULL";
    cout << endl;
}

// Driver code 
int main()
{
    int arr[100];
    int a;
    a= sizeof(arr) / sizeof(arr[0]);
    LinkedList L1;
    cout << "Enter the size of the array\n";

    cin >> a;

    cout << "Enter the values\n";
    
    for (int i = 0; i < a; i++)
    {
        cin >> arr[i];
    }

    cout << "The values entered are\n";
   
    for (int i = 0; i < a; i++)
    L1.insertAtMiddle(arr[i]);
    L1.show();
    return 0;
}
c++
0
Izzatbek Kamolov 1 nov. 2020 à 16:48

1 réponse

Meilleure réponse

Vous pouvez utiliser std::rotate pour déplacer l'élément de fin au milieu.

Pour le problème que tu poses :

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

using std::advance;
using std::cout;
using std::ostream;
using std::rotate;
using std::vector;

static ostream& operator<<(ostream& out, vector<int> const& v) {
    char const* sep = "";
    for (auto i : v) {
        out << sep << i;
        sep = " ";
    }
    return out;
}

int main() {
    using diff_t = vector<int>::iterator::difference_type;
    auto v = vector<int>{1, 2, 3, 4, 5, 0};
    auto sz = static_cast<diff_t>(v.size());
    auto pos = static_cast<diff_t>(v.size() / 2);
    auto f = v.begin();
    advance(f, pos);
    auto fn = v.begin();
    advance(fn, sz-1);
    auto e = v.end();
    rotate(f, fn, e);
    cout << v << "\n";
}
0
Eljay 1 nov. 2020 à 16:34