Witam napisalem cos takiego do usuwania liczby z listy
void usdow(lista*&head,int dana)
{
if (head == NULL)
{
cout << "lista jest pusta";
}
if ((head->next == NULL)&&(head->nazwa==dana))
{
lista*temp;
temp = head->next;
head = temp;
}
lista*prev = NULL;
lista*temp = head;
while ((temp->next != NULL) && (temp->nazwa != dana))
{
prev = temp;
temp = temp->next;
}
if ((temp->next == NULL) && (temp->nazwa == dana))
{
prev->next = NULL;
delete temp;
}
else if ((temp->next != NULL) && (temp->nazwa == dana))
{
prev->next == temp->next;
delete temp;
}
else return;
}
Funkcja dziala tak,ze najpierw sprawdzam czy lista nie jest pusta,potem czy usuwam pierwszy element,jak nie to sprawdzam listę i są 3 możliwości dotarłem na koniec i ostatnia jest do skasowania,albo gdzieś w srodku,albo nie ma tej liczby co chce skasować,ale nie wiem czemu nie działa.
Tutaj cała lista:
#include <iostream>
#include <conio.h>
using namespace std;
struct lista {
int nazwa;
lista*next;
};
void wypis(lista*head);
void dodpocz(lista*&head, char dana);
void dodajend(lista*&head, int dana);
bool znajdz(lista*head, int liczba);
void dodsort(lista*&head, int liczba);
void usun(lista*&head, int liczba);
void usfirst(lista*&head);
void usost(lista*&head);
void usdow(lista*&head, int dana);
int main()
{
bool koniec = false;
char wybor;
int wartosc;
lista*head = NULL;
while (!koniec)
{
cout << "Lista: " << endl;
wypis(head);
cout << endl;
cout << "Program lista jednokierunkowa oto menu:" << endl;
cout << "Wcisnij 1 zeby dodac na poczatek" << endl;
cout << "Wcisnij 2 zeby dodac na koniec" << endl;
cout << "Wcisnij 3 zeby dodac posortowanie" << endl;
cout << "Wcisnij 4 zeby usunac pierwszy element" << endl;
cout << "Wcisnij 5 zeby usunac ostatni element" << endl;
cout << "wcisnij 6 zeby usunac dowolyn element" << endl;
wybor = _getch();
switch (wybor)
{
case '1':
cout << "Jaka liczbe chcesz wpisac na poczatek?" << endl;
cin>> wartosc;
dodpocz(head, wartosc);
break;
case '2':
cout << "Jaka liczbe chcesz wpisac na koniec?" << endl;
cin >> wartosc;
dodajend(head, wartosc);
break;
case '3':
cout << "Jaka wartosc chcesz wpisac posortowanie" << endl;
cin >> wartosc;
dodsort(head, wartosc);
break;
case '5':
cout << "usuwanie ostatniego elementu " << endl;
usost(head);
break;
case '4':
cout << "usuwanie pierwszego elementu " << endl;
usfirst(head);
break;
case '6':
cout << "usuwanie dowolnego elementu podaj jaki" << endl;
cin >> wartosc;
usdow(head, wartosc);
break;
case '9':
koniec = true;
break;
default:
cout << "Zly klawisz" << endl;
break;
}
system("cls");
}
}
void wypis(lista*head)
{
if (head == NULL)
{
cout << "Pusta lista";
}
else
{
while (head)
{
cout << head->nazwa << "|";
head = head->next;
}
}
cout << endl;
}
void dodpocz(lista*&head,char dana)
{
lista*temp;
temp = new lista;
temp->nazwa = dana;
temp->next = head;
head = temp;
}
void dodajend(lista*&head, int dana)
{
lista*tmp = new lista;
tmp->nazwa = dana;
tmp->next = NULL;
if (head == NULL)
head = tmp;
else
{
lista*iterator = head;
while (iterator->next != NULL)iterator = iterator->next;
iterator->next = tmp;
}
}
bool znajdz(lista*head, int liczba)
{
if (head == NULL)return false;
if (head->nazwa == liczba)return true;
else
{
while ((head->next != NULL) && (head->next->nazwa != liczba))head = head->next;
if (head->next == NULL)
{
return false;
}
else return true;
}
}
void dodsort(lista*&head, int liczba)
{
lista*temp = new lista;
temp->nazwa = liczba;
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else if (temp->nazwa < head->nazwa)
{
temp->next = head;
head = temp;
}
else
{
lista*iterator = head;
while ((iterator->next != NULL) && (iterator->next->nazwa < liczba))iterator = iterator->next;
if (iterator->next == NULL)
{
iterator->next = temp;
}
else
{
temp->next = iterator->next;
iterator->next = temp;
}
}
}
void usun(lista*&head, int liczba)
{
if (head == NULL)return;
else
{
lista*prev = NULL;
lista*iterator = head;
while ((iterator->next != NULL) && (iterator->next->nazwa != liczba))
{
prev = iterator;
iterator = iterator->next;
}
if ((iterator->next == NULL) && (iterator->nazwa != liczba))return;
if ((iterator->next == NULL) && (iterator->nazwa == liczba))
{
delete iterator;
prev->next = NULL;
}
}
}
void usfirst(lista*&head)
{
if (head == NULL)
{
cout << "LIsta pusta";
}
if (head)
{
lista*temp;
temp = head->next;
head = temp;
}
}
void usost(lista*&head)
{
if (head == NULL)
{
cout << "lista pusta";
}
if (head)
{
lista*iterator;
lista*prev = NULL;
iterator = head;
while (iterator->next != NULL)
{
prev = iterator;
iterator = iterator->next;
}
delete iterator;
prev->next = NULL;
}
}
void usdow(lista*&head,int dana)
{
if (head == NULL)
{
cout << "lista jest pusta";
}
if ((head->next == NULL)&&(head->nazwa==dana))
{
lista*temp;
temp = head->next;
head = temp;
}
lista*prev = NULL;
lista*temp = head;
while ((temp->next != NULL) && (temp->nazwa != dana))
{
prev = temp;
temp = temp->next;
}
if ((temp->next == NULL) && (temp->nazwa == dana))
{
prev->next = NULL;
delete temp;
}
else if ((temp->next != NULL) && (temp->nazwa == dana))
{
prev->next == temp->next;
delete temp;
}
else return;
}