青岛大学 数据结构 链栈 c++实现

#include<iostream>
using namespace std;

typedef struct StackNode{
	int data;
	struct StackNode *next;
}StackNode,*StackList;
//头指针就是栈顶,不需要头结点,无溢出,头指针为空则为空栈
StackList initial(){
	StackList s = new StackNode;
	s = NULL;
	return s;
}
int push(StackList &s,int x){
	StackList p= new StackNode;
	p->data = x;
	p->next = s;
	s=p;
	return 1;
}
int pop(StackList &s,int &x){
	if(s == NULL){
		return -1;	
	}
	x=s->data;
	StackList p = s;
	s = s->next;
	delete p;
	p = NULL;
	return 1;
}
int destroy(StackList &s){
	StackList p = s;
	while(p){
		s=s->next;
		delete p;
		p = NULL;
		p = s;
	}
	return 1;
}
int main(){
	StackList s = initial();
	for(int i =0;i<5;i++){
	push(s,i);		
	}
	int x;
	pop(s,x);
	while(s){
		cout<<s->data;
		s = s->next;
	}
	destroy(s);
	return 1;
}

 

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页