[data structure] stack - stack chain (dynamic diagram, c + +, java)

Posted by Sindarin on Tue, 18 Jan 2022 11:01:49 +0100

The following is the main body of this article, and the following cases can be used for reference.

Stack chain overview (illustration)

The stack can be stored in sequence or chain, which are called sequence stack and chain stack respectively.

Sequential stack allocates a continuous space and requires two pointers. base points to the bottom of the stack and top points to the top of the stack.

The address of each node of the chain stack is discontinuous, and only one pointer at the top of the stack is required.

As can be seen from the figure, each node of the chain stack contains two fields: data field and pointer field.

Basic operation of chain stack

The chain stack can be regarded as a single chain list without leading nodes, but it can only be inserted and deleted in the head. Value taking and other operations cannot be operated in the middle and tail. Therefore, the nodes of the chain stack can be defined according to the method of single chain list.

First, define a structure (internal class), which contains a data field and a pointer field.

The c + + code is as follows (example):

typedef struct SNode{
    int data;//Data domain
    SNode *next;//Pointer field
}*LinkStack;

The java code is as follows (example):

public class SNode{
    int data;
    SNode next;
}

1. Initialization

Initializing an empty chain stack does not require a header node, so you only need to make the pointer at the top of the stack empty.

The c + + code is as follows (example):

void Init(LinkStack &S){
    S = NULL;
}

The java code is as follows (example):

F(){//Initialize directly with the constructor. If you're tired, destroy it
    s = null;
}

2. Stack

Stacking is to push the new element node into the top of the stack. Because the first node in the chain stack is the top of the stack, insert a new element in front of the first node, and then modify the stack top pointer to point to the new node.

The c + + code is as follows (example):

void Push(LinkStack &S,int e){
    LinkStack p = new SNode;
    p->data = e;
    p->next = S;//The next address of the new element is the top of the old stack
    S = p;//Move stack top up
}

The java code is as follows (example):

public void push(int e){
    SNode p = new SNode();
    p.data = e;
    p.next = s;
    s = p;
}

3. Out of stack

To get out of the stack is to delete the top element of the stack, make the top pointer point to the next node, and then release the node space (c + +).

The c + + code is as follows (example):

bool Pop(LinkStack &S,int &e){
    if(S == NULL){//Stack empty
        return false;
    }
    LinkStack p = S;//Temporary node
    S = p->next;//Stack top down
    e = p->data;//Record stack elements
    delete p;//Free up space
    return true;
}

The java code is as follows (example):

public int pop(){
    if(s == null){
        return -1;
    }
    SNode p = s;
    s = s.next;
    return p.data;
}

4. Take stack top

Taking the top of the stack is different from taking out of the stack.

Take the stack top element, just copy the stack top element, the stack top pointer does not move, and the number of elements in the stack does not change.

The c + + code is as follows (example):

int GetTop(LinkStack S){
    if(S == NULL){
        return -1;
    }
    return S->data;
}

The java code is as follows (example):

public int getTop(){
    if(s == null){
        return -1;
    }
    return s.data;
}

9. Complete code

The c + + code is as follows (example):

#include<iostream>
using namespace std;

typedef struct SNode{
    int data;
    SNode *next;
}*LinkStack;

void Init(LinkStack &S){
    S = NULL;
}

void Push(LinkStack &S,int e){
    LinkStack p = new SNode;
    p->data = e;
    p->next = S;
    S = p;
}

bool Pop(LinkStack &S,int &e){
    if(S == NULL){
        return false;
    }
    LinkStack p = S;
    S = p->next;
    e = p->data;
    delete p;
    return true;
}

int GetTop(LinkStack S){
    if(S == NULL){
        return -1;
    }
    return S->data;
}

int main(){
    LinkStack S;
    int n,e;
    Init(S);
    cout<<"Chain stack initialization succeeded"<<endl;
    cout<<"Enter the number of elements:"<<endl;
    cin>>n;
    cout<<"Input elements and put them on the stack in turn"<<endl;
    while(n--){
        cin>>e;
        Push(S,e);
    }
    cout<<"Elements out of the stack in turn!"<<endl;
    while(S!=NULL){
        cout<<GetTop(S)<<" ";
        Pop(S,e);
    }
    cout<<endl;
}

The java code is as follows (example):

public class F {
    private SNode s;
    public class SNode{
        int data;
        SNode next;
    }

    F(){
        s = null;
    }

    public void push(int e){
        SNode p = new SNode();
        p.data = e;
        p.next = s;
        s = p;
    }

    public int pop(){
        if(s == null){
            return -1;
        }
        SNode p = s;
        s = s.next;
        return p.data;
    }

    public int getTop(){
        if(s == null){
            return -1;
        }
        return s.data;
    }

    public static void main(String[] args) {
        F f = new F();
        System.out.println("Chain stack initialization succeeded!");
        f.push(5);
        f.push(4);
        f.push(3);
        f.push(2);
        f.push(1);
        System.out.println("Created successfully");
        System.out.println("Elements are stacked in turn");
        while(f.s!=null){
            System.out.print(f.getTop()+" ");
            f.pop();
        }
        System.out.println();
    }
}

summary

  1. The basic operations of sequence stack and chain stack only need constant time, so they are indistinguishable in time efficiency.
  2. In terms of space efficiency, the sequential stack needs to allocate a fixed length of space in advance, which may cause space waste or overflow
  3. The chain stack allocates only one node at a time. Unless there is no memory, there will be no overflow, but each node needs a pointer field, which increases the structural overhead.

Therefore, if the number of elements changes greatly, chain stack can be used; Conversely, sequential stacks can be used.

In practical application, sequence stack is more widely used than chain stack.

Next notice: circular queue

Topics: Java C++ data structure stack