Preface
Recently participated in some school recruit, bloggers have been asked a lot of data structure questions, many of which need to be answered not only to understand these concepts, but you need a language (c, C + +, Java) to achieve this structure, but also in accordance with the specific needs of the company to further optimize. For bloggers like this, the data structure of weak players, the end result can only be "Beimian". Bitter experience, now the pain of the crazy fill. Just as there is a saying in the river, "the debt owed will eventually return." Chain Stacks
The chain-type storage structure of the stack is called the chain stack.
In the algorithm to use a number of stacks, it is best to use a linked list as the storage structure of the stack, that is, using pointers to implement the stack. The stacks implemented in this way are also called chain stacks. Since the stack's insert and delete operations are only performed in the header, it is not necessary to set a header unit like a single linked list when using pointers to implement the stack.
I. Chain stack structure and data type
The chain-type storage structure of the stack, also known as the chain stack, is a list of limiting operations, that is, the insertion and deletion operations in the linked list can only be performed at the beginning of the list. The chain stack structure is shown in the diagram.
Java implementation of chain stacks:
Package study_02.test; Implementation of/** * chain stack * @author wwx * @param <T> * * public class Linkstack<t> {//define node data structure private class node{P
Ublic T data;
Public Node Next;
The parameterless constructor public node () {} public node (T Data,node next) {this.next=next;
This.data=data;
}///stack top element private Node tops;
element number private int size;
Inserts the data public void push (T element) {top=new Node (element, top);
size++;
///out stack operation public T pop () {Node oldnode=top;
Top=top.next;
Release reference oldnode.next=null;
size--;
return oldnode.data;
//Returns the top element of the stack, but does not stack public T peek () {return top.data;
}//stack length public int length () {return size;
//Determine if the chain stack is empty stack public boolean empty () {return size = = 0;
The public String toString () {//chain stack is an empty chain stack when if (empty ()) return "[]";
else{StringBuilder sb = new StringBuilder ("[");
for (Node current = top; current!= null; current = Current.next) {sb.append (current.data.toString () + ","); int len = Sb.lengTh ();
Return Sb.delete (Len-2, Len). Append ("]"). ToString ();
} public static void Main (string[] args) {linkstack<string> stack = new linkstack<string> ();
Constantly into the stack Stack.push ("AAAA");
Stack.push ("bbbb");
Stack.push ("CCCC");
Stack.push ("dddd");
SYSTEM.OUT.PRINTLN (stack);
Access stack top element System.out.println ("Access stack top element:" + Stack.peek ());
pops up an element System.out.println ("first pop-up stack top element:" + stack.pop ());
Again pops up an element System.out.println ("second pop-up stack top element:" + stack.pop ());
System.out.println ("Stack after two pops:" + stack);
}
}
Output results:
[dddd, CCCC, BBBB, AAAA]
Access stack top element: dddd
First pop-up stack top element: dddd
Second pop-up stack top element: CCCC
Two times after the pop stack: [bbbb, AAAA]