链式堆栈--代码实现

用链表来实现堆栈,用的是头插法,先进后出。
1、把一个元素压入堆栈是通过在链表头部添加一个元素实现。先新建一个指针指向new_node要添加进来的元素,然后它要指向之前的第一个元素,并且指向第一个节点的指针要指向它
void push (int data)
2、弹出一个元素是通过删除链表头部第一个元素实现。我们要保证指向第一个元素的指针永远指向在第一个元素这个位置上的元素:head = first_node->next;,这个first_node是我们即将弹出的那个元素。void pop(void)
3、打印堆栈:新建一个指针指向头部第一个元素,然后判断,如果这个元素不为空,则打印这个元素的data,然后新建的那个指针指向下一个元素,直到这个指针指向的元素为空,则打印完成。

//定义一个结构以存储堆栈元素
#include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <malloc.h>
  5 
  6 #define THIS
  7 
  8 typedef struct node_s
  9 {
 10         int  data;
 11         struct node_s *next;
 12 }stack;
 13 //指向堆栈中第一个节点的指针
 14 static stack *head;
 15 
 16 void push(int data);
 17 void pop(void);
 18 int empty(void);
 19 void print(void);
 20 void destroty_stack(void);
 21 
 22 void push(int data)
 23 {
 24         stack *new_node;
 25         new_node = (stack *)malloc(sizeof(stack));                          
 26         if(new_node == NULL)
 27                 perror("malloc fail");
 28         new_node->data = data;
 29         new_node->next = head;
 30         head = new_node;
 31 }
 32 
 33 void pop()
 34 {
 35                 stack *first_node;
 36                 first_node = head;
 37                 head = first_node->next;
 38                 free(first_node);
 39 
 40 
 41 }
	 int empty()
 45 {
 46 /*      if(head == NULL)
 47         {
 48                 return 0;
 49         }else
 50                 return 1;
 51 */
 52         return head ==NULL;
 53 }
 54 
 55 void print(void)
 56 {       
 57         stack *p_node;
 58         p_node = head;
 59         printf("打印出链式堆栈里面的值:");
 60         if(p_node == NULL)
 61                 printf("堆栈为空\n");
 62         while(p_node != NULL)
 63         {
 64                 printf("%d",p_node->data);
 65                 p_node = p_node->next;
 66         }
 67         printf("\n");
 68 }
 69 
 70 void destroy_stack()
 71 {
 72         while(!empty())
 73         pop();
 74 }
 75 
 76 int main()
 77 {
 78 
 79 #ifdef THIS
 80         char ch[1];
 81         char sh[1];
 int num = 1;
 83         int a,b;
 84         printf("你想新建一个多大的堆栈:");
 85         scanf("%s\n",ch);
 86         a = atoi(ch);
 87         for(num; num <= a; num++)
 88         {
 89                 char A[1];
 90                 printf("压入的第%d个数据:" ,num);
 91                 scanf("%s\n",A);
 92                 int c;
 93                 c = atoi(A);
 94                 push(c);
 95         }
 96         printf("你新建好的堆栈:");
 97         print();
 98 
 99         printf("你想弹出几个数据:");
100         scanf("%s\n",sh);
101         b = atoi(sh);
102         for(num; num<=b; num++);
103         {
104                 pop();
105         }
106         printf("打印剩下的数据:");
107         print();
108         destroy_stack();
109 #else
110         print();
111         push(10);push(9);push(8);
112         push(7);push(6);
113         printf("push压入数值后:\n");
114         print();
115         pop();
116         pop();
117         printf("pop几个出去之后的数值:\n");
118         print();
119         destroy_stack();
120 #endif
121 return 1 ;
122 }


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
堆栈是一种先进后出(Last-In-First-Out,LIFO)的数据结构链式存储是堆栈的一种实现方式。链式存储的堆栈不需要预先指定堆栈的长度,可以动态地分配内存,具有灵活性和可扩展性。 链式存储的堆栈可以使用单向链表或双向链表实现。以单向链表为例,堆栈的顶端称为栈顶,堆栈的底部称为栈底。 堆栈的基本操作包括入栈(Push)、出栈(Pop)、获取栈顶元素(Top)和判断堆栈是否为空(IsEmpty)。 下面是堆栈链式存储的实现代码: ```C++ #include <iostream> using namespace std; // 定义堆栈结点构体 struct StackNode { int data; // 数据域 StackNode *next; // 指针域,指向下一个结点 }; // 定义堆栈 class Stack { private: StackNode *top; // 栈顶指针 public: Stack() { top = NULL; } // 构造函数,初始化栈顶指针为空 ~Stack(); // 析构函数,释放内存 bool IsEmpty(); // 判断堆栈是否为空 void Push(int x); // 入栈 int Pop(); // 出栈 int Top(); // 获取栈顶元素 }; // 判断堆栈是否为空 bool Stack::IsEmpty() { return top == NULL; } // 入栈 void Stack::Push(int x) { StackNode *newNode = new StackNode; // 创建新结点 newNode->data = x; // 设置数据域 newNode->next = top; // 将新结点插入到栈顶 top = newNode; // 更新栈顶指针 } // 出栈 int Stack::Pop() { if (IsEmpty()) { // 判断是否为空栈 cout << "Error: Stack is empty!" << endl; return -1; } else { StackNode *temp = top; // 保存栈顶结点 int x = temp->data; // 获取栈顶元素 top = top->next; // 更新栈顶指针 delete temp; // 释放内存 return x; } } // 获取栈顶元素 int Stack::Top() { if (IsEmpty()) { // 判断是否为空栈 cout << "Error: Stack is empty!" << endl; return -1; } else { return top->data; // 返回栈顶元素 } } // 析构函数,释放内存 Stack::~Stack() { while (!IsEmpty()) { // 循环弹出栈中所有元素 Pop(); } } // 测试堆栈链式存储的实现 int main() { Stack s; // 创建堆栈对象 // 入栈操作 s.Push(1); s.Push(2); s.Push(3); // 获取栈顶元素 cout << "Top element: " << s.Top() << endl; // 出栈操作 cout << "Pop element: " << s.Pop() << endl; cout << "Pop element: " << s.Pop() << endl; // 获取栈顶元素 cout << "Top element: " << s.Top() << endl; // 出栈操作 cout << "Pop element: " << s.Pop() << endl; // 判断堆栈是否为空 if (s.IsEmpty()) { cout << "Stack is empty!" << endl; } else { cout << "Stack is not empty!" << endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值