Stack error / Can't push and pop 1 item
Mar 30, 2014 at 4:08pm UTC
Hi all,
I am taking a data structures class and all of the code except for the stackTest.cpp file has come from the instructor as part of the assignment. If a stack is to be viewed as a last in first out structure, then shouldn't I be able to push one item onto the stack and then pop said item back off without error. The error am I getting is access violation writing location 0xcdcdcdd1. I have typed these files into Microsoft Visual C++ 2010 Express as well as the unix/putty server for the assignment. On the unix terminal, I get a memory error (core dumped). If anyone can help explain why this is happening or maybe I am not fully understanding the use of a stack, would be greatly appreciated. Thanks in advance.
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
/*
* File: list.h
* Purpose: Just a small linked list class
*/
#ifndef LIST_H
#define LIST_H
template <typename T>
class List{
private :
class Node;
public :
//Constructor and Destructor
List(){
head = tail = 0x00;
}
~List() {
Node *cur;
//start at the head
cur = head;
//kill all things
while (cur) {
cur = cur->next;
delete head;
head = cur;
}
}
//high level operations
void append(T data) {
Node *node = createNode(data);
//special case, empty list!
if (isEmpty()) {
head = tail = node;
return ;
}
node->prev = tail;
tail->next = node;
tail = node;
}
void prepend(T data) {
Node *node = createNode(data);
//special case, empty list!
if (isEmpty()) {
head = tail = node;
return ;
}
node->prev = 0x00;
node->next = head;
head=node;
}
bool isEmpty() {
return !head;
}
//iterator
class iterator {
public :
//dereference
T &operator *() {
return node->data;
}
//comparison
bool operator ==(const iterator &rhs) {
return node == rhs.node;
}
bool operator !=(const iterator &rhs) {
return node != rhs.node;
}
//movement
iterator &operator ++() {
if (node) node=node->next;
return *this ;
}
iterator operator ++(int ) {
iterator itr(*this );
if (node) node=node->next;
return itr;
}
iterator &operator --() {
if (node)
node=node->prev;
else
node=l.tail;
return *this ;
}
iterator operator --(int ) {
iterator itr(*this );
if (node)
node=node->prev;
else
node=l.tail;
return itr;
}
iterator & operator =(const iterator &itr) {
this ->node = itr.node;
return *this ;
}
private :
//current node
Node *node;
List &l;
//private constructor
iterator(Node *node, List &lst) : l(lst) { this ->node = node; }
//we have but one friend
friend class List;
};
//iterator creation
iterator begin() {
return iterator(head, *this );
}
iterator end() {
return iterator(0x00, *this );
}
//iterator action
void insertAfter(iterator &itr, T data) {
//special case, insertion at the tail
if (!itr.node || itr.node==tail) {
append(data);
return ;
}
//insert after the iterator node
Node *node = createNode(data);
node->next = itr.node->next;
itr.node->next = node;
}
void remove(iterator &itr) {
Node * node=itr.node;
//update the iterator
itr--;
//handle the head node
if (node==head) {
head = node->next;
}
//handle the tail node
if (node == tail) {
tail = tail->prev;
}
//unlink
if (node->prev)
node->prev->next = node->next; //this is the line that seems to cause the error
if (node->next)
node->next->prev = node->prev;
//remove the node
delete node;
}
private :
//inner type for storage of information
class Node {
public :
T data; //the payload
Node *next; //the link
Node *prev; //the previous node
Node() { next=0x00; }
};
//private data elements
Node *head;
Node *tail;
//private helpers
Node * createNode(T data) {
Node *node = new Node();
node->data = data;
return node;
}
/*Node * findPrevious(Node *node) {
Node * cur = head;
while(cur) {
if(cur->next == node)
return cur;
cur = cur->next;
}
}*/
};
#endif
#ifndef STACK_H
#define STACK_H
#include "list.h"
template <typename T>
class Stack : private List<T> {
public :
//stack operations
void push(T data){
this ->prepend(data);
}
//pop the stack
T pop() {
T result;
typename List<T>::iterator itr = this ->begin();
result = *itr;
this ->remove(itr);
return result;
}
T peek() {
return *(this ->begin());
}
bool isEmpty() {
return List<T>::isEmpty();
}
};
#endif
//stackTest.cpp
#include <iostream>
#include <string>
#include "stack.h"
using namespace std;
int main(void )
{
Stack<int > s;
string output(" " );
string input;
cout << "input: " ;
cin >> input;
for (int i = 0; i < input.length(); i++)
{
if (input[i] == 42)
{
cout << "*" << endl;
s.push(input[i]); //* pushed on
if (s.isEmpty() )
cout << "stack empty" << endl;
else
cout << "* pushed on" << endl;
}
if (input[i] == 43)
{
if (!s.isEmpty() )
{
cout << "+" << endl;
cout << s.pop() << endl;
if (s.isEmpty() )
cout << "* popped" << endl;
output += "* " ;
cout << "* popped" << endl;
s.push(input[i]);
cout << "+ is now on stack top" << endl;
}
}
}
cout << "output: " << output << endl;
system("pause" );
return 0;
}
Topic archived. No new replies allowed.