Mam oto taki kod:
#include<iostream>
using namespace std;
struct dane
{
int a;
dane *next;
};
dane *pointer;
dane *first;
dane *nowy;
void dodaj(int liczba)
{
nowy=new dane;
nowy->a=liczba;
nowy->next=NULL;
if(pointer==0)
first=nowy;
else
pointer->next=nowy;
pointer=nowy;
}
void pokaz()
{
nowy=first;
while(nowy!=NULL)
{
cout << nowy->a << endl;
nowy=nowy->next;
}
}
void dodaj_element_po(int e)
{
nowy=first;
dane *y;
int q;
cout << "Podaj po jakim elemencie: ";
cin >> q;
for(int i=0;i<q;i++)
{
cout << nowy->a << " ";
nowy=nowy->next;
}
nowy=new dane;
nowy->a=e;
}
void usun()
{
dane *p;
while(first)
{
p=first;
first=first->next;
delete p;
}
}
int main(){
pointer=0;
int t,dana;
cout << "Podaj ilosc elementow: ";
cin >> t;
cout << "-------" << endl;
while(t--)
{
cin >> dana;
dodaj(dana);
}
cout << "--------" << endl;
pokaz();
usun();
cout << "-------" << endl;
pokaz();
cout << "Podaj liczbe: ";
int z;
cin >> z;
dodaj_element_po(z);
pokaz();
system("pause");
return 0;
}
Wszystko chodzi poprawnie tylko nie działa funkcja ktora dodaje element po n-elemencie, to ten kod:
void dodaj_element_po(int e)
{
nowy=first;
dane *y;
int q;
cout << "Podaj po jakim elemencie: ";
cin >> q;
for(int i=0;i<q;i++)
{
cout << nowy->a << " ";
nowy=nowy->next;
}
nowy=new dane;
nowy->a=e;
}
Mógłby mi ktoś powiedzieć co jest źle?
EDIT
Skapłem się że przed dodaniemnowego elementu usuwam wszystkie;p ale i tak nie dziala