stack1.cpp

来自「串及矩阵的压缩存储应用,二叉树的构造与遍历,栈的测试和应用,线性表的链存储操作实」· C++ 代码 · 共 73 行

CPP
73 行
字号
#include "stdio.h"
#include "string.h"
#include "stdlib.h"

#define OVERFLOW    0
#define OK          1
#define ERROR       0

typedef int ElemType;
typedef struct LNode 
{
	ElemType data;
	struct LNode *next;
}LNode,* LinkList;

int Init_LinkList(LinkList &L)
{
	L=(LinkList)malloc(sizeof(LNode));
	if(!L)
		return OVERFLOW;
	L->next=0;
	return OK;
}

void Push(LinkList L,ElemType e)
{
	if(L->next==0)
	{
		LinkList p;
		p=(LinkList)malloc(sizeof(LNode));
		p->data=e;
		p->next=0;
		L->next=p;
	}
	else 
	{
		LinkList p;
		p=(LinkList)malloc(sizeof(LNode));
		p->data=e;
		p->next=L->next;
		L->next=p;
	}
}

int Pop(LinkList L,ElemType &e)
{
	LinkList p;
	if(L->next==0)
		return ERROR;
	e=L->next->data;
	p=L->next;
	L->next=L->next->next;
	free(p);
	return OK;
}

void main()
{
	int i=1,e;
	LinkList L;
	Init_LinkList(L);
	for(i;i<6;i++)
		Push(L,i);
	for(i=0;i<5;i++)
	{
		Pop(L,e);
		printf("%4d",e);
	}
	printf("\n");
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?