stack using linked list

Does everything look okay?

Page 1 of 1

2 Replies - 8446 Views - Last Post: 30 March 2010 - 03:38 PM Rate Topic: -----

#1 RLB31384   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 97
  • Joined: 29-January 10

stack using linked list

Posted 30 March 2010 - 11:47 AM

Okay im writing a program for stack using linked list, and I am implementing the following functionality:
Stack: 1. Push 2. Pop 3. IsEmpty 4. MakeEmpty

I was wondering if everything looked okay, and if I needed to add any deletes anywhere. When I add a temp node do I need to delete it after I am done with it? I would think so, but I wasn't sure. I am also going to add a queue using linked list, would it be better to make it a separate program or to add it in and give the user an option which to use? I am guessing the 2nd option would be be better, should I make two classes and put each in its own class? Any ideas? Thanks

#include <iostream>

using namespace std;

struct node
{  
	  int data;
	  node *nxt;// Pointer to next node
};

node *start_ptr = NULL;
int option = 0;

void pushElement()
  {  node *temp;   // Temporary pointer

     // Reserve space for new node and fill it with data
     temp = new node;
     cout << "Please enter the data to store in stack: ";
     cin >> temp->data;
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
       }
     else
       { temp->nxt = start_ptr;
         // We know this is not NULL - list not empty!
         
		start_ptr = temp;

       }
  }

void popElement()
  {  node *temp;   // Temporary pointer
	temp = new node;

     // Set up link to this node
     if (start_ptr == NULL)
	 { 
		   cout << "Can't pop element, when stack is empty." << endl;
     }
     else
     { 
		 temp->nxt = start_ptr;
         // We know this is not NULL - list not empty!
         
		 temp = start_ptr;
		 start_ptr = start_ptr->nxt;
		 delete temp;
      }
  }


void display_list()
  {  node *temp;
     temp = start_ptr;
     cout << endl;
     if (temp == NULL)
       cout << "The stack is empty!" << endl;
     else
		 cout << "Displaying stack: (top to bottom)" << endl;
       { while (temp != NULL)
	   {  // Display details for what temp points to
              cout << temp->data << endl;
	      temp = temp->nxt;

	   }
	 cout << "End of list!" << endl;
       }
  }

void isEmpty()
{ 
	if (start_ptr == NULL)
	{
		cout << "The stack is empty.";
	}
	else 
	{
		cout << "The stack is not empty.";
	}

}

void makeEmpty()
{
	while( start_ptr != NULL)
	{
		popElement();
	}
}

void main()
  {  start_ptr = NULL;
     do
	{
	  display_list();
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "0. Exit the program." << endl;
	  cout << "1. Push element on top of stack:" << endl;
	  cout << "2. Pop element off top of stack:" << endl;
	  cout << "3. Check if stack is empty: " << endl;
	  cout << "4. Make stack empty: " << endl;
      cout << endl << " >> ";
	  cin >> option;

	  switch (option)
	    {
	      case 1 : pushElement(); break;
	      case 2 : popElement(); break;
	      case 3 : isEmpty(); break;
		  case 4 : makeEmpty();
	    }
	}
     while (option != 0);
  }



Is This A Good Question/Topic? 0
  • +

Replies To: stack using linked list

#2 bodom658   User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: stack using linked list

Posted 30 March 2010 - 12:13 PM

Anything that was dynamically allocated must be deleted before your program exits. I would make sure you have delete statements in any function that removes an element, and a general 'delete list' subroutine at the end of your main function. Normally, it is good to have everything within a class, and create the list as an object.
Was This Post Helpful? 0
  • +
  • -

#3 RLB31384   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 97
  • Joined: 29-January 10

Re: stack using linked list

Posted 30 March 2010 - 03:38 PM

I put it in a class, and modified the code just a little. Does anyone have any suggestions? Also, I changed my main to menu and added a new main. I am going to have the new main ask the user if its wants to use stack or queue. I need to use 4 files for the 2 classes, stack and queue and 1 for main right? Anyways anyone got any suggestions for my code? I'm trying to learn as much as possible so even if its more advanced things or concepts, I wouldn't mind learning about them. Thanks

#include <iostream>

using namespace std;

class Stack
{
private:
	
	struct node
	{  
		int data;
		node *nxt;// Pointer to next node
	}
	*start_ptr;
	int option;

public:

	Stack();
	void pushElement();
	void popElement();
	void display_list();
	void isEmpty();
	void makeEmpty();
	void menu();
};

Stack::Stack()
{
	node *start_ptr = NULL;
	int option = 0;
}

void Stack::pushElement()
  {  node *temp;   // Temporary pointer

     // Reserve space for new node and fill it with data
     temp = new node;
     cout << "Please enter the data to store in stack: ";
     cin >> temp->data;
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
       }
     else
       { temp->nxt = start_ptr;
         // We know this is not NULL - list not empty!
         
                start_ptr = temp;

       }
  }

void Stack::popElement()
  {  
	  node *temp;   // Temporary pointer

     // Set up link to this node
     if (start_ptr == NULL)
         { 
                   cout << "Can't pop element, when stack is empty." << endl;
     }
     else
     { 
         // We know this is not NULL - list not empty!
         
                 temp = start_ptr;
                 start_ptr = start_ptr->nxt;
                 delete temp;
      }
  }


void Stack::display_list()
  {  node *temp;
     temp = start_ptr;
     cout << endl;
     if (temp == NULL)
       cout << "The stack is empty!" << endl;
     else
                 cout << "Displaying stack: (top to bottom)" << endl;
       { while (temp != NULL)
           {  // Display details for what temp points to
              cout << temp->data << endl;
              temp = temp->nxt;

           }
         cout << "End of list!" << endl;
       }
  }

void Stack::isEmpty()
{ 
        if (start_ptr == NULL)
        {
                cout << "The stack is empty.";
        }
        else 
        {
                cout << "The stack is not empty.";
        }

}

void Stack::makeEmpty()
{
        while( start_ptr != NULL)
        {
                popElement();
        }
}

void Stack::menu()
  {  start_ptr = NULL;
     do
        {
          display_list();
          cout << endl;
          cout << "Please select an option : " << endl;
          cout << "0. Exit the program." << endl;
          cout << "1. Push element on top of stack:" << endl;
          cout << "2. Pop element off top of stack:" << endl;
          cout << "3. Check if stack is empty: " << endl;
          cout << "4. Make stack empty: " << endl;
      cout << endl << " >> ";
          cin >> option;

          switch (option)
            {
              case 1 : pushElement(); break;
              case 2 : popElement(); break;
              case 3 : isEmpty(); break;
                  case 4 : makeEmpty();
            }
        }
     while (option != 0);
	 makeEmpty();
  }

void main()
{
	Stack llStack;
	llStack.menu();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1