堆栈的基本操作&&链式存储

public class Node {
    String name;
    Node next;
    public Node() {
        name=null;
        next=null;
    }
    public Node(String name) {
        this(name,null);
    }
    public Node(String name,Node next ) {
        this.name=name;
        this.next=next;
    }
}

public class MyStackDome1 {
    Node head;

    public MyStackDome1() {
        head=new Node();
    }
    //新增
    public void push(String data) {
        Node h=head;
        Node p=new Node(data);
        p.next=h.next;
        h.next=p;

    }
    //删除元素
    public String pop() {
        //判断堆栈是否为空
        if(isEmpty()) {
            System.out.println("堆栈已经为空无法删除");
            return null;
        }
        Node p=head;
        Node temp=p.next;
        p.next=p.next.next;


        return temp.name;
    }
    //判断堆栈是否为空
    public boolean isEmpty() {

        if(head.next==null) {
            return true;
        }
        else
            return false;
    }
    //返回元素个数
    public int size() {
        int num=0;
        Node p=head;
        while(p.next!=null) {
            num++;
            p=p.next;
        }
        return num;
    }
    //查找
    public void find(String data) {
        Node p=head;
        while(p.next!=null) {
            if(p.next.name.equals(data)) {
                System.out.println("此信息存在");
                break;
            }
            else {
                p=p.next;
            }
        }
    }
    //获取栈顶元素
    public String peek() {
        if(isEmpty()) {
            System.out.println("堆栈已经为空无法删除");
            return null;
        }
        return head.next.name;
    }
        public static void main(String[] args) {
            MyStackDome1 stack=new MyStackDome1();
            stack.push("组长");
            stack.push("部门经理");
            stack.push("副总经理");
            stack.push("总经理了");
            stack.push("董事长");
            System.out.println(stack.peek());
            System.out.println(stack.size());

            stack.pop();
            System.out.println(stack.size());
        }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
堆栈是一种后进先出(Last-In-First-Out,LIFO)的数据结构,可以使用链式存储实现。链式存储堆栈由一个链表和一个指向链表顶部的指针组成。 链表节点(Node)通常包含两个成员:数据成员和指向下一个节点的指针成员。堆栈的顶部是链表的头部,堆栈的底部是链表的尾部。 以下是链式存储堆栈的实现过程: 1. 定义节点结构体 ```c typedef struct Node { int data; // 数据成员 struct Node* next; // 指向下一个节点的指针成员 } Node; ``` 2. 定义堆栈结构体 ```c typedef struct Stack { Node* top; // 指向堆栈顶部节点的指针成员 int size; // 堆栈大小 } Stack; ``` 3. 创建堆栈 ```c Stack* createStack() { Stack* stack = (Stack*)malloc(sizeof(Stack)); // 分配堆栈空间 stack->top = NULL; // 初始化堆栈顶部指针为空 stack->size = 0; // 初始化堆栈大小为0 return stack; } ``` 4. 入栈操作 ```c void push(Stack* stack, int data) { Node* node = (Node*)malloc(sizeof(Node)); // 分配节点空间 node->data = data; // 设置节点数据成员 node->next = stack->top; // 将节点插入堆栈顶部 stack->top = node; // 更新堆栈顶部指针 stack->size++; // 更新堆栈大小 } ``` 5. 出栈操作 ```c int pop(Stack* stack) { if (stack->top == NULL) { // 如果堆栈为空,返回 0 return 0; } Node* node = stack->top; // 获取堆栈顶部节点 int data = node->data; // 获取节点数据成员 stack->top = node->next; // 更新堆栈顶部指针 stack->size--; // 更新堆栈大小 free(node); // 释放节点空间 return data; } ``` 6. 获取堆栈顶部元素 ```c int top(Stack* stack) { if (stack->top == NULL) { // 如果堆栈为空,返回 0 return 0; } return stack->top->data; // 返回堆栈顶部节点数据成员 } ``` 7. 判断堆栈是否为空 ```c int isEmpty(Stack* stack) { return stack->top == NULL; // 如果堆栈顶部指针为空,返回 1;否则返回 0 } ``` 8. 获取堆栈大小 ```c int size(Stack* stack) { return stack->size; // 返回堆栈大小 } ``` 9. 销毁堆栈 ```c void destroyStack(Stack* stack) { while (stack->top != NULL) { // 循环弹出堆栈元素 pop(stack); } free(stack); // 释放堆栈空间 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值