1 // zhan.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 using namespace std; 7 typedef struct stacknode 8 { 9 int data; 10 struct stacknode *next; 11 }stacknode,*LinkStack; 12 13 //判断栈为空 14 int StackEmpty(LinkStack &top) 15 { 16 if(top ->next == NULL) 17 return 1; 18 else 19 return 0; 20 } 21 22 //入栈函数 23 LinkStack push(LinkStack &top,int value) 24 { 25 LinkStack p = new stacknode; 26 if(p != NULL) 27 { 28 p ->data = value;//可以理解为在链表尾部插入一个节点。 29 p ->next = top ->next; 30 top ->next = p; 31 } 32 else 33 cout << "没有内存可分配" << endl; 34 return top; 35 } 36 37 //出栈函数 38 int pop(LinkStack &top) 39 { 40 LinkStack temp = new stacknode; 41 int data; 42 if(StackEmpty(top)) 43 cout << "该栈为空!" << endl; 44 else 45 { 46 temp = top ->next;//可以理解为删除一个节点 47 data = temp ->data; 48 top ->next = temp ->next; 49 delete(temp); 50 } 51 return data; 52 } 53 54 //打印函数
C++ 栈的基本操作
最新推荐文章于 2023-12-08 18:02:14 发布