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){
- cout << " nastepny to zero ";
- cout << " poprzedni " << temp->pop->x << "|";
- myList -> tail = temp -> pop;
- temp->pop->nast = NULL;
- }else if (temp->pop ==NULL)
- {
- myList -> head = temp -> nast;
- myList->head -> pop = NULL;
- }
- else{
- cout << "mam obok siebie na prawo i lewo \n usuwam dziada";
- temp -> nast -> pop = temp -> pop;
- temp -> pop -> nast = temp -> nast;
- }
- }
- }
- void 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;
- while(temp!=NULL)
- {
- if(temp->x == element_to_delete){
- cout << " wyrzucam " << temp->x;
- temp=temp->nast;
- deleteElement(myList, ile);
- }
- else
- {
- temp=temp->nast;
- ile++;
- }
- }
- if (temp==NULL)
- {
- cout<<" doszedlem do konca ";
- }
- else
- {
- temp -> pop -> nast = temp -> nast;
- }
- }
- 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;
- addElement(myList, 10, 1);
- addElement(myList, 20, 2);
- addElement(myList, 30, 3);
- addElement(myList, 40, 4);
- addElement(myList, 50, 5);
- addElement(myList, 60, 6);
- deletChoosenElements(myList, 40);
- write(myList);
- reversWrite(myList);
- /* el = myList -> head;
- while(el!=NULL){
- cout << el -> x<<", ";
- el= el -> nast;
- } */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement