链栈的基本操作

来源:互联网 发布:国际贸易统计数据库 编辑:程序博客网 时间:2024/04/25 06:53

链栈的基本操作实现代码如下:

#include<iostream>

using namespace std;

#define TRUE 1

#define FALSE 0


//链栈的结构

typedef struct node

{

int data;

struct node *next;

}LinkStackNode;

typedef LinkStackNode *LinkStack;


//链栈进栈

int Push(LinkStack top, int x)//将数据元素x压入栈top中

{

LinkStackNode *temp;

temp = (LinkStackNode *)malloc(sizeof(LinkStackNode));

if (temp==NULL)//申请空间失败

{

return FALSE;

}

temp->data= x;

temp->next = top->next;

top->next = temp;//修改当前栈顶指针

return TRUE;

}


//链栈出栈

int Pop(LinkStack top, int *x)//将栈top的栈顶元素弹出,放到x所指的存储空间中

{

LinkStackNode *temp;

temp = top->next;

if (temp == NULL)//栈为空

{

return FALSE;

}

top->next = temp->next;

*x=temp->data ;

free(temp);//释放存储空间

return TRUE;

}


本文出自 “岩枭” 博客,请务必保留此出处http://yaoyaolx.blog.51cto.com/10732111/1772833

0 0
原创粉丝点击