/ Published in: C++
Implementacija liste pomocu pokazivaca
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#ifndef _LISTA_POKAZIVAC_H_ #define _LISTA_POKAZIVAC_H_ struct _tempList { int sifra; std::string naziv,vrsta,datum; float cijena; }; struct _list { _tempList a; _list *next; }; typedef _list *List; typedef _list Array; _list *FirstL(_list *list) { return list->next; } _list *EndL(_list *list) { _list *last = list; while (last->next) last = last->next; return last->next; } _list *NextL(_list *temp,_list *list) { if (temp == EndL(list)) return NULL; return temp->next; } _list *PreviousL(_list *temp,_list *list) { if (temp == FirstL(list)) return 0; _list *search = list->next; _list *previous = list; while (true) { if (search == temp) return previous; search = search->next; previous = previous->next; } } _list *LocateL(int temp, _list *list) { _list *search = list->next; while (search) { if (search->a.sifra == temp) return search; search = search->next; } return EndL(list); } _list *LocateL(float temp,_list *list) { _list *search = list->next; while (search) { if (search->a.cijena == temp) return search; search = search->next; } return EndL(list); } _list *LocateL(std::string temp,_list *list) { _list *search = list->next; while (search) { if (search->a.datum == temp || search->a.naziv == temp || search->a.vrsta == temp ) return search; search = search->next; } return EndL(list); } bool InsertL(_tempList copyElement,_list *index,_list *list) { _list *newElement = new _list; _list *search = list->next; _list *previous = list; while (search != index) { search = search->next; previous = previous->next; } newElement->next = previous->next; previous->next=newElement; newElement->a.cijena = copyElement.cijena; newElement->a.datum = copyElement.datum; newElement->a.naziv = copyElement.naziv; newElement->a.sifra = copyElement.sifra; newElement->a.vrsta = copyElement.vrsta; return true; } bool DeleteL(_list *index,_list *list) { _list *search = list->next; _list *previous = list; while (search) { if (search == EndL(list)) return false; if (search == index) { previous->next = search->next; search->next = NULL; delete search; return true; } previous = search; search = search->next; } } _tempList RetrieveL(_list *index, _list *list) { // ne vraca pokazivac jer u mainu hardkodirano _tempList a ne _tempList* _list *search = list; while (search) { if (search == index) return search->a; search = search->next; } } bool DeleteAllL(_list *list) { if (!list->next) { delete list; return true; } _list *previous = list->next; _list *search = previous->next; while (search) { delete previous; previous = search; search = search->next; } delete previous; list->next = NULL; return true; } bool InitL(_list *list) { list->next = NULL; return true; } #endif