栈的操作:push pop。
栈的表头依旧不放任何数据。表头即是栈顶,push,pop都在此处进行!
栈的单元组成
typedef struct node
{
int element;
node*next;
}node;创建一个栈CreateStack(void)
{
node* S
S=malloc(sizeof(node));
if (s==NULL)printf("out of space!!!");
*S.next=NULL;
return S;
}判断一个栈是否为空isempty( node*S)//输入表头的指针
{
return *S.next==NULL;//如果是空栈返回1,不是返回0
}进栈操作(push)void push(int x,node*s)//sΪ±íÍ·µÄÖ¸Õë
{
node*temp;
temp=malloc(sizeof(node));
if(temp==NULL)printf("out of space!!!");
*temp.element=x;
*temp.next=*s.next;
*s.next=temp;
}返回栈顶元素
top(node* s)
{
if (isempty(s)) {printf("empty stack!!!");return 0;}
else return *(*s.next).element;
}出栈操作(pop)
pop(node*s)
{
node* temp;
if(isempty(s))printf("empty stack!!!");
else
{
temp=*s.next;
*s.next=*temp.next;
free(temp);
}
} 空栈操作MakeEmpty(node*s)
{
while(*s.next!=NULL)
pop(s);
}
本文介绍了一种使用C语言实现栈的基本方法,包括创建空栈、判断栈是否为空、元素入栈(push)、获取栈顶元素(top)、元素出栈(pop)及清空栈等核心功能。通过具体的代码示例展示了如何利用结构体来组织栈的数据结构。
》学习笔记③栈&spm=1001.2101.3001.5002&articleId=80274221&d=1&t=3&u=10bfbbea1beb4a5fa09b488f1d8ba0d3)
1万+

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



