C++ Algorytmy! Zamień element 2 z 3 na liście. Próbowałam coś takiego napisać jednak nie działa ;( void ZamienSec(node*&H){ if(H && H->next){ node*p = H; if(H->next->next){ node*e = p->next; e = e->next; e->next= e->next->next ; e->next->next = p->next; } } }
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *create(int data)
{
struct node *nowy = (struct node *) malloc(sizeof(struct node));
nowy ->data = data;
nowy -> next = NULL;
};
void printNode(struct node *example)
{
while(example != NULL)
{
cout << example ->data <<" ";
example = example -> next;
}
}
struct node *pushFront(struct node *example, int data)
{
struct node *nowy = (struct node *) malloc(sizeof(struct node));
nowy ->data = data;
nowy -> next = example;
return nowy;
}
void swap2and3(struct node *example)
{
struct node *bufor = example ->next ->next;
example ->next ->next = example ->next ->next ->next;
bufor ->next = example ->next;
example ->next = bufor;
}
int main()
{
struct node *example = create(3);
example = pushFront(example, 10);
example = pushFront(example, 2);
example = pushFront(example, 5);
printNode(example);
puts("");
swap2and3(example);
puts("");
printNode(example);
}