$EVWUDNWDGDWDW\SHURFK 'DWDVWUXNWXUHU Dstr - 1 3URJUDPE\JJQDGVNRQVWHQV HOHPHQW Dstr - 2 $EVWUDNWDGDWDW\SHU +HOWDO/LVWD6WDFN.|7DEHOO $OJRULWPHU 6|NQLQJVRUWHULQJWUDYHUVHULQJ .RQWUROOVWUXNWXUHU IRULIZKLOH 1 Dstr - 3 'DWDW\SHQ Dstr - 4 $QYlQGDUH VSHFLILNDWLRQ LPSOHPHQWDWLRQ 'DWD ElU LQIRUPDWLRQ JHQRP DWW UHSUHVHQWHUD QnJRW ² 'DWDREMHNWHW µ/XQFK PHOODQ RFK µ DY W\SHQ WH[W NDQ LQIRUPHUD RP DWW DIIlUHQ lU VWlQJG JHQRP DWW UHSUHVHQWHUD DWW GH VRP DUEHWDU GlU lWHU OXQFK PHOODQ NORFNDQ RFK 'DWDW\SHQ ² 'DWDW\S ² (Q GDWDW\S lU HQ W\S DY GDWD 'DWDW\S ² $QYlQGDUHQ $QYlQGDUHQ lU EHJUlQVDG WLOO DWW DQYlQGD GDWDW\SHQ L HQOLJKHW PHG GHVV VSHFLILNDWLRQ REMHNW RSHUDWRUHU 7LOO GDWDW\SHQ KHOWDO K|U LQWH HQGDVW GH HQVNLOGD KHOWDOHQ XWDQ RFNVn IXQNWLRQHU VRP DGGLWLRQ RFK VXEVWUDNWLRQ UHODWLRQHU VRP ·PLQGUH lQ· RFK ·VW|UUH lQ· RVY $EVWUDNW GDWDW\S ² 6SHFLILNDWLRQHQ EHVNULYHU JUlQV\WDQ WLOO GDWDW\SHQV LPSOHPHQWHULQJ (Q WHUP VRP DQYlQGV QlU PDQ YLOO EHVNULYD GLVNXWHUD HOOHU DQYlQGD HQ GDWDW\S XWDQ DWW WD QnJUD KlQV\Q WLOO YDUH VLJ RP HOOHU KXU GHQ lU UHDOLVHUDG L SURJUDPVSUnNHW RFK KnUGYDUDQ 8SSPlUNVDPKHWHQ NRQFHQWUHUDV Sn YDG RSHUDWLRQHUQD J|U RFK YLOND UHVXOWDW GH JHU ,PSOHPHQWDWLRQHQ DY GDWDW\SHQ lU GROG I|U DQYlQGDUHQ 2 Dstr - 5 Dstr - 6 'DWDW\SHQVWDFN 'DWDW\SHQVWDFN )|UVODJWLOOVSHFLILNDWLRQ pop() push() SRS 2P VWDFNHQ lU WRP UHWXUQHUDU IXQNWLRQ ·SRS· QROO , DQQDW IDOO UHWXUQHUDU IXQNWLRQHQ GHW YlUGH VRP I|UHNRPPHU L WRSSHQ DY VWDFNHQ RFK QlVWD YlUGH L VWDFNHQ NRPPHU DWW EOL VWDFNHQV WRSS SXVK )XQNWLRQHQ ·SXVK· DQURSDV PHG HQ SDUDPHWHU VRP KDU GHW YlUGH VRP VND SODFHUDV OlQJVW XSS L VWDFNHQ 6WDFNHQV WRSS NRPPHU GlUHIWHU DWW EHVWn DY GHWWD Q\D YlUGH HPSW\ )XQNWLRQHQ ·HPSW\· UHWXUQHUDU ·WUXH· RP VWDFNHQ lU WRP L DQQDW IDOO UHWXUQHUDU IXQNWLRQHQ ·IDOVH· 3 Dstr - 7 Dstr - 8 'DWDW\SHQVWDFN )|UVODJWLOOJUlQV\WD template <class TYPE> class Stack { public: Stack(); virtual ~Stack(); 'DWDW\SHQVWDFN )|UVODJWLOOHQLPSOHPHQWDWLRQ push(7); //Konstruktor //Destruktor TYPE pop(); //Tar bort elementet i stackens //topp. void push(TYPE item); //Lägger in ett nytt element i //stackens topp top 0 top 1 data top 2 data [0] bool empty(void)const; //Sann om stacken är tom [1] private: //DET HÄR ÄR INGET FÖR ANVÄNDAREN AV STACK !!! TYPE *data; int top; }; push(5); [2] [3] [4] [5] [9999] [10000] Z / & ? ¤ # a a data [0] [1] [2] [3] [4] [5] [9999] [10000] 7 / & ? ¤ # [0] a a [9999] [1] [2] [3] [4] [5] [10000] 5 7 & ? ¤ # a a 4 Dstr - 9 Dstr - 10 'DWDW\SHQVWDFN 'DWDW\SHQVWDFN )|UVODJWLOOHQLPSOHPHQWDWLRQ ([HPSHOSnDQYlQGQLQJ //-------------------------------------------------template <class TYPE> Stack<TYPE>::Stack(void) { top = 0; data = new TYPE[10000]; } //-------------------------------------------------template <class TYPE> Stack<TYPE>::~Stack(void) { delete [] data; } //-------------------------------------------------template <class TYPE> TYPE Stack<TYPE>::pop(void) { if (!empty()) return data[--top]; else return 0; } //-------------------------------------------------template <class TYPE> void Stack<TYPE>::push(TYPE item) { data[top++] = item; } //-------------------------------------------------template <class TYPE> bool Stack<TYPE>::empty(void)const { return (top > 0) ? false : true; } //-------------------------------------------------- #include <iostream.h> #include "stack.h" int main(void) { cout <<"Skriv några tal så räknar jag upp dom!" <<endl; cout << "Bryt inmatningen med noll." << endl; cout << endl; Stack<double> stacken; int nr = 1; double tal = -1.0; cout << "Tal "<< nr++ << " =>: "; cin >> tal; while (tal !=0.0) { stacken.push(tal); cout << "Tal "<< nr++ << " =>: "; cin >> tal; } cout <<endl <<"Nu skriver jag ut talen!" <<endl <<endl; while (!stacken.empty()) cout << stacken.pop() << '\t'; cout << endl << endl; cout << "Det var alla tal i omvänd ordning!" << endl; return 0; } 5 Dstr - 11 Dstr - 12 'DWDW\SHQVWDFN 'DWDW\SHQVWDFN 6DPPDJUlQV\WDPRWDQYlQGDUHQ 0HQHQKHOWDQQDQ LPSOHPHQWDWLRQ template <class TYPE> class Stack { public: Stack(); virtual ~Stack(); push(7); push(5); //Konstruktor //Destruktor TYPE pop(); //Tar bort elementet i stackens //topp. void push(TYPE item); //Lägger in ett nytt element i //stackens topp top 5 top bool empty(void)const; //Sann om stacken är tom 7 private: //DET HÄR ÄR INGET FÖR ANVÄNDAREN AV STACK !!! Node<TYPE> *top; }; 7 top 6 Dstr - 13 'DWDW\SHQVWDFN (QKHOWDQQDQLPSOHPHQWDWLRQGHO //*Node********************************************** template <class TYPE> class Node { friend class Stack<TYPE>; private: Node(TYPE item, Node* nextNode = 0); TYPE data; Node *next; }; //*************************************************** //-Node---------------------------------------------template <class TYPE> Node<TYPE>::Node(TYPE item, Node* nextNode) { data = item; next = nextNode; } //--------------------------------------------------- Dstr - 14 'DWDW\SHQVWDFN (QKHOWDQQDQLPSOHPHQWDWLRQGHO //-Stack--------------------------------------------template <class TYPE> Stack<TYPE>::Stack(void) { top = 0; } //-------------------------------------------------- //-~Stack------------------------------------------template <class TYPE> Stack<TYPE>::~Stack(void) { while (!empty()) { Node<TYPE> *extraPek = top; top = top->next; delete extraPek; } } //-------------------------------------------------- //-push--------------------------------------------template <class TYPE> void Stack<TYPE>::push(TYPE item) { if (empty()) top = new Node<TYPE>(item); else top = new Node<TYPE>(item, top); } //-------------------------------------------------- 7 Dstr - 15 'DWDW\SHQVWDFN Dstr - 16 'DWDW\SHQN| (QKHOWDQQDQLPSOHPHQWDWLRQGHO //-pop---------------------------------------------template <class TYPE> TYPE Stack<TYPE>::pop(void) { if (!empty()) { TYPE item = top->data; Node<TYPE> *extraPek = top; HQTXHXH 4XHXH top = top->next; delete extraPek; return item; } else return 0; } //-------------------------------------------------- //-empty-------------------------------------------template <class TYPE> bool Stack<TYPE>::empty(void)const { return (top != 0) ? false : true; } //-------------------------------------------------- GHTXHXH 8 Dstr - 17 'DWDW\SHQN| )|UVODJWLOOVSHFLILNDWLRQ Dstr - 18 'DWDW\SHQN| )|UVODJWLOOJUlQV\WD 9 Dstr - 19 Dstr - 20 'DWDW\SHQOLVWD 'DWDW\SHQOLVWD Exempel på användning #include "dubbellista.h" int main(void) { oos_List<char*> minLista; minLista.append("ett"); minLista.append("två"); minLista.append("tre"); minLista.append("fyra"); minLista.append("fem"); LQVHUW) oos_Iter<char*> j(minLista); for (j.last(); j.okay(); j.prev()) cout << j() << endl; cout << endl; minLista.insert("sex"); UHPRYH for (j.first(); j.okay(); j.next()) cout << j() << endl; cout << endl; minLista.clear(); fem fyra tre tvÕ ett ett tvÕ tre fyra fem sex return 0; } 1 Dstr - 21 'DWDW\SHQOLVWD *UlQV\WD Dstr - 22 'DWDW\SHQOLVWD 6SHFLILNDWLRQ template <class TYPE> class oos_List { friend class oos_Iter<TYPE>; public: oos_List(); oos_List(const TYPE &t); ~oos_List(); void append(const TYPE &); void prepend(const TYPE &); void insert(const TYPE &); void clear(); void remove(); void last(); void first(); TYPE &curItem()const; TYPE &firstItem()const; TYPE &lastItem()const; bool okay()const; bool hasCur()const; bool hasFirst()const; bool hasLast()const; void next(); void prev(); bool hasNext()const; bool hasPrev()const; int size()const; private: //DET HÄR ÄR INGET FÖR DEN SOM ANVÄNDER LISTAN! int _size; oos_Link<TYPE> *_cur; oos_Link<TYPE> *_last; oos_Link<TYPE> *_first; }; 1 Dstr - 23 Dstr - 24 ,WHUDWRU *UlQV\WD ,WHUDWRU 6SHFLILNDWLRQ template <class TYPE> class oos_Iter { public: oos_Iter(const oos_List<TYPE> &l); void first(); void last(); void next(); void prev(); bool okay()const; TYPE &operator()()const; TYPE &item()const; int hasNext()const; int hasPrev()const; private: oos_Link<TYPE> *link; const oos_List<TYPE> &list; }; 1 Dstr - 25 'XVNDNXQQD« 'X VND YHWD YDG HQ DEVWUDNW GDWDW\S lU 'X VND NXQQD DQYlQGD GDWDW\SHUQD VWDFN N| RFK OLVWD 'X VND NXQQD DQYlQGD HQ LWHUDWRU 'X VND I|UVWn KXU OlQNDGH VWUXNWXUHU IXQJHUDU 1