顺序栈
import java.util.Scanner;
public class commonStack {
private Scanner sc = new Scanner(System.in);
private stack[] stacks;
private static int first,end;
private boolean b = true;
public static void main(String[] args) {
new commonStack().control_Table();
}
private int menu(){
System.out.println(" ********************************** \n");
System.out.println(" * 1. 建立栈\n");
System.out.println(" * 2. 添加结点\n");
System.out.println(" * 3. 删除结点\n");
System.out.println(" * 4. 修改结点\n");
System.out.println(" * 5. 查找结点\n");
System.out.println(" * 6. 结点个数\n");
System.out.println(" * 7. 栈是否为空\n");
System.out.println(" * 8. 销毁栈\n");
System.out.println(" * 9. 输出栈中的数据\n");
System.out.println(" * 0. 退出系统\n");
System.out.println(" ********************************** \n");
System.out.print(" 请输入你的选择:");
return sc.nextInt();
}
private void control_Table(){
int num = menu();
while(num < 0 || num > 9) {
System.out.print("没有这个选项,请重新输入:");
num = sc.nextInt();
}
while(true){
switch (num){
case 1:
if(b){
init_Stack();
b = false;
}else{
System.out.println("[ 栈已建立 ]\n");
}
break;
case 2:
add_DataOfStack();
break;
case 3:
pop_DataOfStack();
break;
case 4:
change_DataOfStack();
break;
case 5:
check_DataOfStack();
break;
case 6:
getLength();
break;
case 7:
isEmpty();
break;
case 8:
destory_Stack();
break;
case 9:
put_DataOfStack();
break;
case 0:
break;
default:
break;
}
num = menu();
while(num < 0 || num > 9) {
System.out.print("没有这个选项,请重新输入:");
num = sc.nextInt();
}
if(num == 0){
break;
}
}
System.out.println("欢迎下次使用.");
}
private void init_Stack(){
stacks = new stack[10];
for(int i = 0;i < stacks.length;i++){
stacks[i] = new stack();
}
first = end = 0;
}
private void add_DataOfStack(){
if(stacks == null){
System.out.println("栈未创建\n");
return;
}
if(end - first == 10){
System.out.println("栈已存满\n");
return;
}
System.out.print("请输入编号:");
int id = sc.nextInt();
System.out.print("请输入数据:");
String data = sc.next();
stacks[end].setId(id);
stacks[end].setData(data);
end++;
}
private void pop_DataOfStack(){
if(stacks == null){
System.out.println("[ 栈未创建 ]\n");
return;
}
end--;
}
private void change_DataOfStack(){
if(stacks == null){
System.out.println("[ 栈未创建 ]\n");
return;
}
int i;
System.out.print("请输入需修改数据的结点的编号:");
int oldId = sc.nextInt();
System.out.print("请输入修改后的编号:");
int newId = sc.nextInt();
System.out.print("请输入修改后的数据:");
String data = sc.next();
for(i = first;i < end;i++){
if(stacks[i].getId() == oldId){
stacks[i].setData(data);
stacks[i].setId(newId);
break;
}
}
if(i == end){
System.out.print("[ 不存在编号未" + oldId + "的结点 ]\n");
}
}
private void check_DataOfStack(){
if(stacks == null){
System.out.println("[ 栈未创建 ]\n");
return;
}
int i;
System.out.print("请输入查找结点的编号:");
int id = sc.nextInt();
for(i = first;i < end;i++){
if(stacks[i].getId() == id){
System.out.println("[ id = " + stacks[i].getId() + ", data = " + stacks[i].getData() + " ]");
break;
}
}
if(i == end){
System.out.print("[ 不存在编号未" + id + "的结点. ]\n");
}
}
private void getLength(){
if(stacks == null){
System.out.println("栈未创建\n");
return;
}
System.out.println("[ 结点的个数为:" + (end - first) + " ]\n");
}
private void isEmpty(){
if(stacks == null){
System.out.println("[ 栈未创建 ]\n");
return;
}
if(end == first){
System.out.println("[ 栈空. ]\n");
}else{
System.out.println("[ 栈不为空 ]\n");
}
}
private void destory_Stack(){
stacks = null;
b = true;
}
private void put_DataOfStack(){
if(stacks == null){
System.out.println("[ 栈未创建 ]\n");
return;
}
if(first == end){
System.out.println("[ 栈内无结点 ]\n");
return;
}
for(int n = first;n < end;n++){
System.out.println("[ id = " + stacks[n].getId() + ", data = " + stacks[n].getData() + " ]");
}
}
}
final class stack{
private String data;
private int id = -1;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
import java.util.Scanner;
public class linkedStack {
private Scanner sc = new Scanner(System.in);
private linkedNode front;
private boolean b = true;
public static void main(String[] args) {
new linkedStack().control_Table();
}
private int menu(){
System.out.println(" ********************************** \n");
System.out.println(" * 1. 建立链栈 \n");
System.out.println(" * 2. 添加结点\n");
System.out.println(" * 3. 删除结点\n");
System.out.println(" * 4. 修改结点\n");
System.out.println(" * 5. 查询数据\n");
System.out.println(" * 6. 链栈长度\n");
System.out.println(" * 7. 栈是否空\n");
System.out.println(" * 8. 销毁栈\n");
System.out.println(" * 9. 输出栈中数据\n");
System.out.println(" * 0. 退出系统\n");
System.out.println(" ********************************** \n");
System.out.print(" 请输入你的选择:");
return sc.nextInt();
}
private void control_Table(){
int num = menu();
while(num < 0 || num > 9) {
System.out.print("没有这个选项,请重新输入:");
num = sc.nextInt();
}
while(true){
switch (num){
case 1:
if(b){
init_Stack();
b = false;
}else{
System.out.println("[ 链栈已创建 ]");
}
break;
case 2:
add_NodeOfStack();
break;
case 3:
pop_NodeOfStack();
break;
case 4:
change_NodeOfStack();
break;
case 5:
check_NodeOFStack();
break;
case 6:
getLength();
break;
case 7:
isEmpty();
break;
case 8:
destory_Stack();
break;
case 9:
put_DataOfStack();
break;
case 0:
break;
default:
break;
}
num = menu();
while(num < 0 || num > 9) {
System.out.print("没有这个选项,请重新输入:");
num = sc.nextInt();
}
if(num == 0){
break;
}
}
System.out.println("[ 欢迎下次使用. ]");
}
private void init_Stack(){
front = new linkedNode();
front.setNode(null);
}
private void add_NodeOfStack(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
linkedNode p = new linkedNode();
System.out.print("请输入编号:");
int id = sc.nextInt();
System.out.print("请输入信息:");
String data = sc.next();
p.setId(id);
p.setData(data);
p.setNode(front.getNode());
front.setNode(p);
}
private void pop_NodeOfStack(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
if(front.getNode() == null){
System.out.println("[ 栈内无结点 ]");
}
System.out.print("删除结点的数据为: ");
System.out.print("[ id = " + front.getNode().getId() + ",data = " + front.getNode().getData() + " ]");
front.setNode(front.getNode().getNode());
}
private void change_NodeOfStack(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
if(front.getNode() == null){
System.out.println("[ 链栈内无结点 ]");
return;
}
linkedNode p = front;
System.out.print("请输入修改结点的编号:");
int oldId = sc.nextInt();
System.out.print("请输入修改后的编号:");
int newId = sc.nextInt();
System.out.print("请输入修改后的数据:");
String data = sc.next();
while( p != null && p.getId() != oldId){
p = p.getNode();
}
if(p != null){
p.setId(newId);
p.setData(data);
}else{
System.out.println("[ 不存在此结点. ]");
}
}
private void check_NodeOFStack(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
if(front.getNode() == null){
System.out.println("[ 链栈为空 ]");
return;
}
linkedNode p = front;
System.out.print("请输入修改结点的编号:");
int id = sc.nextInt();
while(p != null && p.getId() != id){
p = p.getNode();
}
if(p != null){
System.out.println("[ 节点的数据为: id = " + p.getId() + ", name = " + p.getData() + " ]\n");
}else{
System.out.println("[ 不存在此结点. ]");
}
}
private void getLength(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
int count = -1;
linkedNode p = front;
while(p != null){
count++;
p = p.getNode();
}
System.out.println("[ 链栈的长度为:" + count + " ]");
}
private void isEmpty(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
if(front.getNode() == null){
System.out.println("[ 链栈为空 ]");
return;
}
System.out.println("[ 链栈未空 ]");
}
private void destory_Stack(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
front = null;
b = true;
}
private void put_DataOfStack(){
if(front == null){
System.out.println("[ 链栈未建立 ]");
return;
}
if(front.getNode() == null){
System.out.println("[ 链栈为空 ]");
return;
}
linkedNode p = front.getNode();
while(p != null){
System.out.println("[ id = " + p.getId() + ", data = " + p.getData() + " ]");
p = p.getNode();
}
}
}
final class linkedNode{
private int id;
private String data;
private linkedNode node;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public linkedNode getNode() {
return node;
}
public void setNode(linkedNode node) {
this.node = node;
}
}