1、栈基础知识
(1)栈的特征:先进后出;
(2)栈的常用操作:
· push(E):增加一个元素E
· pop(E):弹出一个元素E
· peek(E):显示栈顶元素,但不出栈
· empty(E):判断栈是否为空
(3)Java中的栈:
//package com.xz.stack;这句代码跟import java.util.Stack;不能同时出现。
//import java.util.*;这种写法是把util这个包下的全部类导入到程序中。
//import java.util.Stack;这种写法是只把util包下的Stack类导入到程序中。
//在现实工作中一般都采“importjava.util.Scanner;”这种写法,因为在程序中把没用到的包导入的话只会影响程序执行的效率。
import java.util.Stack;
public class MainTest{
public static void main(String[] args){
Stack<Integer> stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("栈顶元素为:" +stack.peek());
while(!stack.empty()){
System.out.println(stack.peek());
System.out.println(stack.pop());
}
}
}
(4)python中的栈
stack = []
stack.append(1)
stack.append(2)
stack.append(3)
#暂且真的不理解为什么要这么写
print("栈顶元素为:" + str(stack[-1]))
while stack:
#只显示没出栈
print(stack[-1])
#出栈并且显示
print(stack.pop())
(5)对于C语言,没有直接的栈结构,需要自己构造或者引入第三方的包。后面会来实现。
2、基于数组实现栈
思想:采用顺序表实现的栈,内部以数组为基础,实现对元素的存取操作。在应用前要注意栈的容量是否够用,如果不够用,可以经行扩容。
补充:top指针有的地方指向栈顶元素,有的指向栈顶再往上的一个空单位,这里需要根据题目设计。
Java(含测试代码):
import java.util.Arrays;
import java.util.Scanner;
class Mystack<T>{
//实现栈的数组
private Object[] stack;
//栈顶元素
private int top;
Mystack(){
//初始容量为10
stack = new Object[10];
}
//判断是否为空
public boolean isEmpty(){ //我到底看看这个isEmpty是怎么写的
return top == 0;
}
//返回栈顶元素
public T peek(){
T t = null;
if(top > 0)
t = (T) stack[top - 1];
return t;
}
//入栈
public void push(T t){
expandCapacity(top + 1);
stack[top] = t;
top ++;
}
//出栈
public T pop(){
T t = peek();
if(top > 0){
stack[top - 1] = null;
top --;
}
return t;
}
//扩大容量
public void expandCapacity(int size){
int len = stack.length;
if(size > len){
size = size * 3 / 2; // 每次扩大50%
stack = Arrays.copyOf(stack, size);
}
}
}
//下面是测试代码
//写main函数必须要有这两句话
public class Main{
public static void main(String[] args){
Mystack<String> stack = new Mystack<>();
System.out.println(stack.peek());
System.out.println(stack.isEmpty());
stack.push("java");
stack.push("is");
stack.push("beautiful");
stack.push("language");
System.out.println(stack.pop());
System.out.println(stack.isEmpty());
System.out.println(stack.peek());
}
}
Python(本地编辑器测试代码):
class Stack(object):
#基于数组实现栈
def __init__(self):
self.items = []
# 判断是否为空
def is_empty(self):
return self.items == []
#加入元素
def push(self, item):
self.items.append(item)
# 弹出元素
def pop(self):
return self.items.pop()
#返回栈顶元素
def peek(self):
return self.items[len(self.items) - 1]
# 返回栈的大小
def size(self):
return len(self.items)
#以下是测试代码
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.peek())
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(stack.pop())
C语言(本地编辑器测试代码):
#include<bits/stdc++.h>
using namespace std;
//定义栈结构体
typedef struct{
int* items;
int top;
int capacity;
} Stack;
//创建栈、初始化栈
Stack* create_stack(int capacity){
Stack* stack = (Stack* )malloc(sizeof(Stack));
stack->items = (int* )malloc(sizeof(int)* capacity);
stack->top = -1;
stack->capacity = capacity;
return stack;
}
//判断栈是否为空,但下面这种写法我不是很能理解
/*bool StackEmpty(Stack* stack){
return stack->top == 0;
}*/
//判断栈是否为空
bool StackEmpty(Stack* stack){
assert(stack); //防止写代码的人传过来的是空指针
if(stack->top == 0){
return true;
}
else{
return false;
}
}
//判断栈是否满了
bool StackFull(Stack* stack){
return stack->top == stack->capacity - 1;
}
//入栈
void push(Stack* stack, int item){
if(StackFull(stack)){
printf("栈已满,无法入栈\n");
return;
}
//本程序一开始stack->top就等于-1,++ stack->top就等于0,所以本程序top指针指向的就是栈顶元素
stack->items[++ stack->top] = item;
}
//出栈
int pop(Stack* stack){
if(StackEmpty(stack)){
printf("栈已空,无法出栈\n");
return -1;
}
//写法很巧妙,先执行return stack->items[stack->top],再执行stack-top --
return stack->items[stack->top --];
}
//返回栈顶元素
int peek(Stack* stack){
if(StackEmpty(stack)){
printf("栈已空,无法出栈\n");
return -1;
}
return stack->items[stack->top];
}
//返回栈的大小
int size(Stack* stack){
return stack->top + 1;
}
//以下是测试代码
int main(){
//创建一个容量为50的栈
Stack* stack = create_stack(50);
//入栈
push(stack, 1);
push(stack, 2);
push(stack, 3);
printf("peek: %d\n", peek(stack));
//printf("pop: %d\n", pop(stack));
printf("pop: %d\n", pop(stack));
printf("size: %d\n", size(stack));
system("pause");
return 0;
}
C语言的另外一种写法(含本地编辑器测试代码):
#include<bits/stdc++.h>
using namespace std;
typedef struct{
int data[100];
int top;
}MyStack;
void initStack(MyStack *stack){
stack->top = - 1;
}
void push(MyStack *stack, int x){
if(stack->top >= 100 - 1){
printf("Stack overflow\n");
}
stack->data[++ stack->top] = x;
}
int pop(MyStack *stack){
if(stack->top < 0){
printf("Stack underflow\n");
return -1;
}
return stack->data[stack->top --];
}
int top(MyStack *stack){
if(stack->top < 0){
printf("Stack is empty\n");
return -1;
}
return stack->data[stack->top];
}
bool empty(MyStack *stack){
return stack->top < 0;
}
int main(){
MyStack stack;
initStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
printf("%d\n", pop(&stack));
printf("%d\n", pop(&stack));
printf("%d\n", empty(&stack));
system("pause");
return 0;
}
3、基于链表实现栈
前面介绍过没有虚拟结点时对链表表头元素进行插入和删除的操作方法,与这里的基于链表实现栈时完全一致的。
插入与删除都在表头经行。
Java(含测试代码):
import java.util.Stack;
import java.util.Scanner;
import java.util.LinkedList;
class ListStack<T>{
//定义链表
class Node<T>{
public T val;
public Node next;
}
//定义头指针
public Node<T> head;
//构造函数初始化头指针
ListStack(){
head = null;
}
//入栈,头插法,哈哈哈,我跟以前一样想了n久
public void push(T t){
if(t == null){
throw new NullPointerException("参数不能为空");
}
if(head == null){
head = new Node<T>();
head.val = t;
head.next = null;
}
else{
Node<T> temp = head;
head = new Node<>();
head.val = t;
head.next = temp;
}
}
//出栈
public T pop(){
if(head == null){
return null;
}
T val = head.val;
head = head.next;
return val;
}
//取栈顶元素
public T peek(){
if(head == null){
return null;
}
T val = head.val;
return val;
}
//判断是否为栈空
public boolean isEmpty(){
if(head == null){
return true;
}
else{
return false;
}
}
}
//以下是测试代码
public class Main{
public static void main(String[] args){
ListStack stack = new ListStack();
System.out.println(stack.isEmpty());
stack.push("Java");
stack.push("is");
stack.push("beautiful");
stack.push("language");
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.isEmpty());
}
}
C语言(本地编辑器测试代码):
#include<bits/stdc++.h>
using namespace std;
struct Node{
int val = 0;
struct Node* next = NULL;
};
struct Stack{
struct Node* top; //从这个地方已经用到了上面定义的链表了
};
//插入元素
void push(struct Stack* stack, int val){
struct Node* newNode = (struct Node*)malloc((sizeof(struct Node)));
newNode->val = val;
newNode->next = stack->top;
stack->top = newNode;
}
int pop(struct Stack* stack){
if(stack->top == NULL){
printf("栈已空\n");
return -1;
}
int val = stack->top->val;
struct Node* temp = stack->top;
stack->top = stack->top->next;
free(temp);
return val;
}
int peek(struct Stack* stack){
if(stack->top == NULL){
printf("栈已空\n");
return -1;
}
return stack->top->val;
}
int empty(struct Stack* stack){
return stack->top == NULL;
}
//以下是测试代码
int main(){
Stack* stack = (Stack* )malloc((sizeof(Stack)));
//入栈
push(stack, 1);
push(stack, 2);
push(stack, 3);
printf("peek: %d\n", peek(stack));
//printf("pop: %d\n", pop(stack));
printf("pop: %d\n", pop(stack));
system("pause");
return 0;
}
Python本身不擅长链表,Python更合适数组的方式,就不提供代码了。链表属于最简单的数据结构,Python也可以实现,不过比较少有场景是python现有数据结构不能满足而必须要上链表的。
本篇文章为原创,欢迎转载,请注明文章出处链接: https://blog.csdn.net/2301_79084755/article/details/136411475。技术类文章一般都有时效性,本人会不定期对自己的博客进行修正更新,因此请访问出处以查看本文的最新版本。