Thread: Dynamic stack?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    Dynamic stack?

    I have an assignment to do and I don't know what the term dynamic stack means. All I was given was the header and the rest is up to me.

    Code:
    class Stack
    {
    public:
    	Stack();
    	Stack(const Stack &stck);
    	~Stack();
    	void push(int number);
    	void pop();
    	int top() const;
    	bool isEmpty() const;
    	int size() const;
    };
    I also have no idea what top() is supposed to do. Read the value of the top of the stack?

    Can someone write me some code how to make a stack of unknown size (I'm assuming that's what dynamic means)? I can write a "normal" stack but have no idea how to do a dynamic one.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I imagine your requirements are to write an implementation of a stack type that is unlimited in the amount of elements that it can hold (limited by hardware of course).

    Are you allowed to use existing standard C++ classes? If so, look up the vector class. You may find it useful. If you are not allowed to do this, then I would suggest you make a linked list of sorts. A doubly-linked list would do fine. You can use an array, however, you have to resize it everytime you need more space than it has to offer.

    top() is similar to pop(). It just means to return the element without actually removing it from the stack.

    For more information on the structure itself:

    http://en.wikipedia.org/wiki/Stack_(data_structure)

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    I don't quite follow you. Why would you need a doubly linked list for a stack? Shouldn't a singly linked list work?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It should, yes. It would be simpler to implement and would require less memory, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    Just a quick question.

    Why is this crashing at last printout?

    Code:
    	struct node
    	{
    		int data;
    		node *next;
    	};
    
    node *head;
    
    void push(int data)
    	{
    		node* newnode = new node;
    		newnode->data = data;
    		newnode->next = head;
    		head = newnode;
    	}
    
    void printout()
    	{
    		while(head != NULL)
    		{
    			node *save = head->next;
    			cout<< head->data <<' ';
    			delete head;
    			head = save;
    		}

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Maybe because you never initialize head to null?

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    Lol. Thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM