Monday, September 25, 2006
Tot la liste raman
#include
struct nod
{
char nume[50];
nod *next;
};
nod *start = NULL;
void baga()
{
nod *temp, *temp2;
temp = new nod;
cout << "Introduce nume: ";
cin >> temp -> nume;
temp -> next = NULL;
if (start == NULL)
start = temp;
else
{ temp2 = start;
while (temp2 -> next != NULL)
{
temp2 = temp2 -> next;
}
temp2->next = temp;
}
}
void sterge_nod(char nume[50])
{
nod *temp4, *temp5;
temp4 = start;
while(temp4 -> nume != nume)
{
temp5 = temp4;
temp4 = temp4 -> next;
}
temp5 -> next = temp4 -> next;
delete temp4;
}
void sterge_primul_nod()
{
nod *temp6;
temp6 = start;
start = start -> next;
delete temp6;
}
void sterge_ultimul_nod()
{
nod *temp7, *temp8;
temp7 = start;
while(temp7 -> next != NULL)
{
temp8 = temp7;
temp7 = temp7 -> next;
}
temp8 -> next = 0;
}
void listeaza()
{
nod* temp3;
temp3 = start;
do
{
cout << endl << "Numele: " << temp3 -> nume << endl;
temp3 = temp3 -> next;
}
while(temp3 != NULL);
}
int main()
{
int i, n, optiune;
char nume[50];
do
{
cout << endl;
cout << "Alegeti o optiune de mai jos." << endl;
cout << "0. Iesi din program." << endl;
cout << "1. Adauga unul sau mai multe nume." << endl;
cout << "2. Listeza toate numele." << endl;
cout << "3. Sterge un nume din lista." << endl;
cout << "4. Sterge primul nume din lista." << endl;
cout << "5. Sterge ultimul nume din lista." << endl;
cout << endl;
cin >> optiune;
switch(optiune)
{
case 1:
cout << "Cate nume trebuiesc introduse? ";
cin >> n;
for(i = 0; i < n; i++)
{
baga();
}; break;
case 2: listeaza(); break;
case 3: cout << "Ce nume trebuie sters? (ATENTIE! Numele trebuie scris exact cum a fost introdus!)";
cin >> nume;
sterge_nod(nume); break;
case 4: sterge_primul_nod(); break;
case 5: sterge_ultimul_nod(); break;
}
}
while(optiune != 0);
return 0;
}
Cu ajutorul lui fratimiu am reusit sa descifrez misterele unui manual care cica sunt despre intelegerea listelor. Din pacate am ramas la problema stergerii unui nod din mijlocul listei. Nu primesc o eroare ci crapa brusc programul. Incerc sa pricep de ce.
struct nod
{
char nume[50];
nod *next;
};
nod *start = NULL;
void baga()
{
nod *temp, *temp2;
temp = new nod;
cout << "Introduce nume: ";
cin >> temp -> nume;
temp -> next = NULL;
if (start == NULL)
start = temp;
else
{ temp2 = start;
while (temp2 -> next != NULL)
{
temp2 = temp2 -> next;
}
temp2->next = temp;
}
}
void sterge_nod(char nume[50])
{
nod *temp4, *temp5;
temp4 = start;
while(temp4 -> nume != nume)
{
temp5 = temp4;
temp4 = temp4 -> next;
}
temp5 -> next = temp4 -> next;
delete temp4;
}
void sterge_primul_nod()
{
nod *temp6;
temp6 = start;
start = start -> next;
delete temp6;
}
void sterge_ultimul_nod()
{
nod *temp7, *temp8;
temp7 = start;
while(temp7 -> next != NULL)
{
temp8 = temp7;
temp7 = temp7 -> next;
}
temp8 -> next = 0;
}
void listeaza()
{
nod* temp3;
temp3 = start;
do
{
cout << endl << "Numele: " << temp3 -> nume << endl;
temp3 = temp3 -> next;
}
while(temp3 != NULL);
}
int main()
{
int i, n, optiune;
char nume[50];
do
{
cout << endl;
cout << "Alegeti o optiune de mai jos." << endl;
cout << "0. Iesi din program." << endl;
cout << "1. Adauga unul sau mai multe nume." << endl;
cout << "2. Listeza toate numele." << endl;
cout << "3. Sterge un nume din lista." << endl;
cout << "4. Sterge primul nume din lista." << endl;
cout << "5. Sterge ultimul nume din lista." << endl;
cout << endl;
cin >> optiune;
switch(optiune)
{
case 1:
cout << "Cate nume trebuiesc introduse? ";
cin >> n;
for(i = 0; i < n; i++)
{
baga();
}; break;
case 2: listeaza(); break;
case 3: cout << "Ce nume trebuie sters? (ATENTIE! Numele trebuie scris exact cum a fost introdus!)";
cin >> nume;
sterge_nod(nume); break;
case 4: sterge_primul_nod(); break;
case 5: sterge_ultimul_nod(); break;
}
}
while(optiune != 0);
return 0;
}
Cu ajutorul lui fratimiu am reusit sa descifrez misterele unui manual care cica sunt despre intelegerea listelor. Din pacate am ramas la problema stergerii unui nod din mijlocul listei. Nu primesc o eroare ci crapa brusc programul. Incerc sa pricep de ce.

