Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<time.h>
- #include<cstdlib>
- #include<windows.h>
- using namespace std;
- struct Element
- {
- int el;
- struct Element *next;
- };
- struct Element *head;
- struct Element *tail;
- void dodaj(int el,int n,int hyh);
- void usun(int n);
- bool czyPusty();
- void wyswietl();
- void srednia(int *pom);
- void usunL();
- int main()
- {
- srand(time(NULL));
- int el, pom;
- pom=1;
- el = rand()%9+1;
- int wybor;
- head = NULL;
- tail = NULL;
- int nr;
- cout<<"Masz do wyboru:\n";
- cout<<"1.Sprawdzenie czy lista jest pusta\n";
- cout<<"2.Dodanie elementu na koniec listy\n";
- cout<<"3.Dodanie elementu na poczatek listy\n";
- cout<<"4.Dodanie elementu na pozycjê przed elementem o podanym numerze\n";
- cout<<"5.Usuniecie elementu z konca listy\n";
- cout<<"6.Usuniecie elementu z poczatku listy\n";
- cout<<"7.Usuniecie elementu znajdujacego siê na okreslonej pozycji\n";
- cout<<"8.Pobranie pierwszego elementu z listy\n";
- cout<<"9.Pobranie ostatniego elementu z listy\n";
- cout<<"10.Policzenie sredniej arytmetycznej elementww w liscie\n";
- cout<<"11.Wyswietlenie calej listy\n";
- cout<<"12.Usuniecie calej listy wraz ze zwolnieniem pamieci\n";
- cout<<"13.EXIT!\n";
- while(wybor!=13)
- {
- cout<<"\nPodaj ktoras opcje:";
- cin>>wybor;
- switch(wybor)
- {
- case 1:
- if(czyPusty()==true)
- {
- cout<<"Lista jest pusta!\n";
- }
- else
- cout<<"Lista nie jest pusta!\n";
- break;
- case 2:
- cout<<"Liczba: ";
- cin>>nr;
- dodaj(nr,pom,pom);
- pom++;
- wyswietl();
- break;
- case 3:
- cout<<"Liczba: ";
- cin>>nr;
- dodaj(nr,1,pom);
- pom++;
- wyswietl();
- break;
- case 4:
- cout<<"Liczba: ";
- cin>>nr;
- int pozycja;
- cout<<"Podaj pozycje: ";
- cin>>pozycja;
- if(pozycja<1 || pozycja>pom)
- if(pom==1)
- cout<<"Lista jest pusta!n";
- else
- cout<<"Mozesz podac tylko pozycje od 1 do "<<pom<<"\n";
- else
- {
- dodaj(nr, pozycja,pom);
- pom++;
- wyswietl();
- }
- break;
- case 5:
- if(czyPusty()==true)
- {
- cout<<"Lista jest pusta, nie ma nic do usuwania!\n";
- }
- else
- {
- pom--;
- usun(pom);
- wyswietl();
- }
- break;
- case 6:
- if(czyPusty()==true)
- cout<<"\nLista jest pusta, nie da sie usunac elementu.\n";
- else1
- {
- cout<<"Przed usunieciem: \n";
- wyswietl();
- usun(1);
- pom--;
- cout<<"\nPo: \n";
- wyswietl();
- }
- break;
- case 7:
- if(czyPusty()==true)
- cout<<"\nLista jest pusta, nie da sie usunac elementu.\n";
- else
- {
- cout<<"Przed usunieciem: \n";
- wyswietl();
- cout<<"\nPodaj pozycje: ";
- cin>>pozycja;
- if(pozycja<1 || pozycja>pom-1)
- {
- cout<<"Mozesz podac tylko pozycje od 1 do "<<pom-1<<endl;
- }
- else
- {
- usun(pozycja);
- pom--;
- cout<<"\nPo: \n";
- wyswietl();
- }
- }
- break;
- case 8:
- if(head!=NULL)
- cout<<"Pierwszy element w liscie: "<<head->el<<endl;
- else
- cout<<"Lista jest pusta\n";
- break;
- case 9:
- if(tail!=NULL)
- cout<<"Ostatni element w liscie: "<<tail->el<<endl;
- else
- cout<<"Lista jest pusta\n";
- break;
- case 10:
- srednia(&pom);
- break;
- case 11:
- wyswietl();
- break;
- case 12:
- usunL();
- pom=1;
- break;
- case 13:
- exit(1);
- break;
- }
- }
- delete head;
- return 0;
- }
- bool czyPusty()
- {
- if(head==NULL)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void dodaj(int el,int n,int hyh)
- {
- Element *he=new Element();
- he->el=el;
- he->next=NULL;
- if(n==1)
- {
- he->next=head;
- head=he;
- if(tail==NULL)
- {
- tail=head;
- }
- return;
- }
- Element *he2 = head;
- for(int i=0; i<n-2; i++)
- {
- he2=he2->next;
- }
- he->next=he2->next;
- he2->next=he;
- if(n==hyh)
- {
- tail=he;
- }
- }
- void usun(int n)
- {
- Element *he = head;
- if(n == 1)
- {
- head = he->next;
- delete he;
- }
- else
- {
- for(int i=0; i<n-2; i++)
- he = he->next;
- Element *he2 = he->next;
- he->next = he2->next;
- tail=he;
- delete he2;
- }
- }
- void wyswietl()
- {
- Element *he = new Element();
- he = head;
- cout<<"\nLista: ";
- while(he != NULL)
- {
- cout<<he->el<<" ";
- he = he->next;
- }
- cout<<"\n\n";
- }
- void srednia(int *pom)
- {
- float suma=0;
- int ile=*pom-1;
- Element *he = new Element();
- he = head;
- cout<<"\nLista: ";
- while(he != NULL)
- {
- suma+=he->el;
- he = he->next;
- }
- float sredniaa = suma/ile;
- cout<<"\nSrednia arytmetyczna wynosi: "<<sredniaa<<endl;
- }
- void usunL()
- {
- Element *he = new Element();
- while(head!=NULL)
- {
- he = head;
- head = he->next;
- delete he;
- }
- tail=NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement