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