数据结构之栈的实现
栈是一种受限的线性表,栈只能在栈顶添加和删除元素,出入规则为先入后出(FILO)。
java实现栈
Node类的实现
package MyStack; public class Node { private Object data; private Node rear; private Node front; public Node(){} public Node(Object data){ super(); this.data = data; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public Node getRear() { return rear; } public void setRear(Node rear) { this.rear = rear; } public Node getFront() { return front; } public void setFront(Node front) { this.front = front; } }
链栈MyLinkStack类的实现
package MyStack; // 链栈的实现 public class MyLinkStack { private Node base; private Node top; private Node next; private int size; // 获取长度 public int getSize(){ return size; } // 入 public void push(Object data){ if (size==0){ base = new Node(data); top = new Node(); top.setRear(base); next = base; size++; }else { top = new Node(); Node node = new Node(data); top.setRear(node); node.setRear(next); next = node; size++; } } // 出 public Object pop() throws Exception { if (size == 0){ throw new Exception("没有元素在栈中"); }else { Node node = next; top.setRear(next.getRear()); next = next.getRear(); size--; return node.getData(); } } }
测试代码test
package MyStack; public class Test { public static void main(String[] args) throws Exception { MyLinkStack myLinkStack = new MyLinkStack(); System.out.println(myLinkStack.getSize()); myLinkStack.push(1); myLinkStack.push(2); myLinkStack.push(3); System.out.println(myLinkStack.getSize()); System.out.println(myLinkStack.pop()); System.out.println(myLinkStack.pop()); System.out.println(myLinkStack.pop()); System.out.println(myLinkStack.getSize()); myLinkStack.push(1); myLinkStack.push(2); myLinkStack.push(3); System.out.println(myLinkStack.pop()); System.out.println(myLinkStack.pop()); System.out.println(myLinkStack.pop()); } }
顺序栈MySeqStack的实现
package MyStack; public class MySeqStack { private int curSize; private Object [] stack; private int maxSize; public MySeqStack(int maxSize){ this.stack = new Object[maxSize]; this.maxSize = maxSize; } // getsize public int getSize(){ return curSize; } // push public void push(Object data) throws Exception { if (curSize==0){ stack[0] = data; curSize++; }else if (curSize == maxSize){ throw new Exception("栈已满了"); }else { stack[curSize] =data; curSize++; } } // pop public Object pop() throws Exception { if (curSize == 0){ throw new Exception("栈内无元素"); }else { Object data = stack[curSize-1]; stack[curSize-1] = null; curSize--; return data; } } }
测试代码Test2
package MyStack; public class Test2 { public static void main(String[] args) throws Exception { MySeqStack mySList = new MySeqStack(10); System.out.println(mySList.getSize()); mySList.push(1); mySList.push(2); mySList.push(3); mySList.push(4); System.out.println(mySList.pop()); System.out.println(mySList.pop()); System.out.println(mySList.pop()); System.out.println(mySList.pop()); System.out.println(mySList.pop()); mySList.push(1); mySList.push(2); mySList.push(3); mySList.push(4); mySList.push(5); mySList.push(6); mySList.push(7); mySList.push(8); mySList.push(9); mySList.push(10); System.out.println(mySList.pop()); System.out.println(mySList.pop()); System.out.println(mySList.pop()); System.out.println(mySList.pop()); } }
python实现栈
class MySeqStock(object): def __init__(self,maxsize): self.maxsize = maxsize self.curSize = 0 self.stock = [] # 查长度 def getSize(self): return self.curSize # push def push(self,data): if self.curSize == self.maxsize: print("栈已满") else: self.stock.append(data) self.curSize +=1 # pop def pop(self): if self.curSize == 0: return "栈内无数据" else: curSize = self.curSize data = self.stock[curSize-1] del self.stock[curSize-1] self.curSize -= 1 return data class SingleNode(object): def __init__(self,data): self.data = data self.next = None class MyLinkStock(object): def __init__(self): self.top = SingleNode(None) self.next = SingleNode(None) self.base = SingleNode(None) self.size = 0 def getSize(self): return self.size def push(self,data): if self.size == 0: node = SingleNode(data) self.base = node self.top.next = self.base self.next = self.base self.size+=1 else: node = SingleNode(data) node.next = self.next self.top.next = node self.next = node self.size += 1 def pop(self): if self.size == 0: return "栈内无元素" else: self.top.next = self.next.next data = self.next.data self.next = self.top.next self.size -=1 return data print("==============顺序栈=====================") myseqstock = MySeqStock(10) print("size",myseqstock.getSize()) myseqstock.push(1) myseqstock.push(2) myseqstock.push(3) myseqstock.push(4) myseqstock.push(1) myseqstock.push(2) myseqstock.push(3) myseqstock.push(4) myseqstock.push(1) myseqstock.push(2) print(myseqstock.pop()) print(myseqstock.pop()) print(myseqstock.pop()) print(myseqstock.pop()) print(myseqstock.pop()) print("==============链栈=====================") MyLinkStock = MyLinkStock() MyLinkStock.push(1) MyLinkStock.push(2) MyLinkStock.push(3) MyLinkStock.push(4) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop()) MyLinkStock.push(1) MyLinkStock.push(2) MyLinkStock.push(3) MyLinkStock.push(4) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop()) print(MyLinkStock.pop())
如果有所帮助,请点个赞把!!!