tags: queue algorithm data structure java Linked list
Stack is limitedInsert and delete only at the end of the tableofLinear table. The one end that allows insertion and deletion is called the top of the stack, the other end is called the bottom of the stack, and the stack without any data elements is called an empty stack.
Basic operation of the stack
1. Initialization: construct an empty stack
2. Destroy: destroy an existing stack
3. Insert: Insert a new element at the top of the stack
4. Delete: delete the top element of the stack
5. Get: Get the top data element of the stack
6. Judge empty: judge whether the current stack is empty
7. Find the length: find the number of data elements in the stack
8. Positive sequence traversal: visit each element in the stack in turn and output
1. Sequence stack;
1. Initialization of the stack;
//No parameter construction method
public sequenceStack( )
{
top=-1;
stackArray=(T[])new Object[MaxSize];
}
//Parameter construction method
public sequenceStack(int n)
{
if (n<=0) {
System .out.println("Array...The program runs! ");
System.exit(1);
}
top=-1;
stackArray=(T[])new Object[n];
}
2. Push into the stack;
public void push(T obj)
{
if(top==stackArray.length-1){
T []p=(T[])new Object [top*2];
for(int i=0;i<=top;i++)
p[i]=stackArray[i];
stackArray=p;
}
top++;
stackArray[top]=obj;
}
3. Pop out of the stack;
public T pop()
{
if(top==-1){
System.out.println("Empty stack, element cannot be deleted");
return null;
}
top--;
return stackArray[top+1];
}
4. Take the top element of the stack;
public T getHead()
{
if(top==-1){
System.out.println("Empty stack, element cannot be deleted");
return null;
}
return stackArray[top];
}
5. Determine the stack empty operation;
public boolean isEmpty()
{
return top==-1;
}
6. Find the length of the stack;
public int size()
{
return top+1;
}
7. Traverse the stack;
public void nextOrder()
{
for(int i=top;i>=0;i--)
System.out.println(stackArray[i]);
}
8. Clear the stack operation;
public void clear()
{
top=-1;
}
2. Chain stack
The link storage structure of the stack is called the chain stack.

Insertion and deletion are only executed at the top of the stack; the top of the chain stack is at the chain head. The basic operation of the chain stack is as follows:

1. Push into the stack;
public void push(T obj) //Into the stack
{
top=new Node<T>(obj,top);
length++;
}
2. Pop out of the stack;
public T pop() //Pull
{
if(top==null){
System.out.println("Empty stack, cannot be deleted");
return null;
}
T x=top.data;
top=top.next;
length--;
return x;
}
3. Take the top element of the stack;
public T getHead() //Fetch the top data element of the stack
{
if(top==null){
System.out.println("Stack empty...!");
return null;
}
return top.data;
}
4. Find the number of elements, judge empty, and destroy;
public int size() //Find the number of data elements in the stack
{
return length;
}
public boolean isEmpty( ) //Determine whether the current stack is empty
{
return top==null;
}
public void clear() //Destroy an existing stack
{
top=null;
}
A queue is a linear table with restricted operations. The addition of elements is performed at one end of the table, and the deletion of elements is performed at the other end of the table. The end that is allowed to be inserted is called the rear, and the end that is allowed to be deleted is called the front.
(First in, first out principle)
Basic operation of the queue
1. Initialization: construct an empty queue
2. Destroy: Destroy an existing queue
3. Insert: Insert a new element at the end of the queue
4. Delete: delete the queue header element
5. Get: Get the head element of the queue
6. Judge empty: judge whether the current queue is empty
7. Find the length: find the number of data elements in the queue
8. Positive sequence traversal: visit each element in the queue in turn and output
1. Sequential queue + circular queue
The storage area of the sequential queue is assumed to be a ring connected end to end. The queue with this special structure is usually called a circular queue.
Basic operation:
1. Queue initialization;
public sequenceQueue()
{
front=rear=0;
queueArray=(T[])new Object[MaxSize];
}
2. Join the team;
public void EnQueue(T obj)
{
if((rear+1)%queueArray.length==front){
T[] p=(T[])new Object[queueArray.length*2];
if(rear==((T[])queueArray).length-1)
{
for(int i=1;i<=rear;i++)
p[i]=queueArray[i];
}
else{
int i,j=1; for(i=front+1;i<queueArray.length;i++,j++)
p[j]=queueArray[i];
for(i=0;i<=rear;i++,j++)
p[j]=queueArray[i];
front=0;
rear=queueArray.length-1;
}
queueArray=p;
}
rear=(rear+1)%queueArray.length;
queueArray[rear]=obj;
}
3. Departure;
public T DeQueue()
{
if(isEmpty()){
System.out.println("Empty Team");
return null;
}
front=(front+1)%queueArray.length;
return queueArray[front];
}
4. Take the head element operation;
public T getHead( )
{
if(isEmpty()){
System.out.println("Empty Team");
return null;
}
return queueArray[(front+1)%queueArray.length];
}
5. Non-empty judgment;
public boolean isEmpty( )
{
return front==rear;
}
6. Find the length;
public int size()
{
return (rear-front+queueArray.length)%queueArray.length;
}
7. Traverse the queue;
public void nextOrder()
{
int i,j=front;
for(i=1;i<=size();i++)
{
j=(j+1)%queueArray.length;
System.out.println(queueArray[j]);
}
}
8. Clear the queue;
public void clear()
{ front=rear=0; }
2. Chain queue;
The queue represented by the linked list is called the chain queue.
1. Initialize the chain queue;
public linkQueue()
{
length=0;
front=rear=new Node<T>(null);
}
2. Join the team;
public void EnQueue(T obj)
{
rear=rear.next=new Node<T>(obj,null);
length++;
}
3. Departure;
public T DeQueue()
{
if(isEmpty( )){
System.out.println("Empty Team");
return null;
}
Node<T> p=front.next;
T x=p.data;
front.next=p.next;
length--;
if(front.next==null)
rear=front;
return x;
}
4. Take the head of the team;
public T getHead()
{
if(isEmpty( )){
System.out.println("Empty Team");
return null;
}
return front.next.data;
}
5. Find the length of the queue;
public int size()
{
return length;
}
6. Determine whether the queue is empty;
public boolean isEmpty( )
{
return front.next==null;
}
7. Output elements;
public void nextOrder()
{
Node<T> p=front.next;
while(p!=null){
System.out.println(p.data);
p=p.next;
}
}
8. Clear the queue;
public void clear()
{
front.next=rear.next=null;
}
1. The stack is a linear table limited to insert and delete operations only at the end of the table. Insertion and deletion are allowed at one end is called the top of the stack, and the other end is called the bottom of the stack.
2. The characteristic of the stack is last in first out.
3. Queue is a linear table with restricted operations. The end that allows insertion is called rear, and the end that allows deletion is called front.
4. The characteristic of the queue is first in, first out.
5. The queue of sequential storage structure has a false overflow phenomenon, so a circular queue is introduced.
6. The empty judgment condition of the circular queue is front==, and the full judgment condition is: (rear+1)% queueArray.length == front.
Stack Stack application Stack interface design Effective braces (Leetcode 20 questions) Original method User stack solution Use stack + HashMap solution queue Queue interface design Use stack to achie...
Stack and queue 1. Design a stack that get the smallest value of the current stack 1. Design a stack that get the smallest value of the current stack Design a stack that can get the smallest value of ...
Stack concept As mentioned in the title, the stack is a constrained linear structure. We gave a regulation in a linear structure: the first one in the first one. This is like a book is placed on the g...
Introduction Stack is linear form that defines only inserted and deleted operations at the end of the tail. The queue is only allowed to perform a linear table for deleting operations on the other end...
Stack, queue One, stack Second, the queue One, stack Definition: A limited linear structure Features: 1 The data in the stack complies with 'advanced first "principle 2 can only operate data on t...
Table of contents 1. Stack structure and queue structure 1. What are stacks and queues? 2. The structure of the stack and queue 3. The implementation of the array stack and linked list stack (Java lan...
First, give two common examples in life. I believe everyone will encounter this kind of phenomenon when using computers for work and entertainment. When we click on the program or perform other operat...
Data structure and algorithm linear structure stack and queue About the stack and queue, its characteristics are, stacks: Backward first out, queue: advanced first out; through Java code is implemente...
@TOC Data structure and algorithm stack (stack), queue (queue), package (bag) First, the data structure (1) Package: The package can be regarded as a bag. It is thrown into the stone when it meets on ...
We know that in the array, if you know the subscript of the data item, you can immediately access the data item, or by searching the data item sequentially, accessing each data item in the array. But ...