所谓链栈,就是用链表存储结构实现的栈。采用链栈,可以不事先估计栈的最大容量,只要系统有足够的空间,链栈就不会溢出,在使用完后,应与链表一样,给予相应的内存释放。
typedef struct node
{
int data;
struct node * next;
}Node;
Node * InitStack()
{
Node * Head = (Node *)malloc(sizeof(Node));
Head->data = 0;
Head->next = NULL;
return Head;
}
int Push(Node * top, int x)
{
Node * temp;
temp = (Node *)malloc(sizeof(Node));
if (temp == NULL)return(false);
temp->data = x;
temp->next = top->next;
top->next = temp;
return(true);
}
int Pop(Node * top, int *x)
{
Node * temp;
temp = top->next;
if (temp == NULL)return(false);
top->next = temp->next;
*x = temp->data;
free(temp);
return(true);
}
好了,这就是链栈。
若有错误,欢迎指正批评,欢迎讨论。 每文一句:人生是洁白的画纸,我们每个人就是手握各色笔的画师;人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者;人生还像是一块神奇的土地,我们每个人则是手握农具的耕耘者;但人生更像一本难懂的书,我们每个人则是孜孜不倦的读书郎。