Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- using namespace std;
- struct elListy2K {
- int x;
- struct elListy2K *nast;
- struct elListy2K *pop;
- };
- struct Lista2K {
- struct elListy2K *head;
- struct elListy2K *tail;
- };
- void addElement(struct Lista2K* myList, int value , int pos){
- struct elListy2K * new_el = (struct elListy2K*) malloc(sizeof(struct elListy2K));
- new_el -> x = value;
- if(myList -> head == NULL){
- myList -> head = new_el;
- myList -> tail = new_el;
- new_el -> nast = NULL;
- new_el -> pop = NULL;
- }
- else
- if (pos==1)
- {
- myList -> head -> pop = new_el;
- new_el->nast=myList->head;
- new_el -> pop = NULL;
- myList->head=new_el;
- }
- else
- {
- int ile=1;
- struct elListy2K * temp=myList->head;
- while(ile<pos && temp!=NULL)
- {
- temp=temp->nast;
- ile++;
- }
- if (temp!=NULL)
- {
- //wstawiamy przed temp
- temp->pop -> nast = new_el;
- new_el -> pop = temp -> pop;
- temp-> pop = new_el;
- new_el -> nast = temp;
- }
- else
- {
- //wstawiamy na koniec listy
- myList -> tail -> nast = new_el;
- new_el->pop=myList->tail;
- new_el -> nast = NULL;
- myList->tail=new_el;
- }
- }
- }
- void deleteElement(struct Lista2K* myList, int position){
- struct elListy2K * new_el = (struct elListy2K*) malloc(sizeof(struct elListy2K));
- int ile=1;
- struct elListy2K * temp = myList -> head;
- while(ile<position && temp!=NULL)
- {
- temp=temp->nast;
- ile++;
- }
- if (temp==NULL)
- {
- cout<<" Nie ma nic na tej pozycji ";
- }
- else
- {
- if(temp->nast == NULL){
- myList -> tail = temp -> pop;
- temp->pop->nast = NULL;
- }else if (temp->pop ==NULL)
- {
- myList -> head = temp -> nast;
- myList->head -> pop = NULL;
- }
- else{
- temp -> nast -> pop = temp -> pop;
- temp -> pop -> nast = temp -> nast;
- }
- }
- free(temp);
- }
- int deletChoosenElements(struct Lista2K* myList, int element_to_delete ){
- struct elListy2K * new_el = (struct elListy2K*) malloc(sizeof(struct elListy2K));
- int test[2];
- int ile=1;
- bool find=false;
- struct elListy2K * temp = myList -> head;
- int how_many_delete = 0;
- while(temp!=NULL)
- {
- if(temp->x == element_to_delete){
- temp=temp->nast;
- deleteElement(myList, ile);
- how_many_delete++;
- }
- else
- {
- temp=temp->nast;
- ile++;
- }
- }
- if (temp==NULL)
- {
- }
- else
- {
- temp -> pop -> nast = temp -> nast;
- }
- free(temp);
- return how_many_delete;
- }
- void write(struct Lista2K *myList){
- struct elListy2K *el;
- el = myList -> head;
- while(el!=NULL){
- cout << "| " << el -> x<<" ";
- el= el -> nast;
- }
- el = myList -> head;
- cout <<" \n";
- }
- void reversWrite(struct Lista2K *myList){
- struct elListy2K *el;
- el = myList -> tail;
- while(el!=NULL){
- cout << "| " << el -> x <<" ";
- el= el -> pop;
- }
- cout <<" \n";
- el = myList -> tail;
- }
- int main(int argc, char** argv) {
- struct Lista2K * myList = (struct Lista2K*) malloc(sizeof(struct Lista2K));
- myList -> head = NULL;
- myList -> tail = NULL;
- int position = 1;
- int option = 0;
- int value_user;
- int position_user;
- int how_many;
- cout <<"Wybierz opcje: \n";
- cout <<" 1.Dodanie elementu do listy na podaną pozycję\n 2.Usunięcie elementu z listy z podanej pozycji\n ";
- cout << "3.Usunięcie z listy wszystkich elementów o podanej wartości i wyświetlenie ich liczby \n 4.Wydruk elementów listy od początku \n 5.Wydruk elementów listy od konca.\n 6.Koniec programu";
- while(option!=6){
- cout <<"\n Wybierz opcje: \n";
- cin >> option;
- switch( option )
- {
- case 1:
- cout << "Ile elementow chcesz dodac?";
- cin >> how_many;
- for(int i=0;i<how_many;i++){
- cout << "Podaj wartosc: \n";
- cin >> value_user;
- cout << "Podaj numer pozycji na jaka chcesz wstawic: \n";
- cin >> position_user;
- addElement(myList,value_user, position_user );
- }
- break;
- case 2:
- cout << "Element z ktorej pozycji chcesz usunac?\n";
- write(myList);
- cin >> position_user;
- deleteElement(myList, position_user);
- write(myList);
- break;
- case 3:
- cout << "Jakie wartosci chcesz usunac z listy?\n";
- write(myList);
- cin >> value_user;
- cout << "Usunieto: " << deletChoosenElements(myList,value_user) << " wartosci \n";;
- write(myList);
- break;
- case 4:
- write(myList);
- break;
- case 5:
- reversWrite(myList);
- break;
- case 6:
- cout <<"Wychodze";
- break;
- default:
- cout<<" Nie ma takiej opcji ";
- break;
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement