在堆栈中的特定位置插入元素的方法。 (java)
A method that insert an element at a certain position in a stack. (java)
我正在尝试在堆栈中编写代码,并在其中实现各种方法。我正在尝试一种名为 insertAtPos(int value, int pos)
的方法,我在位置 pos
处插入了一个 value
。这可能吗?
我得到了初始堆栈。我不明白错误在哪里。
这是方法(我的方法):
public void insertAtPos(int pos, int value) { //////////?????
Stack s1 = new Stack();
int c = 0;
while(!this.isEmpty() && c != pos) {
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
这是全部class:
public class Stack {
class Element{
int data;
Element next = null;
Element(int value){
data = value;
next = null;
}
}
private Element head = null;
public Stack() {
head = null;
}
public static void display(Stack s) {
Stack s1 = new Stack();
if(s.isEmpty()) {
System.out.println("the stack is empty");
return;
}
while(!s.isEmpty()) {
s1.push(s.stackTop());
s.pop();
System.out.println(s1.stackTop());
}
while(!s1.isEmpty()) {
s.push(s1.stackTop());
s1.pop();
}
}
public boolean isEmpty() {
return this.head == null;
}
public void push(int value) {
Element tmp = new Element(value);
tmp.next = head;
head = tmp;
}
public int pop() {
if(isEmpty())
System.exit(1);
head = head.next;
return 1;
}
public int stackTop() {
if(isEmpty())
System.exit(2);
return head.data;
}
public void makeEmpty() {
while(!this.isEmpty()) {
this.pop();
}
}
public int stackTopAndPop(){
this.pop();
return this.stackTop();
}
public int search(int value) {
Stack s1 = new Stack();
int index = -1;
int c = 0; //counter
if(this.isEmpty()) {
System.out.println("the stack is empty");
return -1;
}
while(!this.isEmpty() && index == -1) {
if(this.stackTop() == value)
index = c;
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
return index;
}
public int count() {
Stack s1 = new Stack();
int c = 0; // counter
while(!this.isEmpty()) {
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
return c;
}
public void insertNoRepetition(int value) { //////////?????
}
public void insertAtPos(int pos, int value) { //////////?????
Stack s1 = new Stack();
int c = 0;
while(!this.isEmpty() && c != pos) {
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void delete(int value) {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() != value)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteFirstOccurence(int value) { //////////?????
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() != value) {
s1.push(this.stackTop());
return;
}
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteEven() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() % 2 != 0)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteOdd() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() % 2 == 0)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deletePositive() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() < 0)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteLessOrEqualThan(int value) {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() > value)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
//delete all elements = to the elements stored in another stack
public void deleteEqualToOther(Stack s) {
Stack s1 = new Stack();
Stack s2 = new Stack();
while(!this.isEmpty() && this.stackTop() == s.stackTop()) {
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
// public Stack union(Stack s1, Stack s2) {
//
// }
//
//
// public Stack intersection(Stack s1, Stack s2) {
//
// }
public Stack ascendingOrder() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
int tmp = this.pop();
while(!s1.isEmpty() && s1.stackTop() > tmp) {
this.push(s1.stackTop());
s1.pop();
}
s1.push(tmp);
}
return s1;
}
public static void main(String[] args) {
Stack s1 = new Stack();
// Stack s2 = new Stack();
s1.push(1);
s1.push(3);
s1.push(3);
s1.push(4);
s1.push(-5);
display(s1);
System.out.println("______________________");
// s2.push(1);
// s2.push(3);
// s2.push(4);
// s2.push(6);
// s2.push(7);
// display(s2);
// System.out.println("______________________");
// System.out.println(s1.search(3));
// System.out.println("______________________");
//
// System.out.println(s1.count());
// System.out.println("______________________");
//
// s1.delete(2);
// display(s1);
// System.out.println("______________________");
// System.out.println(s1.stackTop());
// System.out.println("______________________");
// s1.makeEmpty();
// display(s1);
// System.out.println("______________________");
// s1.deleteLessOrEqualThan(2);
// display(s1);
// System.out.println("______________________");
// s1.deleteEqualToOther(s2);
// display(s1);
// System.out.println("______________________");
// s1.ascendingOrder();
// display(s1);
// System.out.println("______________________");
// s1.deleteFirstOccurence(3);
// display(s1);
// System.out.println("______________________");
s1.insertAtPos(2, 2);
display(s1);
}
}
您得到的结果是初始堆栈,因为您从未在方法“insertAtPos”中压入“值”。
是的,在您的堆栈实现中是可能的。但是你尝试在栈的内部位置插入一个元素是没有任何意义的。这打破了 LIFO“后进先出”的限制。如果您想出于编码学习的目的这样做,那没关系,但是堆栈的实际实现不应该包含该功能。
我正在尝试在堆栈中编写代码,并在其中实现各种方法。我正在尝试一种名为 insertAtPos(int value, int pos)
的方法,我在位置 pos
处插入了一个 value
。这可能吗?
我得到了初始堆栈。我不明白错误在哪里。
这是方法(我的方法):
public void insertAtPos(int pos, int value) { //////////?????
Stack s1 = new Stack();
int c = 0;
while(!this.isEmpty() && c != pos) {
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
这是全部class:
public class Stack {
class Element{
int data;
Element next = null;
Element(int value){
data = value;
next = null;
}
}
private Element head = null;
public Stack() {
head = null;
}
public static void display(Stack s) {
Stack s1 = new Stack();
if(s.isEmpty()) {
System.out.println("the stack is empty");
return;
}
while(!s.isEmpty()) {
s1.push(s.stackTop());
s.pop();
System.out.println(s1.stackTop());
}
while(!s1.isEmpty()) {
s.push(s1.stackTop());
s1.pop();
}
}
public boolean isEmpty() {
return this.head == null;
}
public void push(int value) {
Element tmp = new Element(value);
tmp.next = head;
head = tmp;
}
public int pop() {
if(isEmpty())
System.exit(1);
head = head.next;
return 1;
}
public int stackTop() {
if(isEmpty())
System.exit(2);
return head.data;
}
public void makeEmpty() {
while(!this.isEmpty()) {
this.pop();
}
}
public int stackTopAndPop(){
this.pop();
return this.stackTop();
}
public int search(int value) {
Stack s1 = new Stack();
int index = -1;
int c = 0; //counter
if(this.isEmpty()) {
System.out.println("the stack is empty");
return -1;
}
while(!this.isEmpty() && index == -1) {
if(this.stackTop() == value)
index = c;
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
return index;
}
public int count() {
Stack s1 = new Stack();
int c = 0; // counter
while(!this.isEmpty()) {
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
return c;
}
public void insertNoRepetition(int value) { //////////?????
}
public void insertAtPos(int pos, int value) { //////////?????
Stack s1 = new Stack();
int c = 0;
while(!this.isEmpty() && c != pos) {
s1.push(this.stackTop());
this.pop();
c++;
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void delete(int value) {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() != value)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteFirstOccurence(int value) { //////////?????
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() != value) {
s1.push(this.stackTop());
return;
}
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteEven() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() % 2 != 0)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteOdd() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() % 2 == 0)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deletePositive() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() < 0)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
public void deleteLessOrEqualThan(int value) {
Stack s1 = new Stack();
while(!this.isEmpty()) {
if(this.stackTop() > value)
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
//delete all elements = to the elements stored in another stack
public void deleteEqualToOther(Stack s) {
Stack s1 = new Stack();
Stack s2 = new Stack();
while(!this.isEmpty() && this.stackTop() == s.stackTop()) {
s1.push(this.stackTop());
this.pop();
}
while(!s1.isEmpty()) {
this.push(s1.stackTop());
s1.pop();
}
}
// public Stack union(Stack s1, Stack s2) {
//
// }
//
//
// public Stack intersection(Stack s1, Stack s2) {
//
// }
public Stack ascendingOrder() {
Stack s1 = new Stack();
while(!this.isEmpty()) {
int tmp = this.pop();
while(!s1.isEmpty() && s1.stackTop() > tmp) {
this.push(s1.stackTop());
s1.pop();
}
s1.push(tmp);
}
return s1;
}
public static void main(String[] args) {
Stack s1 = new Stack();
// Stack s2 = new Stack();
s1.push(1);
s1.push(3);
s1.push(3);
s1.push(4);
s1.push(-5);
display(s1);
System.out.println("______________________");
// s2.push(1);
// s2.push(3);
// s2.push(4);
// s2.push(6);
// s2.push(7);
// display(s2);
// System.out.println("______________________");
// System.out.println(s1.search(3));
// System.out.println("______________________");
//
// System.out.println(s1.count());
// System.out.println("______________________");
//
// s1.delete(2);
// display(s1);
// System.out.println("______________________");
// System.out.println(s1.stackTop());
// System.out.println("______________________");
// s1.makeEmpty();
// display(s1);
// System.out.println("______________________");
// s1.deleteLessOrEqualThan(2);
// display(s1);
// System.out.println("______________________");
// s1.deleteEqualToOther(s2);
// display(s1);
// System.out.println("______________________");
// s1.ascendingOrder();
// display(s1);
// System.out.println("______________________");
// s1.deleteFirstOccurence(3);
// display(s1);
// System.out.println("______________________");
s1.insertAtPos(2, 2);
display(s1);
}
}
您得到的结果是初始堆栈,因为您从未在方法“insertAtPos”中压入“值”。
是的,在您的堆栈实现中是可能的。但是你尝试在栈的内部位置插入一个元素是没有任何意义的。这打破了 LIFO“后进先出”的限制。如果您想出于编码学习的目的这样做,那没关系,但是堆栈的实际实现不应该包含该功能。