#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Node {
int data;
struct Node* pNext;
};
struct stack {
struct Node*pTop;
struct Node* pBottom;
};
bool init_stack(struct stack*ps) {
ps->pTop = (struct Node*)malloc(sizeof(struct Node));
if (ps->pTop == NULL) {
printf("创建失败");
return false;
exit(-1);
}
else {
ps->pBottom = ps->pTop;
ps->pBottom->pNext = NULL;
return true;
}
}
bool is_empty(struct stack*ps) {
if (ps->pBottom == ps->pTop) {
printf("栈为空");
return true;
}
return false;
}
void push_stack(struct stack* ps,int val) {
struct Node* pnew = (struct Node*)malloc(sizeof(struct Node));
pnew->data = val;
pnew->pNext = ps->pTop;
ps->pTop = pnew;
return;
}
bool pop_stack(struct stack* ps, int* val) {
if (is_empty(ps)) {
printf("栈为空,出栈失败");
}
else {
struct Node* r = ps->pTop;
*val = r->data;
ps->pTop = ps->pTop->pNext;
free(r);
r = NULL;
}
return true;
}
void display(struct stack* ps) {
struct Node* p = ps->pTop;
while (p != ps->pBottom) {
printf("%d", p->data);
p = p->pNext;
}
return;
}
void clear_stack(struct stack*ps) {
if (is_empty(ps)) {
printf("本来就是空的不用清");
}
else {
struct Node* p = NULL;
struct Node* q = NULL;
p = ps->pTop;
while (q != NULL) {
q = p->pNext;
free(p);
p = q;
}
ps->pTop = ps->pBottom;
}
return;
}
int main() {
int val;
struct stack s;
init_stack(&s);
push_stack(&s, 4);
push_stack(&s, 5);
push_stack(&s, 6);
push_stack(&s, 7);
push_stack(&s, 8);
display(&s);
printf("\n");
printf("出栈后的变化:");
pop_stack(&s, &val);
display(&s);
printf("\n");
printf("出栈的元素是:%d", val);
printf("\n");
printf("清空后:");
clear_stack(&s);
display(&s);
return 0;
}
//代码2
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef struct Node
{
int data;
struct Node * pNext;
}NODE, * PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;
}STACK, * PSTACK; //PSTACK 等价于 struct STACK *
void init(PSTACK);
void push(PSTACK, int );
void traverse(PSTACK);
bool pop(PSTACK, int *);
void clear(PSTACK pS);
int main(void)
{
STACK S; //STACK 等价于 struct Stack
int val;
init(&S); //目的是造出一个空栈
push(&S, 1); //压栈
push(&S, 2);
push(&S, 3);
push(&S, 4);
push(&S, 5);
push(&S, 6);
traverse(&S); //遍历输出
clear(&S);
//traverse(&S); //遍历输出
if ( pop(&S, &val) )
{
printf("出栈成功,出栈的元素是%d\n", val);
}
else
{
printf("出栈失败!\n");
}
traverse(&S); //遍历输出
return 0;
}
void init(PSTACK pS)
{
pS->pTop = (PNODE)malloc(sizeof(NODE));
if (NULL == pS->pTop)
{
printf("动态内存分配失败!\n");
exit(-1);
}
else
{
pS->pBottom = pS->pTop;
pS->pTop->pNext = NULL; //pS->Bottom->pNext = NULL;
}
}
void push(PSTACK pS, int val)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->data = val;
pNew->pNext = pS->pTop; //pS->Top不能改成pS->Bottom
pS->pTop = pNew;
return;
}
void traverse(PSTACK pS)
{
PNODE p = pS->pTop;
while (p != pS->pBottom)
{
printf("%d ", p->data);
p = p->pNext;
}
printf("\n");
return;
}
bool empty(PSTACK pS)
{
if (pS->pTop == pS->pBottom)
return true;
else
return false;
}
//把pS所指向的栈出栈一次,并把出栈的元素存入pVal形参所指向的变量中,如果出栈失败,返回false,否则返回true
bool pop(PSTACK pS, int * pVal)
{
if ( empty(pS) ) //pS本身存放的就是S的地址
{
return false;
}
else
{
PNODE r = pS->pTop;
*pVal = r->data;
pS->pTop = r->pNext;
free(r);
r = NULL;
return true;
}
}
//clear清空
void clear(PSTACK pS)
{
if (empty(pS))
{
return;
}
else
{
PNODE p = pS->pTop;
PNODE q = NULL;
while (p != pS->pBottom)
{
q = p->pNext;
free(p);
p = q;
}
pS->pTop = pS->pBottom;
}
}
栈的创建——
最新推荐文章于 2023-03-02 23:21:51 发布