This is the 6th day of my participation in the August More Text Challenge
1. The stack profile
A stack is a special kind of list in which elements can only be accessed through one end of the list, called the top of the stack. And it’s a fifo storage, which means that the last item added is removed first; It is a constrained linear table that can only be inserted and deleted at the top of the table/stack.
2. Stack properties and methods
The two main operations on the stack are pushing an element onto the stack and pushing an element off the stack. Push () is used for pushing and pop() is used for pushing. The peek() method returns only the top element of the stack without removing it. The clear() method clears all elements in the stack, and the length attribute records the number of elements in the stack. The empty property is used to indicate whether the stack contains elements, but the length property can also be used. The variable top records the top position on the stack.
3. Realization of stack
- The structure of the stack
function Stack() {
this.data = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
}
Copy the code
- Push () pushes a new element onto the stack, increasing top by 1
function push(element) {
this.data[this.top++] = element;
}
Copy the code
- Pop () returns the top element, decreasing top by 1
function pop() {
return this.data[--this.top];
}
Copy the code
- Peek () returns to top of stack
function peek() {
return this.data[this.top-1];
}
Copy the code
- Length returns the number of elements in the stack
function length() {
return this.top;
}
Copy the code
- The clear empty stack
function length() {
return this.top;
}
Copy the code
4. Queue Overview
A queue is a first-in-first-out (FIFO) data structure that allows only insertion at one end and deletion at the other. Queues are used in many ways. For example, submit a series of processes to the operating system for execution, print a task pool, and so on.
5. Operation methods and attributes of queues
- The push() method adds elements to the end of the array
- The shift() method removes the first element of the array
- The enqueue() method adds an element to the end of the queue
this.enqueue = function enqueue(element) {
this.data.push(element); };Copy the code
- The dequeue() method removes the element at the head of queue
this.dequeue = function dequeue() {
return this.data.shift();
};
Copy the code
- The toString() method displays all the elements in the queue
this.toString = function toString() {
var retStr = "";
for (var i = 0; i < this.data.length; ++i) {
retStr += this.data[i] + "\n";
}
return retStr;
};
Copy the code
- The empty() method determines whether the queue is empty
this.empty = function empty() {
if (this.data.length == 0) {
return true;
} else {
return false; }};Copy the code
6. Implementation of queues
function Queue() {
this.data = [];
this.front = function front() {
return this.data[0];
};
this.back = function back() {
return this.data[this.data.length - 1];
};
// There are other methods above
}
Copy the code