#include "TreeNode.h"
#ifndef _LINKSTACK_H
#define _LINKSTACK_H
class LinkNode{
public:
TreeNode* treenode;
LinkNode* next;
LinkNode(TreeNode* thenode){
treenode=thenode;
next=NULL;
}
};
class LinkStack{
public:
LinkNode* head;
LinkStack(){ head=NULL; }
~LinkStack(){
deletenode(head);
}
void deletenode(LinkNode *n){
if(n==NULL) return;
deletenode(n->next);
delete n;
}
BOOL isEmpty() { return (head==NULL); }
void push(TreeNode* thenode){
LinkNode* newnode = new LinkNode(thenode);
if(isEmpty()){
head=newnode;
}
else{
newnode->next=head;
head=newnode;
}
}
UINT NumOfNodes(){
LinkNode* curr;
curr=head;
UINT temp;
temp=0;
while(curr!=NULL){
temp++;
curr=curr->next;
}
return temp;
}
LinkNode* getNode(int nodeno){
int i;
LinkNode* curr;
curr=head;
i=0;
while(i<nodeno){
curr=curr->next;
i++;
}
return curr;
}
LinkNode* pop(){
LinkNode* temp;
temp=head;
head=head->next;
return temp;
}
void reset(){
if(head==NULL) return;
deletenode(head);
head=NULL;
}
};
#endif
No comments:
Post a Comment