五种编程语言解释数据结构与算法—链式栈

五种编程语言解释数据结构与算法—链式栈

1、栈的链式存储结构介绍

1.1、逻辑结构示意图

栈的链式存储结构与单链表相似,并且所有的操作都在表头进行

对于链式栈的操作与单链表基本一致,就不再一一叙述了。

2、栈的应用

2.1、括号匹配问题

2.2、表达式求值问题

2.2.1、三缀表达式介绍

2.2.2、中缀表达式转换成前缀表达式

将运算符提前

2.2.3、中缀表达式转换成后缀表达式

将运算符置后

算法思想

3、栈和递归的关系

因为递归算法有着许多问题,因此如果要将递归算法转化成非递归算法,往往需要借助栈来解决

4、C语言实现链式栈

4.1、LinkStack.h文件的内容

#ifndef XGP_STUDY_DEMO49_LINKSTACK_H
#define XGP_STUDY_DEMO49_LINKSTACK_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//链式栈的结点
typedef struct LINKNODE {
    struct LINKNODE* next;
}LinkNode;

//链式栈
typedef struct LINKSTACK {
    LinkNode head;
    int size;
}LinkStack;

//1,初始化函数
LinkStack* Init_LinkStack();
//2,入栈
void Push_LinkStack(LinkStack* stack,LinkNode* data);
//3,出栈
LinkNode* Pop_LinkStack(LinkStack* stack);
//4,返回栈顶元素
LinkNode* Top_LinkStack(LinkStack* stack);
//5,返回栈元素的个数
int Size_LinkStack(LinkStack* stack);
//6,清空栈
void Clear_LinkStack(LinkStack* stack);
//7,销毁栈
void FreeSpace_LinkStack(LinkStack* stack);

#endif //XGP_STUDY_DEMO49_LINKSTACK_H

4.2、LinkStack.c文件中的内容

#include "LinkStack.h"

//1,初始化函数
LinkStack* Init_LinkStack() {
    LinkStack* stack = (LinkStack*)malloc(sizeof(LinkStack));
    stack->head.next = NULL;
    stack->size = 0;
    return stack;
}
//2,入栈
void Push_LinkStack(LinkStack* stack,LinkNode* data) {
    if(stack == NULL || data == NULL) return;

    data->next = stack->head.next;
    stack->head.next = data;
    stack->size++;
}
//3,出栈
LinkNode* Pop_LinkStack(LinkStack* stack) {
    if(stack == NULL) return  NULL;
    if(stack->size == 0) return NULL;

    LinkNode* save = stack->head.next;
    stack->head.next = save->next;
    save->next = NULL;

    stack->size--;
    return save;
}
//4,返回栈顶元素
LinkNode* Top_LinkStack(LinkStack* stack) {
    if(stack == NULL) return  NULL;
    if(stack->size == 0) return NULL;

    return stack->head.next;
}
//5,返回栈元素的个数
int Size_LinkStack(LinkStack* stack) {
    if(stack == NULL) return  -1;
    return stack->size;
}
//6,清空栈
void Clear_LinkStack(LinkStack* stack) {
    if(stack == NULL) return;

    stack->head.next = NULL;
    stack->size = 0;

}
//7,销毁栈
void FreeSpace_LinkStack(LinkStack* stack) {
    if(stack == NULL) return;

    free(stack);
}

4.3、main.c文件中的内容

#include "LinkStack.h"

typedef struct PERSON {
    LinkNode node;
    char name[64];
    int age;
}Person;

int main() {
    LinkStack* stack = Init_LinkStack();

    Person p1,p2,p3,p4,p5;
    strcpy(p1.name,"aaa");
    strcpy(p2.name,"bbb");
    strcpy(p3.name,"ccc");
    strcpy(p4.name,"ddd");
    strcpy(p5.name,"eee");

    p1.age = 18;
    p2.age = 19;
    p3.age = 20;
    p4.age = 21;
    p5.age = 22;

    Push_LinkStack(stack,(LinkNode*)&p1);
    Push_LinkStack(stack,(LinkNode*)&p2);
    Push_LinkStack(stack,(LinkNode*)&p3);
    Push_LinkStack(stack,(LinkNode*)&p4);
    Push_LinkStack(stack,(LinkNode*)&p5);

    while (Size_LinkStack(stack) > 0) {
        //取出栈顶元素
        Person* p = (Person*)Pop_LinkStack(stack);
        printf("Name:%s\tAge:%d\n",p->name,p->age);
    }

    FreeSpace_LinkStack(stack);

    return 0;
}

4.4、输出结果

Name:eee        Age:22
Name:ddd        Age:21
Name:ccc        Age:20
Name:bbb        Age:19
Name:aaa        Age:18

Process finished with exit code 0

5、C++语言来实现链式栈

5.1、LinkStack.h文件中的内容

#ifndef XGP_STUDY_DEMO51_LINKSTACK_H
#define XGP_STUDY_DEMO51_LINKSTACK_H

#include <iostream>
using namespace std;

class LinkNode {
public:
    LinkNode* next;
};

class LinkStack {
public:
    LinkNode head;
    int size;

    //1,初始化函数
    LinkStack* Init_LinkStack();
//2,入栈
    void Push_LinkStack(LinkNode* data);
//3,出栈
    LinkNode* Pop_LinkStack();
//4,返回栈顶元素
    LinkNode* Top_LinkStack();
//5,返回栈元素的个数
    int Size_LinkStack();
//6,清空栈
    void Clear_LinkStack();
//7,销毁栈
    void FreeSpace_LinkStack();
};


#endif //XGP_STUDY_DEMO51_LINKSTACK_H

5.2、LinkStack.cpp文件中的内容

#include "LinkStack.h"

LinkStack *LinkStack::Init_LinkStack() {
    this->head.next = NULL;
    this->size = 0;
    return this;
}

void LinkStack::Push_LinkStack(LinkNode *data) {
    if(this == NULL || data == NULL) return;

    data->next = this->head.next;
    this->head.next = data;
    this->size++;
}

LinkNode *LinkStack::Pop_LinkStack() {
    if(this == NULL) return  NULL;
    if(this->size == 0) return NULL;

    LinkNode* save = this->head.next;
    this->head.next = save->next;
    save->next = NULL;

    this->size--;
    return save;
}

LinkNode *LinkStack::Top_LinkStack() {
    if(this == NULL) return  NULL;
    if(this->size == 0) return NULL;

    return this->head.next;
}

int LinkStack::Size_LinkStack() {
    if(this == NULL) return  -1;
    return this->size;
}

void LinkStack::Clear_LinkStack() {
    if(this == NULL) return;

    this->head.next = NULL;
    this->size = 0;
}

void LinkStack::FreeSpace_LinkStack() {
    if(this == NULL) return;

    delete(this);
}

5.3、main.cpp文件中的内容

#include <ostream>
#include "LinkStack.h"

class Person {
public:
    LinkNode node;
    string name;
    int age;

    Person(const string &name, int age) : name(name), age(age) {}

    friend ostream &operator<<(ostream &os, const Person &person) {
        os << "name: " << person.name << " age: " << person.age;
        return os;
    }
};

int main() {

    LinkStack* stack = new LinkStack();

    Person p1("aaa",18);
    Person p2("bbb",19);
    Person p3("ccc",20);
    Person p4("ddd",21);
    Person p5("eee",22);


    stack->Push_LinkStack((LinkNode*)&p1);
    stack->Push_LinkStack((LinkNode*)&p2);
    stack->Push_LinkStack((LinkNode*)&p3);
    stack->Push_LinkStack((LinkNode*)&p4);
    stack->Push_LinkStack((LinkNode*)&p5);

    while (stack->Size_LinkStack() > 0) {
        //取出栈顶元素
        Person* p = (Person*)stack->Pop_LinkStack();
        cout<<*p<<endl;
    }

    stack->FreeSpace_LinkStack();

    return 0;
}

5.4、输出结果

name: eee age: 22
name: ddd age: 21
name: ccc age: 20
name: bbb age: 19
name: aaa age: 18

Process finished with exit code 0

6、java语言来实现链式栈

6.1、LinkStack.java文件中的内容

package com.xgp.栈的链式存储;

public interface LinkStack {
    //1,初始化函数
    LinkStack Init_LinkStack();
    //2,入栈
    void Push_LinkStack(LinkNode data);
//3,出栈
    LinkNode Pop_LinkStack();
//4,返回栈顶元素
    LinkNode Top_LinkStack();
    //5,返回栈元素的个数
    int Size_LinkStack();
    //6,清空栈
    void Clear_LinkStack();
    //7,销毁栈
    void FreeSpace_LinkStack();
}

6.2、LinkStackImpl.java文件中的内容

package com.xgp.栈的链式存储;

public class LinkStackImpl implements LinkStack {
    public LinkNode head = new LinkNode();
    public int size;

    @Override
    public LinkStack Init_LinkStack() {
        this.head.next = null;
        this.size = 0;
        return this;
    }

    @Override
    public void Push_LinkStack(LinkNode data) {
        if(data == null) return;

        data.next = this.head.next;
        this.head.next = data;
        this.size++;
    }

    @Override
    public LinkNode Pop_LinkStack() {
        if(this.size == 0) return null;

        LinkNode save = this.head.next;
        this.head.next = save.next;
        save.next = null;

        this.size--;
        return save;
    }

    @Override
    public LinkNode Top_LinkStack() {
        if(this.size == 0) return null;

        return this.head.next;
    }

    @Override
    public int Size_LinkStack() {
        return this.size;
    }

    @Override
    public void Clear_LinkStack() {
        this.head.next = null;
        this.size = 0;
    }

    @Override
    public void FreeSpace_LinkStack() {
        Clear_LinkStack();
        this.head = null;
        System.gc();
    }
}

class LinkNode {
    LinkNode next;
}

6.3、Main.java文件中的内容

package com.xgp.栈的链式存储;

public class Main {
    public static void main(String[] args) {
        LinkStack stack = new LinkStackImpl();

        Person p1 = new Person("aaa",18);
        Person p2 = new Person("bbb",19);
        Person p3 = new Person("ccc",20);
        Person p4 = new Person("ddd",21);
        Person p5 = new Person("eee",22);

        stack.Push_LinkStack(p1);
        stack.Push_LinkStack(p2);
        stack.Push_LinkStack(p3);
        stack.Push_LinkStack(p4);
        stack.Push_LinkStack(p5);

        while (stack.Size_LinkStack() > 0) {
            //取出栈顶元素
            Person p = (Person)stack.Pop_LinkStack();
            System.out.println(p);
        }

        stack.FreeSpace_LinkStack();
    }
}

class Person extends LinkNode {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

6.4、输出结果

Person{name='eee', age=22}
Person{name='ddd', age=21}
Person{name='ccc', age=20}
Person{name='bbb', age=19}
Person{name='aaa', age=18}

进程完成,退出码 0

7、JavaScript语言来实现链式栈

7.1、LinkStack.js文件中的内容

class LinkNode {

}

class LinkStack {
    Init_LinkStack() {
        this.head = new LinkNode();
        this.head.next = null;
        this.size = 0;
        return this;
    }

    Push_LinkStack(data) {
        if(this == null || data == null) return;

        data.next = this.head.next;
        this.head.next = data;
        this.size++;
    }

    Pop_LinkStack() {
        if(this == null || this.size == 0) return null;

        var save = this.head.next;
        this.head.next = save.next;
        save.next = null;

        this.size--;
        return save;
    }

    Top_LinkStack() {
        if(this == null || this.size == 0) return null;

        return this.head.next;
    }

    Size_LinkStack() {
        if(this == null) return -1;
        return this.size;
    }

    Clear_LinkStack() {
        if(this == null) return;
        this.head.next = null;
        this.size = 0;
    }

    FreeSpace_LinkStack() {
        if(this == null) return;
        this.Clear_LinkStack();
        this.head = null;
    }
}

7.2、LinkStack.html文件中的内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="LinkStack.js"></script>
</head>
<body>
    <script>
        class Person {
            constructor(name,age) {
                this.name = name;
                this.age = age;
            }

            print() {
                console.log("Name:" + this.name + "\t" + "Age:" + this.age);
            }
        }

        var stack = new LinkStack();
        stack.Init_LinkStack();

        var p1 = new Person("aaa",18);
        var p2 = new Person("bbb",19);
        var p3 = new Person("ccc",20);
        var p4 = new Person("ddd",21);
        var p5 = new Person("eee",22);

        stack.Push_LinkStack(p1);
        stack.Push_LinkStack(p2);
        stack.Push_LinkStack(p3);
        stack.Push_LinkStack(p4);
        stack.Push_LinkStack(p5);


        while (stack.Size_LinkStack() > 0)
            stack.Pop_LinkStack().print();

        stack.FreeSpace_LinkStack();
    </script>
</body>
</html>

7.3、输出结果

Name:eee	Age:22 LinkStack.html:18:25
Name:ddd	Age:21 LinkStack.html:18:25
Name:ccc	Age:20 LinkStack.html:18:25
Name:bbb	Age:19 LinkStack.html:18:25
Name:aaa	Age:18 LinkStack.html:18:25

8、Python语言来实现链式栈

8.1、LinkStack.py文件中的内容

class LinkNode:
    pass

class LinkStack:

    def Init_LinkStack(self):
        self.head = LinkNode()
        self.head.next = None
        self.size = 0
        return self


    def Push_LinkStack(self,data):
        if(self == None or data == None):
            return
        data.next = self.head.next
        self.head.next = data
        self.size += 1

    def Pop_LinkStack(self):
        if(self == None or self.size == 0):
            return None
        save = self.head.next
        self.head.next = save.next
        save.next = None
        self.size -= 1
        return save

    def Top_LinkStack(self):
        if(self == None or self.size == 0):
            return None
        return self.head.next

    def Size_LinkStack(self):
        if(self == None):
            return -1
        return self.size

    def Clear_LinkStack(self):
        if(self == None):
            return
        self.head.next = None
        self.size = 0

    def FreeSpace_LinkStack(self):
        if(self == None):
            return
        self.Clear_LinkStack()
        self.head = None

8.2、main.py文件中的内容

from LinkStack import *

class Person:

    def __init__(self,name,age) -> None:
        self.name = name
        self.age = age

    def __str__(self) -> str:
        return "Name:" + self.name + "\t" + "Age:" + str(self.age)

stack = LinkStack()
stack.Init_LinkStack()

p1 = Person("aaa", 18)
p2 = Person("bbb", 19)
p3 = Person("ccc", 20)
p4 = Person("ddd", 21)
p5 = Person("eee", 22)

stack.Push_LinkStack(p1)
stack.Push_LinkStack(p2)
stack.Push_LinkStack(p3)
stack.Push_LinkStack(p4)
stack.Push_LinkStack(p5)

while(stack.Size_LinkStack() > 0):
    print(stack.Pop_LinkStack())

stack.FreeSpace_LinkStack()

8.3、输出结果

Name:eee	Age:22
Name:ddd	Age:21
Name:ccc	Age:20
Name:bbb	Age:19
Name:aaa	Age:18

Process finished with exit code 0
posted @ 2020-03-06 16:42  孤傲的咸鱼  阅读(441)  评论(0编辑  收藏  举报