#include <cstdlib>
#include <iostream>
using namespace std;
struct Lista{
int dana; //dana w liscie - w tej bedzie to int
Lista *next;//wsk to next element
};
int D(Lista*& head)
{
Lista* first = new Lista; //zmienna pomocnicza przechowujaca wartosc elementu pierwszego
first = head;
int x = 0;
while (head != 0)
{
head = head -> next;
x++;
}
cout<<"lista ma "<<x<<" elementow"<<endl;
head = first;
return x;
}
void Sort(Lista*& head)
{
Lista* pom = new Lista;
Lista* pom1 = new Lista;
Lista* first = new Lista;
Lista* TMP = new Lista;
first = head;
int n = D(head),x =0;
if (n>1){
for (int i = 0 ;i < n-1 ;i++)
{
TMP = head;
head = head -> next;
if (TMP->dana > head->dana)
{
x++;
TMP->next = head -> next;
head->next = TMP;
if (i==0) first = head ;
if (x>1)
{
pom1=head;
head = pom;
head->next = pom1;
head = pom1;
}
pom =head;
head = head->next;
}
}
}
head = first;
}
void AddToList1(Lista*& head, int Liczba1) //dodawanie elementu na poczatek listy
{
Lista* TMP = new Lista;
TMP->dana = Liczba1;//przesun stary pierwszy element
TMP->next = head;
head = TMP; //nowy 1-wszy element
Sort(head);
}
void AddToList(Lista*& head, int Liczba1) //dodawanie elementu na poczatek listy
{
Lista* first = new Lista; //zmienna pomocnicza przechowujaca wartosc elementu pierwszego
Lista* pom = new Lista;
first = head; //first = element pierwszy (head - czolowy)
while (head != NULL)
{
pom = head;
head = head -> next;
}
head = pom;
Lista* TMP = new Lista;
TMP->dana = Liczba1;
TMP->next = head->next;
head->next = TMP;
head = first;
Sort(head);
}
void C(Lista*& head) //usun pierwszy el
{
int a = 0;
Lista* TMP = new Lista; //zmienna pomocnicza
TMP = head -> next;
free(head);
head = TMP;
}
void E(const Lista* head)
{
while (head !=NULL)
{
cout << head->dana << " ";
head = head->next;
}
}
void Pisz()
{
cout<<endl;
cout<<"wprowadz znak:"<<endl;
cout<<"a - aby dodac element do kolejki"<<endl;
cout<<"c - aby wykonac podpunkt c"<<endl;
cout<<"d - aby wykonac podpunkt d"<<endl;
cout<<"e - aby wykonac podpunkt e"<<endl;
cout<<"q - aby zakonczyc"<<endl;
cout<<endl;
}
int main(int argc, char *argv[])
{
Lista* head = NULL;
char a = ' ';
int b,c;
int empty = 0;
while(a!='q')
{
Pisz();
cin>>a;
cout<<endl;
if (a=='a'){cout<<"wprowadz liczbe: ";cin>>b;
if(empty == 0){empty++; AddToList1(head, b);}else {empty++;AddToList1(head, b);}}
else if (a=='c'){empty--; C(head);}
else if (a=='d')D(head);
else if (a=='e')E(head);
}
system("PAUSE");
return EXIT_SUCCESS;
}