Lista jednokierunkowa uporzadkowana - usuwanie dowolnego elementu
| wena | 07.06.2010 14:44:56 | |
![]() | Witam Staram się napisać listę jednokierunkowa która podczas dokładania elementów je sortuje. Jedyny problem polega na tym, iż lista ta powinna mieć opcje szukania elementu oraz usuwania go (czyli dowolnego). Wcześniej miałem usuwanie elementów od końca listy jednak nie wiem jak usunąć dowolny element. Oto kod mojego programu:
#include <iostream>
#include <stdio.h>
using namespace std;
struct Element
{
int value;
Element *next;
};
struct Info
{
Element *begin;
Element *end;
};
Info inf;
void dodaj(int value2)
{
Element *temp = new Element;
temp->value = value2;
if(inf.begin == NULL)
{
inf.begin = inf.end = temp;
temp->next = NULL;
}
else
{
Element *przed=NULL;
Element *po=inf.begin;
bool end=0;
while ((end==0) && (po!=NULL))
{
if(po->value >= temp->value)
end=1;
else
{
przed = po;
po = po->next;
}
}
if(przed == NULL)
{
inf.begin = temp;
temp->next = po;
}
else if (po == NULL)
{
(inf.end)->next = temp;
temp->next = NULL;
inf.end = temp;
}
else
{
przed->next = temp;
temp->next = po;
}
}
}
void pokaz()
{
Element *temp2 = inf.begin;
while( temp2!=NULL )
{
cout << temp2->value << " ";
temp2 = temp2->next;
}
}
void szukaj(int value3)
{
char a;
bool end2=0;
Element *adres_temp = inf.begin;
while ((adres_temp != NULL) && (end2==0))
{
if(adres_temp->value == value3)
{
cout <<"\nZnaleziono element\n";
cout <<"Chcesz usunac element? (t/n) ";
cin >>a;
if((a='t') || (a='T'))
{
//usuwanie elementu
}
end2=1;
}
else
adres_temp=adres_temp->next;
}
if (end2==0)
cout <<"Nie ma takiego elementu";
}
void delete
int main()
{
int tmp;
int ile;
int x;
cout <<"Ile liczb chcesz dodac: ";
cin >>ile;
for(int i=0;i<ile;++i)
{
cout <<"\nPodaj wartosc nr "<<i+1<<" : ";
cin >> tmp;
dodaj(tmp);
}
cout << "\n\nAktualna zawartosc listy: ";
pokaz();
cout <<"\n\nJakiego elementu szukasz: ";
cin >> x;
szukaj(x);
cout<<"\n\nLista na koniec:\n";
pokaz();
getchar();
}
Nie wiem jak to rozwiązać. | |

