一个栈类的实现(链栈)

typedef struct LNode
{
 int data;
 struct LNode *next;
}LNode, *pLinkList;

//类的声明

class LinkList
{
 private:
        pLinkList m_pList;
        int m_listLength;
public:
        LinkList();
        ~LinkList();
        bool InitList ();
        bool DestroyList ();
        bool ClearList();
        bool IsEmpty ();
        int GetLength ();
        bool GetNode(int position, LNode** node);
        int LocateElem(int elem);
        bool SetNodeData(int position, int newData);
        bool GetNodeData(int position, int &data);
        bool InsertNode(int beforeWhich, int data);
        bool DeleteNode(int position);
};

//类的实现

 

Stack::Stack():top(NULL)
{
//
}
Stack::Stack(const Stack& a_stack)
{
 if (a_stack.top == NULL)
 {
  top =NULL;
 }
 else
 {
  StackNodePtr temp = a_stack.top;
  StackNodePtr end;
  end= new StackNode;
  end->data = temp->data;
  top =end;
  //第一各节点已经创建,而且填充了数据
  //现在,新的节点要在这个节点之后添加
  temp=temp->link;
  while(temp!=NULL)
  {
   end->link = new StackNode;
   end=end->link;
   end->data =temp->data;
   temp =temp->link;
  }
  end->link =NULL;

 }
}

Stack::~Stack()
{
  char next;
  while (top!=NULL)
    next =pop();
}

BOOL Stack::isempty() const
{
 return (top==NULL);
}

void Stack::push(char the_symbol)
{
 StackNodePtr temNode = new StackNode;
  temNode->data = the_symbol;
  temNode->link = top;
  top= temNode;
}
char Stack::pop()
{

 if (top==NULL)
  return 0;

  char pResult = top->data;
  StackNodePtr temNodePtr = top;
  top=top->link;//top指针向下移动一位
  delete temNodePtr; //释放那个节点的空间
 
}

 

你可能感兴趣的:(实现)