Java棧的實現(xiàn)
創(chuàng)新互聯(lián)擁有網(wǎng)站維護技術和項目管理團隊,建立的售前、實施和售后服務體系,為客戶提供定制化的網(wǎng)站設計、成都網(wǎng)站制作、網(wǎng)站維護、電信機房托管解決方案。為客戶網(wǎng)站安全和日常運維提供整體管家式外包優(yōu)質服務。我們的網(wǎng)站維護服務覆蓋集團企業(yè)、上市公司、外企網(wǎng)站、商城網(wǎng)站建設、政府網(wǎng)站等各類型客戶群體,為全球成百上千家企業(yè)提供全方位網(wǎng)站維護、服務器維護解決方案。
public class MyStack { //定義一個堆棧類
int[] array; //用int數(shù)組來保存數(shù)據(jù),根據(jù)需要可以換類型
int s_size; //定義堆棧的寬度
public MyStack(int i){ //定義一個帶參數(shù)構造器
array=new int[i]; //動態(tài)定義數(shù)組的長度
s_size=0; //堆棧的默認寬度為0
}
public MyStack(){ //默認構造器
this(50); //默認構造器可容納50個元素
}
public void push(int i){ //壓棧
array[this.s_size]=i;
this.s_size++;
}
public int pop(){ //從堆棧中取元素,從棧頂開始取
if(this.s_size!=0){
int t=array[s_size-1]; //用中間變量保存棧頂?shù)脑?/p>
array[s_size-1]=0; //取完元素該位置設為0
s_size--; //棧的大小減1
return t; //返回棧頂元素
}else{
System.out.println("This stack is empty"); //當棧為空時顯示提示信息,返回0
return 0;
}
}
public boolean isEmpty(){ //判斷棧是否為空
return this.s_size==0;
}
public int top(){ //從棧頂取值,功能和 pop() 方法一樣
if(!this.isEmpty()){
int t=array[this.s_size-1];
array[this.s_size-1]=0;
this.s_size--;
return t;
}else{
System.out.println("This stack is empty!");
return 0;
}
}
public void printAll(){ //打印出堆棧中的所有元素的值,不是取出,元素依然在堆棧里
if(!this.isEmpty()){
for(int i=this.s_size - 1;i=0;i--){
System.out.println(array[i]);
}
}
}
//下面是測試代碼
public static void main(String[] args){
MyStack stack=new MyStack();
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
//System.out.println(stack.isEmpty());
stack.printAll();
System.out.println("===========");
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
}
}
/**
* GenericLinkedStack.java
*/
package fix;
import java.util.EmptyStackException;
/**
*泛型的鏈式棧數(shù)據(jù)結構
*/
public class GenericLinkedStackE {
// 棧頂元素
private Item top = null;
// 返回棧頂元素,并彈出
public E pop() throws EmptyStackException {
if (isEmpty()) {
throw new EmptyStackException();
}
E e = top.value;
top = top.next;
return e;
}
/**
* 棧頂壓入一個元素
* @param e 被壓入的元素
*/
public void push(E e) {
Item curr = new Item(e);
curr.next = top;
top = curr;
}
/**
* 返回棧頂元素,但不出棧
* @return 棧頂元素
*/
public E peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return top.value;
}
/**
* 判斷棧是否為空
* @return 判斷結果
*/
public boolean isEmpty() {
return top == null;
}
/**
* 棧中元素
* @author jilen
*
*/
class Item {
//元素
private E value;
//下一個
private Item next;
public Item(E e) {
this.value = e;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Item getNext() {
return next;
}
public void setNext(Item next) {
this.next = next;
}
}
}
/**
* InfixToPostfixConverter.java
*/
package fix;
import java.util.Hashtable;
/**
* @author jilen
*
*/
public class InfixToPostfixConverter {
// 操作符及其優(yōu)先級組成的鍵值對
private static final HashtableCharacter, Integer operators;
private StringBuffer infix;
private StringBuffer postfix;
GenericLinkedStackCharacter stack = new GenericLinkedStackCharacter();
// 初始化操作符列表,static語句塊會在加載類時自動執(zhí)行
static {
operators = new HashtableCharacter, Integer();
operators.put('^', 4);
operators.put('*', 3);
operators.put('/', 3);
operators.put('%', 3);
operators.put('+', 2);
operators.put('-', 2);
operators.put('(', -1);
operators.put(')', 5);
}
/**
*
*/
public InfixToPostfixConverter(StringBuffer infix, StringBuffer postfix) {
this.infix = infix;
this.postfix = postfix;
}
/**
* 轉換函數(shù)
*/
public void convertToPostfix() {
// 對輸入字符串中字符遍歷
for (int i = 0, n = infix.length(); i n; i++) {
char c = infix.charAt(i);
// 是數(shù)字之間添加到轉換后字符串
if (isNumber(c)) {
postfix.append(c);
} else if (isOperator(c)) {
switch (c) {
// '(' 直接入棧
case '(':
stack.push(c);
break;
// ')' 彈出元素直到碰到'('
case ')':
while (!stack.isEmpty() stack.peek() != '(') {
postfix.append(stack.pop());
}
stack.pop();
break;
// 其他操作符
default:
// 當前操作符比棧頂操作符優(yōu)先級高,直接入棧
if (stack.isEmpty() || precedence(c, stack.peek())) {
stack.push(c);
}
// 當前操作符比棧頂操作符優(yōu)先級低,出棧直到為空或棧頂優(yōu)先級低于當前操作符
else if (!precedence(c, stack.peek())) {
while (!stack.isEmpty() !precedence(c, stack.peek())) {
postfix.append(stack.pop());
}
stack.push(c);
}
break;
}
}
}
// 若棧中還有操作符,所以元素出棧
while (!stack.isEmpty()) {
postfix.append(stack.pop());
}
}
/**
* 判斷是否為操作符
* @param c
* @return
*/
public static boolean isOperator(char c) {
return operators.containsKey(c);
}
/**
* 優(yōu)先級大小關系operator1 operator2 則返回true,否則false
* @param operator1
* @param operator2
* @return 判斷結果
*/
public static boolean precedence(char operator1, char operator2) {
return operators.get(operator1) operators.get(operator2);
}
/**
* 是否數(shù)字
* @param c 要判斷的字符
* @return 判斷結果
*/
public static boolean isNumber(char c) {
return c = '0' c = '9';
}
}
/**
*Main.java測試類
*/
package fix;
/**
* @author Administrator
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
StringBuffer infix = new StringBuffer("(1+2)*3/4");
StringBuffer postfix = new StringBuffer();
InfixToPostfixConverter converter = new InfixToPostfixConverter(infix,
postfix);
converter.convertToPostfix();
System.out.println(postfix.toString());
}
}
中綴轉后綴的程序,有GenericLinkedStack.java,InfixToPostfix.java,Main.java三個源文件需要放在fix包下
棧和隊列是兩種特殊的線性表,它們的邏輯結構和線性表相同,只是其運算規(guī)則較線性表有更多的限制,故又稱它們?yōu)檫\算受限的線性表。
LinkedList數(shù)據(jù)結構是一種雙向的鏈式結構,每一個對象除了數(shù)據(jù)本身外,還有兩個引用,分別指向前一個元素和后一個元素,和數(shù)組的順序存儲結構(如:ArrayList)相比,插入和刪除比較方便,但速度會慢一些。
棧的定義
棧(Stack)是限制僅在表的一端進行插入和刪除運算的線性表。
(1)通常稱插入、刪除的這一端為棧頂(Top),另一端稱為棧底(Bottom)。
(2)當表中沒有元素時稱為空棧。
(3)棧為后進先出(Last In First Out)的線性表,簡稱為LIFO表。
棧的修改是按后進先出的原則進行。每次刪除(退棧)的總是當前棧中"最新"的元素,即最后插入(進棧)的元素,而最先插入的是被放在棧的底部,要到最后才能刪除。
實現(xiàn)代碼:
package com.weisou.dataStruct;
import java.util.LinkedList;
@SuppressWarnings("unchecked")
public class MyStack {
LinkedList linkList = new LinkedListObject();
public void push(Object object) {
linkList.addFirst(object);
}
public boolean isEmpty() {
return linkList.isEmpty();
}
public void clear() {
linkList.clear();
}
// 移除并返回此列表的第一個元素
public Object pop() {
if (!linkList.isEmpty())
return linkList.removeFirst();
return "棧內無元素";
}
public int getSize() {
return linkList.size();
}
public static void main(String[] args) {
MyStack myStack = new MyStack();
myStack.push(2);
myStack.push(3);
myStack.push(4);
System.out.println(myStack.pop());
System.out.println(myStack.pop());
}
}
隊列定義
隊列(Queue)是只允許在一端進行插入,而在另一端進行刪除的運算受限的線性表
(1)允許刪除的一端稱為隊頭(Front)。
(2)允許插入的一端稱為隊尾(Rear)。
(3)當隊列中沒有元素時稱為空隊列。
(4)隊列亦稱作先進先出(First In First Out)的線性表,簡稱為FIFO表。
實現(xiàn)代碼:
package com.weisou.dataStruct;
import java.util.LinkedList;
/**
*
* @author gf
* @date 2009-11-13
*/
public class MyQueue {
LinkedList linkedList = new LinkedList();
//隊尾插
public void put(Object o){
linkedList.addLast(o);
//隊頭取 取完并刪除
public Object get(){
if(!linkedList.isEmpty())
return linkedList.removeFirst();
else
return "";
}
public boolean isEmpty(){
return linkedList.isEmpty();
}
public int size(){
return linkedList.size();
}
public void clear(){
linkedList.clear();
}
/**
* @param args
*/
public static void main(String[] args) {
MyQueue myQueue= new MyQueue();
myQueue.put(1);
myQueue.put(2);
myQueue.put(3);
System.out.println(myQueue.get());
}
}
public Object setEle(Object element)
{
Object oldElement = this.element;
this.element = element;
return oldElement;
}
是啥意思,給值還return??把這函數(shù)刪了
public Linked()
{
nextNode = null;
element = null;
}
改成
public Linked(Object element)
{
this.element = element;
nextNode = null;
}
#includestdio.h
#includestdlib.h
struct node{
int data;
struct node* pre;
};
void print(struct node *p) //打印鏈棧
{while(p)
{printf("%d ",p-data);
p=p-pre;
}
}
int main()
{int i;
struct node *p,*q;
for(i=1;i11;i++) //1~10依次入棧
{p=(struct node*)malloc(sizeof(struct node));
if(i==1)p-pre=NULL;
else p-pre=q;
p-data=i;
q=p;
}
print(p);
return 0;
}
當前名稱:java鏈式棧代碼,棧的鏈式存儲結構代碼
本文來源:http://m.2m8n56k.cn/article28/hceccp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、外貿建站、網(wǎng)站設計公司、動態(tài)網(wǎng)站、網(wǎng)站內鏈、建站公司
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)