Thread: 'pop' stacks

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    'pop' stacks

    I think i have the push working .... at least no errors when I run it, but I am lost with the pop .... & in the program how do you 'push' & 'pop' the number to the stack????????

    Code:
    bool stacklist::push()
    {
    	//ptrs = number(top), stacklist(head), push(nptr)
    	bool success;
    	number* nptr = new number;
    	
    	nptr->remainder = rem;
    
    	if (head != NULL)
    	{
    		success = false;
    	}
    	else
    	{
    		if (head == NULL)
    		{
    			nptr->top = NULL;
    			head = nptr;
    		}
    		else
    		{
    			nptr->top = head;
    			head = nptr;
    		}
    		count++;
    	}
    	success = true;
    }
    
    bool stacklist::pop()
    {
    	bool success;
    	number* dlptr = new number;
    
    
    	if (head != NULL)
    	{
    		success = false;
    	}
    	else
    	{
    		if (head == NULL)
    		{
    			cout << "Nothing to pop" << endl;
    		}
    		else
    		{
    			dlptr = top;
    			
    		}
    }
    [MOD EDIT: Disabled Smilies]

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Push:
    Code:
    newnode->next = head;
    head = newnode;
    Pop:
    Code:
    head = head->next;
    Make sure you understand the concepts involved in adding and removing at the head of a linked list before trying to write your code.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    a stack is often used in a program like this:

    stacklist myStack;
    number num;
    cout << "enter a number" << endl;
    cin >> num;
    myStack.push(num);
    cout << "you entered" << myStack.pop();

    The exact syntax will depend on your code however. For example, the value to push on the stack may not come from user, but from some container/file/etc. Likewise, pop doesn't need to return a value, it just needs to remove a value from the stack, though it often does return the value removed from the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  2. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 02:08 AM
  3. stacks - push - pop
    By student2005 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 08:15 PM
  4. Stack using push and pop
    By silicon in forum C++ Programming
    Replies: 5
    Last Post: 11-03-2003, 04:54 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM