#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node{
int date;
struct Node* next;
}NODE,*PNODE;
typedef struct strack{
PNODE ptop;
PNODE pbottom;
}STACK,*PSTACK;
void init(PSTACK s){
s->ptop=(PNODE)malloc(sizeof(NODE));
if(s->ptop==NULL){
printf("分配失败");
exit(-1);
}
else{
s->pbottom=s->ptop;
s->pbottom->next=NULL;
}
}
void push(PSTACK s,int x){
PNODE pNew=(PNODE)malloc(sizeof(NODE));
pNew->date=x;
pNew->next=s->ptop;
s->ptop=pNew;
}
void pop(PSTACK s){
s->ptop=s->ptop->next;
}
void traverse(PSTACK s){
PNODE p=s->ptop;
while(p!=s->pbottom){
printf("%d ",p->date);
p=p->next;
}
printf("\n");
return;
// for(PSTACK i=s->ptop;i!=NULL;i=i->ptop){
// cout<<i->date;
}
bool empty(PSTACK s){
if(s->pbottom==s->ptop) return true;
else return false;
}
void clear(PSTACK s){
//s->pbottom=s->ptop;
PNODE p,q;
p=s->ptop,q=NULL;
while(p!=s->pbottom){
q=p->next;
free(p);
p=q;
}
s->ptop=s->pbottom;
}
int main(){
STACK s;
init(&s);
push(&s,3);
push(&s,77);
push(&s,55);
pop(&s);
traverse(&s);
clear(&s);
traverse(&s);
return 0;
}
栈的插入,删除,遍历操作
最新推荐文章于 2022-05-14 13:03:44 发布