stack using linked list problem

This is a discussion on stack using linked list problem within the C++ Programming forums, part of the General Programming Boards category; having issue with stack using linked list. 1) the num didnt give any value tho with the cout in count() ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    30

    Unhappy stack using linked list problem

    having issue with stack using linked list.

    1) the num didnt give any value tho with the cout in count()

    2) how to return string from pop()? i tried many ways like return ptr->s and stuff but didnt work

    could anyone pls point out where my mistake is? thanks heaps!

    header file

    Code:
    class Element{
    public:
    	std::string s;
    	Element *next;
    };
    
    Element * start = NULL;
    
    class Stack {
    public:
    	//temporary pointer
    	Element * ptr, *ptr2;
    	unsigned long num;
    
    	Stack();
    	~Stack();
    
    	void push(std::string n);
    	unsigned long count();
    	std::string pop();
    
    	
    };

    cpp.file

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include "stack.h"
    
    using namespace std;
    
    Stack::Stack(){		
    		num = 0;
    }
    
    void Stack :: push(std::string n){
    
    	ptr = new Element; //create space
    	ptr->s = n;		   // pointing at string
    	ptr->next = NULL;
    
    	if (start == NULL){ //empty
    		start = ptr;}	//ptr and start pointing at the same spot
    
    	else{			  //if start already has element
    		ptr2 = start; 
    		while (ptr2->next != NULL){
    			ptr2 = ptr2->next; // keep moving to next element
    		}  
    		//if next element is null,add new element
    		ptr2->next = ptr;
    		
    		num++;
    	}
    	/*cout << ptr->s << endl;*/
    }
    
    std:: string Stack::pop(){
    	//pop until stack is empty
    
    	std::string n2 = ptr->s ;
    
    	if ( start != NULL ){
    		ptr = start;
    		//if next element where ptr is pointing is null,delete current ptr
    		if (ptr->next == NULL){
    			delete ptr;
    			start = NULL;
    		}
    
    		else {
    			while (ptr->next != NULL){
    				ptr2 = ptr;
    				ptr = ptr->next;}
    			delete ptr;
    			ptr2->next = NULL;
    		}
    		num--;
    	}	
    
    	cout << n2 << endl;
    	return n2;
    
    }
    	
    unsigned long Stack::count(){
    	cout << num;
    	return num;
    }
    
    Stack :: ~Stack () {
    
    	while (start != NULL){
    		Element* temp = start;
    		start = start->next;
    		delete temp;
    	}
    }
    	
    int main(){
    	Stack s;
    	s.push("Hello");
    	s.push("world");
    	s.pop();
    	s.pop();
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you plan to initialize start to NULL at some point?

  3. #3
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    1) Do you realize everything declared in your Stack class is publicly accessible?

    2) Why would anyone/anything else care about explicitly using your Element class?

    3) Why do you need two Element pointers in your Stack class?

    4) Stacks should provide a way to access the top element via reference. You return the string by value from pop(). Instead, implement a top() function that
    returns the top element by reference (and constant reference as well). Pop should return void.
    Last edited by StainedBlue; 09-16-2009 at 02:19 PM.

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Maybe try drawing out what your program should do first. That helps a lot. StanedBlue asked a bunch of questions that seems like they would be taken care of if you plan this out a bit more.

    Further more your pop() functions is a bit odd for a stack... stacks are LIFO (last in first out), meaning that if the element isn't at the top, it doesn't get to leave.
    -- Will you show me how to c++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 01:26 AM
  2. Linked List Problem
    By Syneris in forum C++ Programming
    Replies: 4
    Last Post: 01-07-2006, 12:03 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  4. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21