Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1.  
  2. LList *EnchantList = new LList; // Instantiate a Linked List object.
  3.  
  4. /* Looping and Pushing the Roster names into the front of the linked list */
  5. for (RosterUnit *pRosterUnit = *(p_D2CLIENT_PlayerUnitList); pRosterUnit; pRosterUnit = pRosterUnit->pNext)
  6. {
  7. PrintText("%s", pRosterUnit->szName);
  8. EnchantList->push(pRosterUnit->szName);
  9. }
  10.  
  11. /* Traverse the linked list and fetch the data from it while outputting it afterwards. How should i traverse the linked list properly because my WIP draft doesn't work :( */
  12. for(Node *EnchantItem = reinterpret_cast<Node *>(EnchantList->first()); EnchantItem != NULL; EnchantItem = reinterpret_cast<Node *>(EnchantList->next()))
  13. {
  14. PrintText("%s", EnchantItem->Data());
  15. }
  16.  
  17.  
  18. /* Class Header Below */
  19.  
  20. // llist1.hpp
  21. // ctrudeau@etude
  22. //
  23. // This file contains the declaration for the link list class.
  24. //
  25. // ----------------------------------------------------------------------------
  26.  
  27. #include <stdio.h>
  28.  
  29. // Node - container where objects in the linked list are stored
  30. class Node
  31. {
  32. public:
  33. Node( void *_pData )
  34. {
  35. pData = _pData;
  36. pNext = NULL;
  37. } // end constructor
  38.  
  39. Node *Next() { return( pNext ); }
  40. void SetNext( Node *pNode ) { pNext = pNode; }
  41. void *Data() { return( pData ); }
  42.  
  43. private:
  44. void *pData;
  45. Node *pNext;
  46. }; // end Node
  47.  
  48. // ----------------------------------------------------------------------------
  49.  
  50. class LList
  51. {
  52. public:
  53. LList()
  54. {
  55. pHead = NULL;
  56. pTail = NULL;
  57. pCurrent = NULL;
  58. } // end constructor
  59.  
  60. ~LList() { this->destroy(); }
  61.  
  62. // member functions for manipulating the list
  63. void push( void *pData );
  64. void queue( void *pData );
  65. void *pop();
  66. void destroy();
  67.  
  68. // member functions for traversing the list
  69. void top() { pCurrent = NULL; }
  70. void *first()
  71. {
  72. if( pHead == NULL )
  73. return( NULL );
  74.  
  75. return( pHead->Data() );
  76. } // end first
  77.  
  78. void *last()
  79. {
  80. if( pTail == NULL )
  81. return( NULL );
  82.  
  83. return( pTail->Data() );
  84. } // end last
  85.  
  86. void *next();
  87.  
  88. private:
  89. Node *pHead;
  90. Node *pTail;
  91. Node *pCurrent;
  92. }; // end LList
  93.  
  94. // ----------------------------------------------------------------------------
  95.  
  96. /* Class Implementation (That i make use of) in the current code. */
  97.  
  98. void *LList::next()
  99. {
  100. // if the list is empty return NULL
  101. if( pHead == NULL )
  102. return( NULL );
  103.  
  104. // change the current pointer
  105. if( pCurrent == NULL )
  106. pCurrent = pHead;
  107. else
  108. pCurrent = pCurrent->Next();
  109.  
  110. if( pCurrent == NULL )
  111. return( NULL );
  112.  
  113. return( pCurrent->Data() );
  114. } // end next
  115.  
  116. void *first()
  117. {
  118. if( pHead == NULL )
  119. return( NULL );
  120.  
  121. return( pHead->Data() );
  122. } // end first
Add Comment
Please, Sign In to add comment