`Przy funkcji zamień mam problem, nie zamienia dobrze wartosci z nastepnym elementem przy wpisaniu indeksu 0
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <iostream>
using namespace std;
typedef struct ListElement {
int data;
struct ListElement * previous;
struct ListElement * next;
} ListElement_type;
void show(ListElement_type *head);
int list_size(ListElement_type *head);
void push_front(ListElement_type **head, int number);
void push_back(ListElement_type **head, int number);
void push_by_index(ListElement_type **head, int number, int position);
void pop_front(ListElement_type **head);
void pop_back(ListElement_type **head);
void zamien(ListElement_type**head, int position);
void pop_by_index(ListElement_type **head, int position);
int main()
{
ListElement_type *head;
head = (ListElement_type *)malloc(sizeof(ListElement_type));
head = NULL;
int option = -1;
int number = -1;
int index = -1;
while (option != 0)
{
system("cls");
printf("\nAktualny stan listy: ");
show(head);
printf("\n\nDrogi uzytkowniku! Co chcesz zrobic?");
printf("\nIndeks liczymy od 0\n\n");
printf("1. Dodac element na poczatek listy.\n");
printf("2. Dodac element na koniec listy.\n");
printf("3. Dodac element zgodnie z wybranym indeksem.\n");
printf("4. Usunac element z poczatku listy.\n");
printf("5. Usunac element z konca listy.\n");
printf("6. Usunac element o wybranym indeksie.\n");
printf("7. Zamien element z nastepnym.\n");
printf("0. Zakonczyc program.\n");
cin>>option;
switch (option)
{
case 0:
return 0;
break;
case 1:
printf("Wpisz liczbe jaka chcesz dodac: ");
cin>>number;
push_front(&head, number);
break;
case 2:
printf("Wpisz liczbe jaka chcesz dodac: ");
cin>>number;
push_back(&head, number);
break;
case 3:
printf("Wpisz liczbe jaka chcesz dodac: ");
cin>>number;
printf("Wpisz indeks: ");
cin>>index;
push_by_index(&head, number, index);
break;
case 4:
pop_front(&head);
break;
case 5:
pop_back(&head);
break;
case 6:
printf("Wpisz indeks elementu, ktorego chcesz sie pozbyc raz na zawsze: ");
cin>>index;
pop_by_index(&head, index);
break;
case 7:
printf("Wpisz indeks: ");
cin >> index;
zamien(&head, index);
break;
default:
printf("Podaj wlasciwa opcje."); Sleep(2000);
break;
}
}
return 0;
}
void push_front(ListElement_type **head, int number)
{
if (*head == NULL) {
*head = (ListElement_type *)malloc(sizeof(ListElement_type));
(*head)->data = number;
(*head)->previous = NULL;
(*head)->next = NULL;
}
else {
ListElement_type *current;
current = (ListElement_type *)malloc(sizeof(ListElement_type));
current->data = number;
current->previous = NULL;
current->next = (*head);
(*head)->previous = current;
*head = current;
}
}
void push_back(ListElement_type **head, int number)
{
if (*head == NULL)
{
*head = (ListElement_type *)malloc(sizeof(ListElement_type));
(*head)->data = number;
(*head)->previous = NULL;
(*head)->next = NULL;
}
else
{
ListElement_type *current = *head;
ListElement_type *new_element;
while (current->next != NULL) {
current = current->next;
}
current->next = (ListElement_type *)malloc(sizeof(ListElement_type));
current->next->data = number;
current->next->previous = current;
current->next->next = NULL;
}
}
void push_by_index(ListElement_type **head, int number, int position)
{
if (position == 0) push_front(head, number);
else
{
if (position == list_size(*head)) push_back(head, number);
else
{
ListElement_type *current = *head;
ListElement_type *tmp;
int i = 0;
while (current->next != NULL && i < position - 1) {
current = current->next;
i++;
}
tmp = current->next;
current->next = (ListElement_type *)malloc(sizeof(ListElement_type));
current->next->data = number;
current->next->previous = current;
tmp->previous = current->next;
current->next->next = tmp;
}
}
}
void pop_front(ListElement_type **head)
{
if (*head != NULL) {
if ((*head)->next == NULL) {
*head = NULL;
}
else {
ListElement_type *tmp;
tmp = (*head)->next;
free(*head);
*head = tmp;
(*head)->previous = NULL;
}
}
}
void pop_back(ListElement_type **head)
{
if ((*head)->next == NULL)
{
*head = NULL;
}
else
{
ListElement_type *current = *head;
while (current->next->next != NULL) {
current = current->next;
}
free(current->next);
current->next = NULL;
}
}
void zamien(ListElement_type ** head, int position)
{
ListElement_type *current = *head;
ListElement_type *tmp;
if (*head == NULL ) // || *head = koniec ( jesli glowa jest koncem listy 1 element )
{
printf("Nie ma co zamieniac");
exit(-1);
}
else // dla indeksu 0 zamienia indeks 1 z nastepnym
{
int i = 0;
while (current->next != NULL && i < position - 1) {
current = current->next;
i++;
}
tmp = (ListElement_type *)malloc(sizeof(ListElement_type));
tmp->data = current->next->data;
current->next->data = current->next->next->data;
current->next->next->data = tmp->data;
}
}
void pop_by_index(ListElement_type **head, int position)
{
if (position == 0) pop_front(head);
else
{
ListElement_type *current = *head;
ListElement_type *tmp;
int i = 0;
while (current->next != NULL && i < position - 1) {
current = current->next;
i++;
}
tmp = current->next;
current->next = tmp->next;
current->next->previous = current;
free(tmp);
//cout << current->next;
//Sleep(30000);
}
}
void show(ListElement_type *head)
{
printf("\n");
if (head == NULL) printf("Lista jest pusta");
else
{
ListElement_type *current = head;
do {
printf("%i", current->data);
printf("\n");
current = current->next;
} while (current != NULL);
}
}
int list_size(ListElement_type *head)
{
int counter = 0;
if (head == NULL) return counter;
else
{
ListElement_type *current = head;
do {
counter++;
current = current->next;
} while (current != NULL);
}
return counter;
}