Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct element{
- int liczba;
- element* next;
- };
- struct lista{
- element *head;
- element *tail;
- int liczba_el;
- };
- bool isEmpty(lista l){
- if(l.tail==nullptr) return 1;
- else return 0;
- }
- void add_first(lista &l){
- element* el=new element;
- int x;
- cout<<"Jaki element chcesz dodac? ";
- cin>>x;
- el->liczba=x;
- el->next=l.head;
- l.head=el;
- if(l.tail==nullptr) l.tail=el;
- l.liczba_el++;
- }
- void add_last(lista &l){
- element *el=new element();
- int x;
- cout<<"Jaki element chcesz dodac? ";
- cin>>x;
- el->liczba=x;
- el->next=nullptr;
- if(l.tail!=nullptr) l.tail->next=el;
- l.tail=el;
- if(l.head==nullptr) l.head=el;
- l.liczba_el++;
- }
- void add_pos(lista &l,int pos){
- if(pos>l.liczba_el+1)cout<<"Nie mozna wstawic elementu na te pozycje"<<endl;
- else if(pos==l.liczba_el+1) add_last(l);
- else if(pos==1) add_first(l);
- else {
- element* el=new element;
- int x;
- cout<<"Jaki element chcesz dodac? ";
- cin>>x;
- el->liczba=x;
- element *temp=l.head;
- for(int i=1; i<pos-1;i++) temp=temp->next;
- el->next=temp->next;
- temp->next=el;
- l.liczba_el++;
- }
- }
- void delete_first(lista &l){
- if(isEmpty(l)==1) cout<<"Kolejka juz jest pusta"<<endl;
- else {
- element* temp=l.head;
- l.head=l.head->next;
- delete temp;
- if(l.head==nullptr) l.tail=nullptr;
- l.liczba_el--;
- }
- }
- void delete_last(lista &l){
- if(isEmpty(l)==1) cout<<"Kolejka juz jest pusta"<<endl;
- else {
- element* temp=l.tail;
- if(l.liczba_el==1){
- l.tail=nullptr;
- l.head=nullptr;
- }
- else {
- element* bef_temp=l.head;
- for(int i=1; i<l.liczba_el-1;i++) bef_temp=bef_temp->next;
- l.tail=bef_temp;
- l.tail->next=nullptr;
- }
- delete temp;
- l.liczba_el--;
- }
- }
- void delete_pos(lista &l,int pos){
- if(pos>l.liczba_el) cout<<"Nie ma elementu na tej pozycji"<<endl;
- else if(pos==1) delete_first(l);
- else if(pos==l.liczba_el+1) delete_last(l);
- else {
- element* temp=l.head;
- for(int i=1;i<pos-1;i++) temp=temp->next;
- element* temp_us=temp->next;
- temp->next=temp_us->next;
- delete temp_us;
- l.liczba_el--;
- }
- }
- void get_first(lista l){
- if(isEmpty(l)==1) cout<<"Lista jest pusta"<<endl;
- else cout<<l.head->liczba;
- }
- void get_last(lista l){
- if(isEmpty(l)==1) cout<<"Lista jest pusta"<<endl;
- else cout<<l.tail->liczba;
- }
- void medium(lista l){
- if(isEmpty(l)==1) cout<<"Lista jest pusta"<<endl;
- else {int suma=0;
- element* temp=l.head;
- for(int i=1; i<=l.liczba_el; i++){
- suma=suma+temp->liczba;
- temp=temp->next;
- }
- cout<<"Srednia arytmetyczna liczb w liscie to: "<<suma/l.liczba_el<<endl;
- }
- }
- void show(lista l){
- if(isEmpty(l)==1) cout<<"Lista jest pusta"<<endl;
- else {
- element* temp=l.head;
- for(int i=1; i<=l.liczba_el; i++){
- cout<<temp->liczba<<" ";
- temp=temp->next;
- }
- }
- }
- void delete_all(lista &l){
- while(l.tail!=nullptr){
- element* temp=l.head;
- l.head=l.head->next;
- delete temp;
- if(l.head==nullptr) l.tail=nullptr;
- l.liczba_el--;
- }
- }
- int main(){
- lista l;
- l.head=nullptr;
- l.tail=nullptr;
- l.liczba_el=0;
- int p;
- int znak;
- for(;;){
- cout<<"1. Empty"<<endl<<"2. add_first"<<endl<<"3. add_last"<<endl<<"4. add_pos"<<endl<<"5. delete_first"<<endl<<"6. delete_last"<<endl<<"7. delete_pos"<<endl<<"8. get_first"<<endl<<"9. get_last"<<endl<<"10. medium"<<endl<<"11. show"<<endl<<"12. delete_all"<<endl<<"13. Wyjscie"<<endl;
- cout<<"Wybierz zadanie"<<endl;
- cin>>znak;
- switch(znak){
- case 1:{if(isEmpty(l)==1) cout<<"Lista jest pusta"<<endl;
- else cout<<"Lista nie jest pusta"<<endl;
- break;}
- case 2:{add_first(l);
- break;}
- case 3:{add_last(l);
- break;}
- case 4:{cout<<"Na jaka pozycje chcesz wstawic element? ";
- cin>>p;
- add_pos(l,p);
- break;}
- case 5: {delete_first(l);
- break;}
- case 6: {delete_last(l);
- break;}
- case 7:{cout<<"Z jakiej pozycji chcesz usunac element? ";
- cin>>p;
- delete_pos(l,p);
- break;}
- case 8:{get_first(l);
- break;}
- case 9:{get_last(l);
- break;}
- case 10:{medium(l);
- break;}
- case 11:{show(l);
- break;}
- case 12:{delete_all(l);
- break;}
- case 13: exit(0);
- default :cout<<"Nie ma takiej opcji"<<endl;
- }
- fflush(stdin);
- if(getchar()) system("cls");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement