myPathStack::myPathStack()
{
head = (PPATHNODE)malloc(sizeof(PATHNODE));
memset(head,0,sizeof(PATHNODE));
head->next = NULL;
}
myPathStack::~myPathStack()
{
free(head);
head = NULL;
}
PPATHNODE myPathStack::Push(HTREEITEM item)
{
if(item == head->hItem)
return NULL;
PPATHNODE temp = NULL;
temp = (PPATHNODE)malloc(sizeof(PATHNODE));
if(temp == NULL)
return NULL;
temp->hItem = item;
temp->next = head->next;
head->next = temp;
head->hItem = temp->hItem;//指向栈顶
return temp;
}
PPATHNODE myPathStack::Pop()
{
if(head->next == NULL)
return NULL;
PPATHNODE temp;
temp = head->next;
head->next = temp->next;
head->hItem = temp->hItem;
free(temp);
return head; //返回栈顶
}
{
head = (PPATHNODE)malloc(sizeof(PATHNODE));
memset(head,0,sizeof(PATHNODE));
head->next = NULL;
}
myPathStack::~myPathStack()
{
free(head);
head = NULL;
}
PPATHNODE myPathStack::Push(HTREEITEM item)
{
if(item == head->hItem)
return NULL;
PPATHNODE temp = NULL;
temp = (PPATHNODE)malloc(sizeof(PATHNODE));
if(temp == NULL)
return NULL;
temp->hItem = item;
temp->next = head->next;
head->next = temp;
head->hItem = temp->hItem;//指向栈顶
return temp;
}
PPATHNODE myPathStack::Pop()
{
if(head->next == NULL)
return NULL;
PPATHNODE temp;
temp = head->next;
head->next = temp->next;
head->hItem = temp->hItem;
free(temp);
return head; //返回栈顶
}
本文详细介绍了如何使用C++实现栈的基本操作,包括初始化、入栈、出栈和释放内存。
3万+

被折叠的 条评论
为什么被折叠?



