c语言堆栈实现,堆栈的实现(c语言)

#include #define EmptyTOS (-1) //数组堆栈需要

#define MinStackSize (5) //数组堆栈需要

//堆栈函数的声明

struct Node;

typedef struct Node *PtrToNode;

typedef PtrToNode Stack;

typedef int ElementType;

bool IsEmpty(Stack S); //检测堆栈是否为空

Stack CreateStack(); //创建堆栈

void DisposeStack(Stack); //毁灭堆栈

void Push(ElementType X, Stack S); //元素进栈

ElementType Pop(Stack S); //元素出栈

void MakeEmpty(Stack S); //清空堆栈中的元素

//ElementType Pop(Stack S); //元素出栈 (数组实现)

bool IsFull(Stack S); //检测堆栈是否已满 (数组实现需要)

void printStack(Stack S); //输出堆栈中的元素

struct Node { //链表结构

ElementType Element;

PtrToNode Next;

};

//struct Node { //数组结构

//int Capacity; //容量

//int TopOfStack: //栈顶

//ElementType *Array;

//};

int main()

{

int number, flag;

ElementType X;

Stack S;

flag = 1;

printf("| 堆栈的基本使用 |\n");

printf("|************************************|\n");

printf("| 1. 创建堆栈 |\n");

printf("| 2. 销毁堆栈 |\n");

printf("| 3. 入栈 |\n");

printf("| 4. 出栈 |\n");

printf("| 5. 打印堆栈元素 |\n");

printf("|************************************|\n");

while (flag) {

printf("请选择功能 : \n");

scanf("%d", &number);

switch(number) {

case 1 :

S = CreateStack();

break;

case 2 :

if (S) {

DisposeStack(S);

} else

printf("堆栈不存在!!!\n");

break;

case 3 :

if (S) {

printf("请输入需要插入的元素:");

scanf("%d", &X);

Push(X, S);

} else

printf("堆栈不存在!!!\n");

break;

case 4 :

if (S) {

Pop(S);

} else

printf("堆栈不存在!!!\n");

break;

case 5 :

if (S) {

printf("堆栈中元素如下:");

printStack(S);

} else

printf("堆栈不存在!!!\n");

break;

default :

printf("程序运行结束,请按任意键退出!!!\n");

flag = 0;

}

}

return 0;

}

//函数定义

bool IsEmpty(Stack S) //检测堆栈是否为空 (链表)

{

return S->Next == NULL;

}

//bool IsEmpty(Stack S) //检测堆栈是否为空 (数组)

//{

//return S->TopOfStack == EmptyTOS;

//}

Stack CreateStack() //创建堆栈 (链表)

{

Stack S;

if (!(S = malloc(sizeof(struct Node))))

exit(-1);

S->Next = NULL;

return S;

}

// Stack CreateStack(int MaxElements) //创建堆栈 (数组)

//{

//Stack S;

//

//if (MaxElements < MinStackSize) {

//printf("堆栈空间太小了!!");

//return -1;

//}

//

//if (!(S = malloc(sizeof(struct Node))))

//exit(-1);

//if (!(S->Array = malloc(sizeof((ElementType) * MaxStackSize))))

//exit(-1);

//S->Capacity = MaxStackSize;

//

//return S;

// }

void DisposeStack(Stack S) //毁灭堆栈 (链表)

{

PtrToNode P, TmpCell;

P = S->Next;

S->Next = NULL;

while (P) {

TmpCell = P->Next;

free(P);

P = TmpCell;

}

free(S);

}

// void DisposeStack(Stack S) //毁灭堆栈 (数组)

// {

// if (S != NULL) {

// free(S->Array);

// free(S);

// }

// }

void Push(ElementType X, Stack S) //元素进栈 (链表)

{

PtrToNode TmpCell;

if (!(TmpCell = malloc(sizeof(struct Node))))

exit(-1);

else {

TmpCell->Element = X;

TmpCell->Next = NULL;

}

TmpCell->Next = S->Next;

S->Next = TmpCell;

}

//void Push(ElementType X, Stack S) //元素进栈 (数组)

//{

//if (!IsFull(S))

//S->Array[++TopOfStack] = X;

//else

//printf("堆栈已满!!\n");

//}

ElementType Pop(Stack S) //元素出栈 (链表)

{

Stack TmpCell;

ElementType X;

if (IsEmpty(S)) {

printf("堆栈为空!!!\n");

return -1;

}

else {

TmpCell = S->Next;

S->Next = TmpCell->Next;

X = TmpCell->Element;

free(TmpCell);

return X;

}

}

//ElementType Pop(Stack S) //元素出栈 (数组)

//{

//if (!IsEmpty(S))

//return S->Array[TopOfStack--];

//else

//printf("堆栈为空,出栈异常!!!\n");

//}

void MakeEmpty(Stack S) //清空堆栈中的元素 (链表)

{

PtrToNode P, TmpCell;

P = S->Next;

S->Next = NULL;

while (P) {

TmpCell = P->Next;

free(P);

P = TmpCell;

}

}

//void MakeEmpty(Stack S) //清空堆栈中的元素 (数组)

//{

//S->TopOfStack == EmptyTOS;

//}

//bool IsFull(Stack S) //检测堆栈是否已满 (数组实现需要)

//{

//return S->(++TopOfStack) == S->Capacity;

//}

//

void printStack(Stack S) //打印堆栈中元素

{

ElementType P;

while (!IsEmpty(S)) {

P = Pop(S);

printf("%d ", P);

}

printf("\n");

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用堆栈实现前序遍历的c语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAXSIZE 50 typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; typedef struct Stack { TreeNode* data[MAXSIZE]; int top; } Stack; void push(Stack* s, TreeNode* node) { if (s->top == MAXSIZE - 1) { printf("Error: Stack is full.\n"); return; } s->top++; s->data[s->top] = node; } TreeNode* pop(Stack* s) { if (s->top == -1) { printf("Error: Stack is empty.\n"); return NULL; } TreeNode* node = s->data[s->top]; s->top--; return node; } void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } Stack* s = (Stack*)malloc(sizeof(Stack)); s->top = -1; push(s, root); while (s->top != -1) { TreeNode* node = pop(s); printf("%d ", node->data); if (node->right != NULL) { push(s, node->right); } if (node->left != NULL) { push(s, node->left); } } free(s); } int main() { TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->data = 1; root->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->data = 2; root->left->left = NULL; root->left->right = NULL; root->right = (TreeNode*)malloc(sizeof(TreeNode)); root->right->data = 3; root->right->left = (TreeNode*)malloc(sizeof(TreeNode)); root->right->left->data = 4; root->right->left->left = NULL; root->right->left->right = NULL; root->right->right = (TreeNode*)malloc(sizeof(TreeNode)); root->right->right->data = 5; root->right->right->left = NULL; root->right->right->right = NULL; printf("Preorder Traversal: "); preorderTraversal(root); printf("\n"); return 0; } ``` 在这个代码中,我们定义了一个`Stack`结构体和一个`TreeNode`结构体。`Stack`用于实现堆栈,其中包含一个数组和一个指向堆栈顶部的指针。`TreeNode`用于表示二叉树的节点,其中包含一个整数数据和左右子节点的指针。 我们定义了几个函数来操作堆栈,包括`push`和`pop`函数。`push`函数用于将节点压入堆栈,`pop`函数用于将节点弹出堆栈。 `preorderTraversal`函数实现了前序遍历。我们首先创建一个堆栈,并将根节点压入堆栈。接着,我们进入一个循环,每次从堆栈中弹出一个节点并打印它的数据。然后,我们将其右子节点压入堆栈,再将其左子节点压入堆栈。这样,下一次循环时,我们会先访问左子节点,然后访问右子节点。 最后,我们在`main`函数中创建一个二叉树并调用`preorderTraversal`函数来进行前序遍历。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值