Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lista.cpp : Dyefines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include "conio.h"
- #include "stdlib.h"
- struct Element
- {
- struct Element *next, *prev;
- int wartosc;
- };
- struct Element * tail;
- struct Element * head;
- Element * init(){
- Element * nowa_lista = (struct Element *) malloc(sizeof(struct Element));
- nowa_lista->wartosc = NULL;
- nowa_lista->next = NULL;
- nowa_lista->prev = NULL;
- head = nowa_lista;
- return nowa_lista;
- }
- //void dodajlos(lista * l)
- bool dodaj(Element * glowa, int x){
- Element * prev = NULL;
- Element * wsk = glowa;
- Element * tmp;
- /*if(wsk == NULL)
- {
- *glowa = nowy_element;
- wsk = nowy_element;
- //*glowa->next = NULL;
- //*glowa->prev = NULL;
- return 0;
- }*/
- while (wsk->next != NULL)
- {
- prev = wsk;
- wsk = wsk->next;
- if (wsk->wartosc == x)
- {
- printf("Element o wartosci %d znajduje sie juz na liscie", wsk->wartosc);
- return false;
- }
- if (wsk->wartosc > x)
- {
- tmp = wsk->prev;
- wsk->prev = (struct Element *)malloc(sizeof(struct Element));
- wsk->prev->next = wsk;
- wsk->prev->prev = tmp;
- wsk->prev->wartosc = x;
- wsk->prev->prev->next = wsk->prev;
- return true;
- }
- if (wsk->next == NULL) break;
- }
- if (wsk->next == NULL)
- {
- wsk->next = (struct Element *)malloc(sizeof(struct Element));
- wsk->next->prev = wsk;
- wsk = wsk->next;
- wsk->wartosc = x;
- wsk->next = NULL;
- tail = wsk;
- return true;
- }
- }
- /*i
- else
- {
- if (wsk->wartosc < x && wsk->prev == NULL && wsk->next = NULL)
- {
- wsk->next = nowy_element;
- nowy_element->prev = wsk;
- return 0;
- }
- else
- {
- if (!prev)
- {
- wsk->prev = nowy_element;
- nowy_element->next = wsk;
- wsk = nowy_element;
- nowy_element->prev = NULL;
- return 0;
- }
- else
- {
- prev->next = nowy_element;
- nowy_element->next = *glowa;
- return 0;
- }
- wsk = wsk->next;
- }
- wsk->next = nowy_element;
- nowy_element->next = NULL;
- return 0;
- }
- }*/
- /*for (i = 0; i <= licz; i++)
- {
- if (l[i].wartosc == x)
- {
- czy = 1;
- }
- // else
- // l = l->next;
- }
- l = l;
- if (l->next != NULL && czy != 1)
- {
- //l = l->next;
- l->next = nowy_element;
- }
- else
- cout << "Taka wartosc juz istnieje" << endl;
- }*/
- void wypisz(Element * glowa)
- {
- while (glowa != NULL)
- {
- printf("%d ", glowa->wartosc);
- glowa = glowa->next;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- Element *glowa;
- glowa = init();
- //Element *glowa = (l);
- //lista * l = new lista;
- dodaj(glowa, 11);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 115);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 56);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 117);
- wypisz(glowa);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement