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 = NULL;
- struct Element * head = NULL;
- Element * init(){
- struct Element *wsk;
- wsk = (struct Element *)malloc(sizeof(struct Element));
- wsk->wartosc = NULL;
- wsk->next = NULL;
- wsk->prev = NULL;
- head = wsk;
- return wsk;
- }
- int raz = 1;
- bool dodaj(Element * glowa, int x){
- Element * prev = NULL;
- Element * wsk = glowa;
- Element * tmp;
- if (wsk->wartosc == 0)
- {
- // tmp = head;
- wsk->wartosc = x;
- wsk->next = NULL;
- wsk->prev = NULL;
- return true;
- }
- /*while (raz)
- {
- wsk->wartosc = x;
- wsk->next = wsk;
- raz--;
- return true;
- }
- if (wsk->wartosc == x)
- {
- printf("Element o wartosci %d znajduje sie juz na liscie\n", wsk->wartosc);
- return false;
- }*/
- while (wsk->next != NULL)
- {
- prev = wsk;
- wsk = wsk->next;
- if (wsk->wartosc == x)
- {
- printf("Element o wartosci %d znajduje sie juz na liscie\n", wsk->wartosc);
- return false;
- }
- if (x < wsk->wartosc)
- {
- tmp = wsk;
- wsk->wartosc = x;
- tmp->prev = NULL;
- wsk = tmp;
- return true;
- }
- if (wsk->wartosc > x)
- {
- while (wsk != NULL) {
- 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;
- }
- }
- else
- 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;
- }
- }
- void wypisz(Element * glowa)
- {
- while (glowa != NULL)
- {
- printf("%d ", glowa->wartosc);
- glowa = glowa->next;
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- Element *glowa;
- glowa = init();
- dodaj(glowa, 99);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 1167);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 5);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 1000000);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 67);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 66);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 22);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 39183);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 9);
- wypisz(glowa);
- system("PAUSE");
- dodaj(glowa, 83721);
- wypisz(glowa);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement