/*----------------------------------------------------------------------*/
/*                           LStack.cpp                                 */
/* Authored By : H.P.Kasmaei                                            */
/* Last Update : November 10, 2007                                      */
/* www.hpkclasses.ir                                                    */
/*----------------------------------------------------------------------*/
#include <iostream.h>
#include <stdlib.h>


typedef int ItemType;


typedef struct Node
{
ItemType Info;
Node * Next;
};

typedef Node * NodePtr;

NodePtr Top;

int Count;

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*                           ClearStack                                 */
/* Task:   Clear out all nodes on the Stack.                            */
/*	   The list is thus set to an empty list.			            */
/* Given:  Nothing 							            */
/* Return: Nothing    					                        */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

void ClearStack(void)
   {
   NodePtr Current, Temp;

   Current = Top;
   while (Current != NULL)
      {
      Temp = Current;
      Current = Current->Next;
      delete Temp;
      }

   Top = NULL;
   Count = 0;
   }

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*                           ShowTop                                    */
/* Task:   Return data on top of the stack                              */
/* Given:  Nothing 							            */
/* Return: item    data on top of the stack                             */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

ItemType ShowTop(void)
   {
      Return (Top->Info);
   }

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*                           GetNode                                    */
/* Task:   To allocate a new node and place Item and NextPtr in it.     */
/* Given:  Item: A data item to place in the Info field of a new node.  */
/*	   NextPtr: The pointer to place in the Next field of the new     */
/*	   node							                  */
/* Return: A pointer to the new node is returned in the function name.	*/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

NodePtr GetNode(const ItemType & Item, NodePtr NextPtr)
   {
   NodePtr Current;

   Current = new Node;

   if (Current == NULL)
      {
      cerr << "Memory allocation error!" << endl;
      exit(1);
      }
   Current->Next = NextPtr;
   Current->Info = Item;

   return Current;
   }

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*                           CreateStack                                */
/* Task:   Create an empty stack.                        		      */
/* Given:  Nothing 							            */
/* Return: Nothing    					                        */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

void CreateStack(void)
   {
   ItemType Item;

   Top = NULL;
   Count = 0;
   }

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*                           IsEmpty                                    */
/* Task:   To check if stack is empty.                                  */
/* Given:  Nothing 							            */
/* Return: 0 if stack is empty, 1 if is not                             */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

int IsEmpty(void)
   {
   if (Top==NULL)
      return(1);
   else
      return(0);
   }

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*                           Push                                       */
/* Task:   To insert a new node containing Item at the Top of the stack */
/* Given:  Item   A data item       				            */
/* Return: Nothing							            */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

void Push(const ItemType & Item)
   {
   NodePtr Current;

   Current = GetNode(Item, Top);
   Top = Current;

   Count++;
   }

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*                           Pop                                        */
/* Task:   To remove top pf the stack                                   */
/* Given:  Nothing                  				            */
/* Return: Data item that removed from the stack			      */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

void Pop(ItemType & Item)
   {
   NodePtr Current;

   if (IsEmpty())
      {
      cerr << "ERROR: there is no item to remove in the list!" << endl;
      exit(1);
      }

   Current = Top;
   Item = Current->Info;
   Top = Current->Next;
   Count--;

   delete Current;
   }

/*----------------------------------------------------------------------*/
/*                              M A I N                                 */
/*----------------------------------------------------------------------*/

void main ()
{

   ItemType Item;
   NodePtr p;


   CreateStack();

   Push(20);
   Push(15);
   Push(10);
   Pop(Item);
   cout<<endl<<Item<< " was removed from stack";
   Push(30);

   ClearStack();

   delete Top;
}
