/** ********************* * Construct an empty linked list. ********************* */ publicLinkedList() { header = newNode(0); }// Of the first constructor
/** ********************* * Locate the given value. If it appears in multiple positions, simply return * the first one. * * @param paraValue The given value. * @return The position. -1 for not found. ********************* */ publicintlocate(int paraValue) { inttempPosition= -1;
NodetempNode= header.next; inttempCurrentPosition=0; while (tempNode != null) { if (tempNode.data == paraValue) { tempPosition = tempCurrentPosition; break; } // Of if
tempNode = tempNode.next; tempCurrentPosition++; } // Of while
/** ********************* * Insert a value to a position. If the list is already full, do nothing. * * @param paraPosition The given position. * @param paraValue The given value. * @return Success or not. ********************* */ publicbooleaninsert(int paraPosition, int paraValue) { NodetempNode= header; Node tempNewNode;
for (inti=0; i < paraPosition; i++) { if (tempNode.next == null) { System.out.println("The position " + paraPosition + " is illegal."); returnfalse; } // Of if
tempNode = tempNode.next; } // Of for i
// Construct a new node. tempNewNode = newNode(paraValue);
// Now link them. tempNewNode.next = tempNode.next; tempNode.next = tempNewNode;
/** ********************* * Delete a value at a position. * * @param paraPosition The given position. * @return Success or not. ********************* */ publicbooleandelete(int paraPosition) { if (header.next == null) { System.out.println("Cannot delete element from an empty list."); returnfalse; } // Of if
NodetempNode= header;
for (inti=0; i < paraPosition; i++) { if (tempNode.next.next == null) { System.out.println("The position " + paraPosition + " is illegal."); returnfalse; } // Of if
/** * Linked List * * @author Shihuai Wen Email:wshysxcc@outlook.com */ publicclassLinkedList { /** * An inner class. */ classNode { /** * The data. */ int data;
/** * The reference to the next node. */ Node next;
/** ******************* * The constructor * * @param paraValue The data. ******************* */ publicNode(int paraValue) { data = paraValue; next = null; }// Of the constructor }// Of class Node
/** * The header node. The data is never used. */ Node header;
/** ********************* * Construct an empty linked list. ********************* */ publicLinkedList() { header = newNode(0); }// Of the first constructor
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ @Override public String toString() { StringresultString="";
if (header.next == null) { return"empty"; } // Of if
NodetempNode= header.next; while (tempNode != null) { resultString += tempNode.data + ", "; tempNode = tempNode.next; } // Of while
return resultString; }// Of toString
/** ********************* * Reset to empty. Free the space through garbage collection. ********************* */ publicvoidreset() { header.next = null; }// Of reset
/** ********************* * Locate the given value. If it appears in multiple positions, simply return * the first one. * * @param paraValue The given value. * @return The position. -1 for not found. ********************* */ publicintlocate(int paraValue) { inttempPosition= -1;
NodetempNode= header.next; inttempCurrentPosition=0; while (tempNode != null) { if (tempNode.data == paraValue) { tempPosition = tempCurrentPosition; break; } // Of if
tempNode = tempNode.next; tempCurrentPosition++; } // Of while
return tempPosition; }// Of locate
/** ********************* * Insert a value to a position. If the list is already full, do nothing. * * @param paraPosition The given position. * @param paraValue The given value. * @return Success or not. ********************* */ publicbooleaninsert(int paraPosition, int paraValue) { NodetempNode= header; Node tempNewNode;
for (inti=0; i < paraPosition; i++) { if (tempNode.next == null) { System.out.println("The position " + paraPosition + " is illegal."); returnfalse; } // Of if
tempNode = tempNode.next; } // Of for i
// Construct a new node. tempNewNode = newNode(paraValue);
// Now link them. tempNewNode.next = tempNode.next; tempNode.next = tempNewNode;
returntrue; }// Of insert
/** ********************* * Delete a value at a position. * * @param paraPosition The given position. * @return Success or not. ********************* */ publicbooleandelete(int paraPosition) { if (header.next == null) { System.out.println("Cannot delete element from an empty list."); returnfalse; } // Of if
NodetempNode= header;
for (inti=0; i < paraPosition; i++) { if (tempNode.next.next == null) { System.out.println("The position " + paraPosition + " is illegal."); returnfalse; } // Of if
tempNode = tempNode.next; } // Of for i
tempNode.next = tempNode.next.next;
returntrue; }// Of delete
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { LinkedListtempFirstList=newLinkedList(); System.out.println("Initialized, the list is: " + tempFirstList.toString());
for (inti=0; i < 5; i++) { tempFirstList.insert(0, i); } // Of for i System.out.println("Inserted, the list is: " + tempFirstList.toString());
tempFirstList.insert(6, 9);
tempFirstList.delete(4);
tempFirstList.delete(2); System.out.println("Deleted, the list is: " + tempFirstList.toString());
tempFirstList.delete(0); System.out.println("Deleted, the list is: " + tempFirstList.toString());
for (inti=0; i < 5; i++) { tempFirstList.delete(0); System.out.println("Looped delete, the list is: " + tempFirstList.toString()); } // Of for i }// Of main
/** ********************* * Push an element. * * @param paraChar The given char. * @return Success or not. ********************* */ publicbooleanpush(char paraChar) { if (depth == MAX_DEPTH) { System.out.println("Stack full."); returnfalse; } // Of if
data[depth] = paraChar; depth++;
returntrue; }// Of push
三、出栈
描述
从栈顶去除一个元素.
输入
无
输出
返回一个char值为之前的栈顶元素, 若栈为空则返回 '\0'
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** ********************* * Pop an element. * * @return The popped char. ********************* */ publiccharpop() { if (depth == 0) { System.out.println("Nothing to pop."); return'\0'; } // Of if
/** * Char stack. I do not use Stack because it is already defined in Java. * * @author Shihuai Wen Email:wshysxcc@outlook.com */ publicclassCharStack { /** * The depth. */ privatestaticfinalintMAX_DEPTH=10;
/** * The actual depth. */ int depth;
/** * The data */ char[] data;
/** ********************* * Construct an empty char stack. ********************* */ publicCharStack() { depth = 0; data = newchar[MAX_DEPTH]; }// Of the first constructor
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ @Override public String toString() { StringresultString=""; for (inti=0; i < depth; i++) { resultString += data[i]; } // Of for i
return resultString; }// Of toString
/** ********************* * Push an element. * * @param paraChar The given char. * @return Success or not. ********************* */ publicbooleanpush(char paraChar) { if (depth == MAX_DEPTH) { System.out.println("Stack full."); returnfalse; } // Of if
data[depth] = paraChar; depth++;
returntrue; }// Of push
/** ********************* * Pop an element. * * @return The popped char. ********************* */ publiccharpop() { if (depth == 0) { System.out.println("Nothing to pop."); return'\0'; } // Of if
charresultChar= data[depth - 1]; depth--;
return resultChar; }// Of pop
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { CharStacktempStack=newCharStack(); for (charch='a'; ch < 'm'; ch++) { tempStack.push(ch); System.out.println("The current stack is: " + tempStack); } // Of for ch
char tempChar; for (inti=0; i < 12; i++) { tempChar = tempStack.pop(); System.out.println("Poped: " + tempChar); System.out.println("The current stack is: " + tempStack); } // Of for i }// Of main }// Of CharStack