Solved

# Stack, Linked List - help on code :(

Posted on 2003-03-28

Ok I posted a question a day or so ago asking about a stack implemented as an array... I got that code working and now I am looking at a linked list implementation, and basically I am very confused and am not totally aware of how a linked list works but I have some code written up. Maybe someone could take a look at it and let me know how to improve it or provide an example?? Because I know what I have it wrong, and Im not sure where to go from here... be ready, here is my ugly code :)

1. Is my general code correct?

2. Any way to shorten or improve the pop/push methods?

3. I am very unsure how isFull would work?

4. Any other tips appreciated.

stack.h

----------

#ifndef LOTTERY_H

#define LOTTERY_H

struct Node

{

Node* next;

Node* previous;

int Data;

}

class Stack

{

public:

Stack();

void push(int);

int pop();

bool isFull();

bool isEmpty();

private:

node top;

node *current;

};

#endif

--------------

stack.cpp

--------------

#include "stack.h"

Stack::Stack()

{

top=NULL;

}

void Stack::push(int item)

{

node *temp=current;

current->next=new node;

current=current->next;

current->data=item;

current->previous=temp;

}

int Stack::pop()

{

int temp;

temp=current->data;

if(current->previous==NULL)

{return temp;}

current=current->previous;

delete current->next;

return temp;

}

bool Stack::isFull();

{

// ??

}

bool Stack::isEmpty();

{

return(top==NULL;)

}