您的位置:首页 > 其它

用单链表实现栈 Linked List implementation of stacks

2014-03-13 23:01 579 查看
struct Node
{
int data;
Node* next;
};

Node* top = NULL;

void Push(int x)
{
Node* tmp = (Node*)malloc(sizeof(Node));
tmp->data = x;
tmp->next = top;
top = tmp;
}
void Pop()
{
if(top == NULL)
return;

Node* tmp = top;
top = top->next;
delete tmp;
}

int Top()
{
if(top!=NULL)
return top->data;
}

void PrintStack()
{
if(top == NULL)
return;
Node* tmp = top;
while(tmp != NULL)
{
cout << tmp->data << " ";
tmp = tmp->next;
}
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: