Czesc mam problem taki, ze program sie wysypuje po usunieciu ostatniego elementu z listy. Np. dodam jakies 4 liczby i gdy usune wszystkie 4 petla sie wysypuje. Prosze o pomoc
Kod programu:
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*start;
void stworzListe(int wartosc)
{
struct node *s, *temp;
temp=new(struct node);
temp->info=wartosc;
temp->next=NULL;
if(start==NULL)
{
temp->prev=NULL;
start=temp;
}
else
{
s=start;
while(s->next!=NULL)
s=s->next;
s->next=temp;
temp->prev=s;
}
}
void dodajLiczbe(int wartosc)
{
if (start==NULL)
{
cout<<"Wpierw stworz liste!"<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->prev=NULL;
temp->info=wartosc;
temp->next=start;
start->prev=temp;
start=temp;
cout<<"Liczbe wstawiono"<<endl;
}
void dodajPo(int wartosc, int pos)
{
if (start==NULL)
{
cout<<"Wpierw stworz liste!"<<endl;
return;
}
struct node *tmp, *q;
int i;
q=start;
for (i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
cout<<"Jest mnie niz ";
cout<<pos<<" liczb elementow"<<endl;
return;
}
}
tmp=new(struct node);
tmp->info=wartosc;
if (q->next==NULL)
{
q->next=tmp;
tmp->next=NULL;
tmp->prev=q;
}
else
{
tmp->next=q->next;
tmp->next->prev=tmp;
q->next=tmp;
tmp->prev=q;
}
cout<<"Liczbe wstawiono"<<endl;
}
void usun(int wartosc)
{
struct node *tmp, *q;
if (start->info==wartosc)
{
tmp=start;
start=start->next;
start->prev=NULL;
cout<<"Liczbe usunieto"<<endl;
free(tmp);
return;
}
q=start;
while (q->next->next!=NULL)
{
if (q->next->info==wartosc)
{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
cout<<"Liczbe usunieto"<<endl;
free(tmp);
return;
}
q=q->next;
}
if (q->next->info==wartosc)
{
tmp=q->next;
free(tmp);
q->next=NULL;
cout<<"Liczbe usunieto"<<endl;
return;
}
cout<<"Liczby "<<wartosc<<" nie znaleziono"<<endl;
}
void wyswietl()
{
struct node *q;
if (start==NULL)
{
cout<<"Lista jest pusta"<<endl;
return;
}
q=start;
cout<<"Elementy listy: "<<endl;
while (q!=NULL)
{
cout<<q->info<<" ";
q=q->next;
}
cout<<"NULL"<<endl;
}
int main()
{
int wybor, element, pozycja;
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"MENU"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1.Wstaw do wierzcholka listy"<<endl;
cout<<"2.Wstaw na poczatek"<<endl;
cout<<"3.Wstaw po liczbie elementow"<<endl;
cout<<"4.Skasuj"<<endl;
cout<<"5.Wyswietl"<<endl;
cout<<"6.Koniec"<<endl;
cout<<"Wpisz swoj wybor z MENU: ";
cin>>wybor;
switch (wybor)
{
case 1:
cout<<"Wpisz liczbe: ";
cin>>element;
stworzListe(element);
cout<<endl;
break;
case 2:
cout<<"Wpisz liczbe: ";
cin>>element;
dodajLiczbe(element);
cout<<endl;
break;
case 3:
cout<<"Wpisz liczbe: ";
cin>>element;
cout<<"Za ktora liczba elementow wstawic?: ";
cin>>pozycja;
dodajPo(element, pozycja);
cout<<endl;
break;
case 4:
if (start == NULL)
{
cout<<"Lista jest pusta"<<endl;
break;
}
cout<<"Wybierz element do skasowania: ";
cin>>element;
usun(element);
cout<<endl;
break;
case 5:
wyswietl();
cout<<endl;
break;
case 6:
exit(1);
default:
cout<<"Zly wybor"<<endl;
}
}
return 0;
}