用顺序结构和链式结构分别实现栈和队列
一、栈
实现顺序栈和链栈的插入、删除、获取栈顶等方法(function(){
var Stack = function(){
var arr = [];
//压栈
var push = function(node){
arr[arr.length] = node;
}
//弹栈
var pop = function(){
var top = arr[arr.length-1];
arr.length = arr.length-1;
return top;
}
//获取栈顶元素
var getTop = function(){
return arr[arr.length-1];
}
//获取栈
var getStack = function(){
let result = [];
for(let i=0;i<arr.length;i++){
result[i] = arr[i];
}
return result;
}
return {
push:push,
pop:pop,
getTop:getTop,
getStack:getStack
}
}
var stack = new Stack();
stack.push(4);
stack.push(2);
stack.push(3);
console.log(stack.getTop());
stack.pop();
console.log(stack.getStack());
//辅助链栈结点
var Node = function(){
this.next = null;
this.data = null;
}
//链栈
var linkStack = function(){
var tail = new Node();
//压栈
var push = function(node){
let newNode = new Node();
newNode.data = node;
let p = tail;
while(p.next != null){
p = p.next;
}
p.next = newNode;
}
//弹栈
var pop = function(){
let p = tail, pre = null;
while(p.next != null){
pre = p;
p = p.next;
}
pre.next = null;
return p.data;
}
//获取栈顶元素
var getTop = function(){
let p = tail;
while(p.next != null){
p = p.next;
}
return p.data;
}
//获取栈
var getStack = function(){
let result = [];
let p = tail;
while(p.next != null){
p = p.next;
result.push(p.data);
}
return result;
}
return {
push:push,
getTop:getTop,
pop:pop,
getStack:getStack
}
}
var link_stack = new linkStack();
link_stack.push(0);
link_stack.push(2);
link_stack.push(1);
//link_stack.pop();
console.log(link_stack.getTop());
console.log(link_stack.getStack());
})();
二、队列
实现顺序队列和链式队列的插入、删除、获取队列头等方法
(function(){
var CirQueue = function(queueSize){
if(typeof queueSize !== "number"){
queueSize = 100;
}
var arr = [];
var front = queueSize-1, rear = queueSize-1;
//插入队列
var insert = function(x){
if((rear+1)%queueSize === front){
throw Error("CirQueue overflow!");
}
rear = (rear+1)%queueSize;
arr[rear] = x;
}
//删除队列头
var shift = function(){
if(rear === front){
throw Error("CirQueue is already empty!");
}
front = (front+1)%queueSize;
return arr[front];
}
//获取队列头
var getHead = function(){
if(rear === front){
throw Error("CirQueue is already empty!");
}
var index = (front+1)%queueSize;
return arr[index];
}
//获取队列长度
var length = function(){
var index = front;
var count = 0;
while(index%queueSize !== rear){
index++;
count++;
}
return count;
}
//获取队列
var getQueue = function(){
var index = front;
var result = [];
var count = 0;
while(index%queueSize !== rear){
index++;
result[count++] = arr[index];
//alert(index);
}
return result;
}
return {
insert:insert,
shift:shift,
getHead:getHead,
getQueue:getQueue,
length:length
}
}
var queue = new CirQueue(6);
queue.insert(0);
queue.insert(1);
queue.insert(2);
queue.insert(3);
queue.insert(4);
queue.shift();
queue.insert(5);
console.log(queue.getQueue());
console.log(queue.length());
})();
(function(){
//辅助链式结构创造结点
var Node = function(){
this.next = null;
this.data = null;
}
//循环队列
var LinkQueue = function(queueSize){
if(typeof queueSize !== "number"){
queueSize = 100;
}
var arr = [];
var rear = front = new Node();
//插入队列
var insert = function(x){
var newNode = new Node();
newNode.data = x;
rear.next = newNode;
rear = newNode;
}
//删除队列头
var shift = function(){
if(rear === front){
throw Error("LinkQueue is already empty!");
}
var p = front.next;
var data = front.data;
front.next = p.next;
if(p.next === null){
rear = front;
}
return data;
}
//获取队列头
var getHead = function(){
if(rear === front){
throw Error("LinkQueue is already empty!");
}
return front.next.data;
}
//获取队列长度
var length = function(){
var p = front;
var count = 0;
while(p.next !== null){
p = p.next;
count++;
}
return count;
}
//获取队列
var getQueue = function(){
var result = [];
var p = front;
var count = 0;
while(p.next !== null){
p = p.next;
result[count++] = p.data;
}
return result;
}
return {
insert:insert,
shift:shift,
getHead:getHead,
getQueue:getQueue,
length:length
}
}
var queue = new LinkQueue(6);
queue.insert(0);
queue.insert(1);
queue.insert(2);
queue.insert(3);
queue.insert(4);
queue.shift();
queue.insert(5);
//console.log(queue.getHead());
console.log(queue.getQueue());
console.log(queue.length());
})();