栈-2-链栈的基本操作

#include 
#include 
using namespace std; 

typedef struct LNode{
     
	int data;
	struct LNode *next;
}LNode;

void initStack(LNode *&st){
     
	st = (LNode*)malloc(sizeof(LNode));
	st->next = NULL;
}

int isEmpty(LNode *st){
     
	if(st->next == NULL)
		return 1;
	return 0;
}

void push(LNode *&st,int x){
     
	LNode *s;
	s = (LNode*)malloc(sizeof(LNode));
	s->data = x;
	s->next = NULL;
	
	s->next = st->next;
	st->next = s;
}

int pop(LNode *&st,int &e){
     
	LNode *r;
	if(st->next == NULL)
		return 0;
	
	r = st->next;
	st->next = r->next;
	e = r->data;
	free(r);
	return 1;	
}

int main(int argc, char** argv) {
     	
	LNode *st,*r;
	
	initStack(st);
	cout<<"is empty:"<<isEmpty(st)<<endl;
	
	push(st,3);
	push(st,6);
	
	r = st;
	while(r->next!=NULL){
     
		cout<<r->next->data<<"...";
		r = r->next;
	}
		
	

	return 0;
}

你可能感兴趣的:(栈-2-链栈的基本操作)