Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C&XX2B中插入数组中间的最后一个元素;x2B;?_C++ - Fatal编程技术网

C++ 在C&XX2B中插入数组中间的最后一个元素;x2B;?

C++ 在C&XX2B中插入数组中间的最后一个元素;x2B;?,c++,C++,代码更改了其他元素的位置,我希望输出结果没有更改 如果大小=6个元素1 2 3 4 5 0,则输出应为1 2 3 0 5 6,但代码输出1 3 5 0 2 4 我在C++中实现编码,我想知道我到底在哪里犯错误< /P> #include <iostream> #include<stdio.h> using namespace std; struct Node { int value; struct Node* next; }; class Li

代码更改了其他元素的位置,我希望输出结果没有更改 如果大小=6个元素1 2 3 4 5 0,则输出应为1 2 3 0 5 6,但代码输出1 3 5 0 2 4

<>我在C++中实现编码,我想知道我到底在哪里犯错误< /P>
  #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;
}
代码更改了其他元素的位置,我希望输出结果没有更改 如果大小=6个元素1 2 3 4 5 0,则输出应为1 2 3 0 5 6,但代码输出1 3 5 0 2 4

<>我在C++中实现编码,我想知道我到底在哪里犯错误< /P>
  #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;
}
#包括
#包括
使用名称空间std;
结构节点
{
int值;
结构节点*下一步;
};
类链接列表
{
私人:
结构节点*head,*mid;
整数计数;
公众:
LinkedList();
void insertamidle(int);
void show();
};
LinkedList::LinkedList()
{
head=NULL;
mid=NULL;
计数=0;
}
//Фаааааааааа节点
// середина связанного списка
void LinkedList::insertAtMiddle(int n)
{
结构节点*temp=新结构节点();
结构节点*temp1;
temp->next=NULL;
温度->值=n;
// Если количество элементов меньше 2
如果(计数<1)
{
if(head==NULL)
{
压头=温度;
}
其他的
{
temp1=头部;
temp1->next=temp;
}
计数++;
//第一个元素的中点
中间=头部;
}
//如果元素的数量已经存在
//大于2
其他的
{
临时->下一步=中期->下一步;
中间->下一步=温度;
计数++;
//插入后元素的If数
//这些都很奇怪
如果(计数%2!=0)
{
//最新数据的中点
//插入节点
mid=mid->next;
}
}
}
//函数打印节点
//链接列表的名称
void LinkedList::show()
{
结构节点*temp;
温度=水头;
//初始化温度到磁头
//迭代和打印直到
//链表的结尾
//也就是说,直到temp为null
while(temp!=NULL)
{
其次是价值观;
}
无法到达[i];
}

cout您可以使用
std::rotate
将结束元素移动到中间

对于您提出的问题:

#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";
}
#包括
#包括
#包括
#包括
使用std::advance;
使用std::cout;
使用std::ostream;
使用std::旋转;
使用std::vector;

静态ostream&Operator是否尝试过调试?是,但无法在函数更改位置的位置使用笔和纸在列表上绘制您执行的操作。为节点绘制框,为链接绘制箭头。当您在调试器中单步执行代码时,请将它们绘制在纸上。修改指针时擦除并重新绘制箭头。你尝试过使用吗?@Eljay否,你能添加它并发送给我吗?非常感谢,兄弟,你能为N个数字编写代码吗?代码将处理将最后一个元素移动到大小为3或更大的向量中间的问题。非常感谢,好吗