• 数据结构题型10-链栈


    //参考博客:https://blog.csdn.net/weixin_37716512/article/details/104068102
    #include   //引入头文件
    using namespace std;
    
    typedef int Elemtype;
    
    #define Maxsize 10
    #define ERROR 0
    #define OK    1
    
    typedef struct Linknode
    {
    	Elemtype data;   // 数据域
    	struct Linknode *next;   //指针域
    }Linknode,* LinkStack;
    
    
    void InitStack(LinkStack& L)
    {
    	L = (LinkStack)malloc(sizeof(Linknode));
    	L->next = NULL;
    }
    
    bool StackEmpty(LinkStack L)
    {
    	if (L->next == NULL)   //堆空
    	{
    		cout<<"栈为空"<<endl;
    		return ERROR;
    	}
    	else              //不空
    	{
    		cout << "栈不为空" << endl;
    		return OK;
    	}
    }
    
    bool Push(LinkStack &L, Elemtype e)
    {
    	Linknode *s = (LinkStack)malloc(sizeof(Linknode));
    	s->data = e;
    	s->next = L->next;
    	L->next = s;
    	return OK;
    }
    
    bool Pop(LinkStack& L, Elemtype &e)
    {
    	if (L->next == NULL)
    	{
    		cout << "栈为空" << endl;
    		return ERROR;
    	}
    	Linknode* p = L->next;
    	e = p->data;
    	L->next = p->next;
    	cout << "出栈元素值为: " << e << endl;
    	free(p);
    	return OK;
    }
    
    bool GetTop(LinkStack& L, Elemtype& e)
    {
    	if (L->next == NULL)
    	{
    		cout << "栈为空" << endl;
    		return ERROR;
    	}
    	e = L->next->data;
    	cout << "栈顶元素值为: " << e << endl;
    	return OK;
    }
    
    void PrintStack(LinkStack L)
    {
    	Linknode* p= L->next;
    	while (p != NULL)
    	{
    		cout << "|______" << p->data << "______|" << endl;
    		p = p->next;
    	}
    }
    int main(void)
    {
    	LinkStack S = NULL;
    	InitStack(S);
    	StackEmpty(S);
    	Push(S, 1);
    	Push(S, 2);
    	Push(S, 3);
    	Push(S, 4);
    	StackEmpty(S);
    	PrintStack(S);
    	int e = 0, x = 0;
    	Pop(S, e);
    	PrintStack(S);
    	GetTop(S, x);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98

    在这里插入图片描述

  • 相关阅读:
    Django command执行脚本
    AcWing 258. 石头剪子布
    【MATLAB的方程组求解】
    web大作业:基于html+css+javascript+jquery实现智能分控网站
    C语言:链表
    通过linux定时任务删除es日志索引
    2023年9月电子学会Python等级考试试卷(一级)答案解析
    Java毕业设计-疫情防控系统
    【机器学习】七、降维与度量学习
    【LIN总线测试】——LIN主节点调度表测试
  • 原文地址:https://blog.csdn.net/qq_41735476/article/details/133269605