File: stacklis.cpp

package info (click to toggle)
bcpp 0.0.20050725-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 680 kB
  • ctags: 336
  • sloc: cpp: 4,410; sh: 2,871; makefile: 179
file content (176 lines) | stat: -rw-r--r-- 4,359 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// $Id: stacklis.cpp,v 1.3 2005/04/10 18:31:34 tom Exp $
#ifndef _STACK_LIST_CODE
#define _STACK_LIST_CODE

// Code written by Steven De Toni ACBC 11
// This file contains the methods that were defined in stacklist.h
// header file (i.e container class that stores items in a linked list,
// in stack form)

#include "stacklis.h"
#include <stdio.h>          // NULL Constant

// ############################################################################
// #### StackList Class ####
// #########################

// ############################## Public Methods ##############################
// ############################### Constructors ###############################

#define MY_DEFAULT \
      pCurrentPos(NULL), \
      itemCount(0), \
      spaceAvailable(0)

// Initialise internal variables.
//
StackList::StackList (void)
    : MY_DEFAULT
{
}

// Initalise variables, and place item passed in a new list
//
// Parameters:
//     pItem    : Pointer to the object that is will to be stored.
//                Item must be of descendant ANYOBJECT.
//
StackList::StackList (ANYOBJECT* pItem)
    : MY_DEFAULT
{
    push (pItem);
}


// Places a new item in the list (i.e on the stack).
//
// Parameters:
//     pItem    : Pointer to the object that is will to be stored.
//                Item must be of descendant ANYOBJECT.
//
// Return Values:
//     int      : Returns a error code value to indicate whether operation
//                was successful or not.
//                Value:
//                0  =  No Worries, item stacked.
//               -1  =  Item not stacked, memory allocation failure
//
int StackList::push (ANYOBJECT* pItem)
{
    ListLink*    newItem = new ListLink;

    if (newItem)           // not NULL
    {
         // update contents of structure
         newItem->pItem       = pItem;
         newItem->pLinkedItem = NULL;

         if (pCurrentPos)  // not NULL
             newItem->pLinkedItem = pCurrentPos;

         // update start of stack pointer
         pCurrentPos = newItem;
         itemCount++;
         return spaceAvailable;
    }
    else
    {
        spaceAvailable = -1;
        return  spaceAvailable;
    }
}

// Removes a item from the list and returns the value contained within it
// back to the user. A NULL value is returns if there are no more items
// within the list.
//
// Return Values:
//     ANYOBJECT* : Pointer to the object last object that was placed
//                  on the stack. Returns NULL pointer if operation
//                  failed.
//
ANYOBJECT* StackList::pop (void)
{
    ListLink*    pBackUp = pCurrentPos;

    if (pCurrentPos) // not NULL
    {
         ANYOBJECT* pTemp = pCurrentPos->pItem;
         pCurrentPos      = pCurrentPos->pLinkedItem;
         delete pBackUp;
         itemCount--;
         return pTemp;
    }
    else
        return NULL;
}

// Peeks at items within the linked list without removing
// them from the list.
//
// Parameters:
//    int item :     item number in list.
//
// Return Values:
//   ANYOBJECT*  : Returns NULL if operation failed, else
//                 pointer to the object contained at list
//                 number selected!
//
ANYOBJECT* StackList::peek (int item)
{
    ListLink*   pPeekPos = pCurrentPos;

    // invalid range !
    if ( ((item < 1) || (item > itemCount)) || (pPeekPos == NULL) )
              return NULL;

    while (item > 1)
    {
        pPeekPos = pPeekPos -> pLinkedItem;
        item--;
    }

    return pPeekPos -> pItem;
}

// Method returns whether last operation failed due to memory allocation
// failure.
//
// Return Values:
//     int  : Returns 1 of two values ...
//            Values:
//              0  =  memory available
//             -1  =  Last memory allocation failed.
//
int  StackList::space (void)
{
    return spaceAvailable;
}


// Returns the number of items current being stacked.
//
// Returns Values:
//     int :    Num of items within queue.
//
int  StackList::status (void)
{
    return itemCount;
}

// ############################### Destructor ###############################
// Method will remove all list items from memory if they still exist,
// no garbage collection provided, or used.
//
StackList::~StackList  (void)
{
    ANYOBJECT* pTest = pop();

    while (pTest != NULL)
    {
        delete pTest;
        pTest = pop();
    }
}

#endif