Witam mam problem w tym programie (poniżej), napisałem dodawanie, usuwanie, wyświetlanie ktoś mi pomógł ale nie mogę zrobić dodawania elementu na daną pozycje wskazaną przez użytkownika jak to zrobić??
#include <iostream>
using namespace std;
struct Element{
int liczba;
Element* back;
Element* next;
};
void dodaj(int liczba, Element *&glowka);
void wyswietl(Element *&glowka);
void usun(Element *&glowka);
void wstaw (int liczba, Element *&glowka);
int main(){
Element *glowa = new Element;
glowa = NULL;
dodaj(14, glowa);
dodaj(25, glowa);
dodaj(36, glowa);
dodaj(46, glowa);
wyswietl(glowa);
usun(glowa);
wyswietl(glowa);
wstaw(glowa);
return 0;
}
void dodaj(int liczba, Element *&glowka){
Element *tmp = new Element();
tmp->liczba = liczba;
tmp->next = NULL;
tmp->back = NULL;
//jeĹĽeli lista jest pusta..
if(glowka == NULL) glowka = tmp;
else{
Element *t = glowka;
while(t->next) t = t->next;
t->next = tmp;
tmp->back = t;
}
}
void wyswietl(Element *&glowka){
unsigned int i=1;
Element *tmp = glowka;
Element *tmp2 = glowka;
while(glowka != NULL){
cout<<"Element "<<i<<":"<<endl;
i+=1;
cout<<"Liczba: "<<glowka->liczba<<endl;
cout<<"Addr: "<<glowka<<endl;
cout<<"Next: "<<glowka->next<<endl;
cout<<"Back: "<<glowka->back<<endl<<endl<<endl;
glowka = glowka->next;
}
cout<<endl<<endl<<endl;
glowka = tmp;
while(glowka != NULL){
cout<<glowka->liczba<<"->";
glowka = glowka->next;
}
cout<<"NULL"<<endl<<endl;
glowka = tmp2;
}
void usun(Element *&glowka){
if (glowka != NULL){
Element *t = glowka;
while(t->next) t = t->next;
//cout<<t->liczba<<endl;
t->back->next = NULL;
delete t;
}
void wstaw(int liczba, Element *&glowka){
Element *tmp = new Element();
}
}