[关闭]
@ysner 2021-10-25T12:20:25.000000Z 字数 8252 阅读 692

燕山楠的Homework 2

数据结构 JAVA


T1(核心是函数)

本题代码为T1代码。核心代码为函数。

  1. import java.util.Scanner;
  2. public class hw3_1
  3. {
  4. public static class SqStack<Integer>
  5. {
  6. final int initcap=100;
  7. int cap;
  8. int[] data;
  9. int top;
  10. public SqStack()
  11. {
  12. cap=initcap;
  13. data=new int[initcap];
  14. top=-1;
  15. }
  16. public void updcap(int newcap)
  17. {
  18. cap=newcap;
  19. int[] newdata=new int[newcap];
  20. for(int i=0;i<=top;i++)
  21. newdata[i]=data[i];
  22. data=newdata;
  23. }
  24. public boolean empty()
  25. {
  26. return top==-1;
  27. }
  28. public void push(int x)
  29. {
  30. if(top==cap-1)
  31. updcap(cap<<1);
  32. top++;
  33. data[top]=x;
  34. }
  35. public int pop()
  36. {
  37. if(empty())
  38. throw new IllegalArgumentException("The stack is empty.\nstr is not legal!!!");
  39. int x=data[top];
  40. top--;
  41. if(cap>initcap&&top+1==cap/4)
  42. updcap(cap/2);
  43. return x;
  44. }
  45. }
  46. public static void main(String args[])
  47. /*函数思路:模拟题目中提到的操作。
  48. 终端输入一个字符串,然后遍历该字符串。初始化一个空栈。
  49. 若扫到“I”,则将1入栈;若扫到“0”,则将栈顶元素出栈(如果无元素可供出栈,直接报错并输出not legal)
  50. 最后判断若栈非空,也输出not legal;否则输出legal。 、
  51. */
  52. {
  53. Scanner in=new Scanner(System.in);
  54. String s=in.nextLine();
  55. SqStack Q=new SqStack<Integer>();
  56. for(int i=0;i<s.length();i++)
  57. if(s.charAt(i)=='I') Q.push(1);
  58. else Q.pop();
  59. if(Q.empty()) System.out.println("str is legal!");
  60. else System.out.println("The stack is not empty in the end.\nstr is not legal!!!");
  61. }
  62. }

T2(核心是函数)

本题代码为T2代码。核心代码为函数。

  1. import java.util.Scanner;
  2. public class hw3_2
  3. {
  4. public static class SqStack<Integer>
  5. {
  6. final int initcap=100;
  7. int cap;
  8. int[] data;
  9. int top;
  10. public SqStack()
  11. {
  12. cap=initcap;
  13. data=new int[initcap];
  14. top=-1;
  15. }
  16. public void updcap(int newcap)
  17. {
  18. cap=newcap;
  19. int[] newdata=new int[newcap];
  20. for(int i=0;i<=top;i++)
  21. newdata[i]=data[i];
  22. data=newdata;
  23. }
  24. public boolean empty()
  25. {
  26. return top==-1;
  27. }
  28. public void push(int x)
  29. {
  30. if(top==cap-1)
  31. updcap(cap<<1);
  32. top++;
  33. data[top]=x;
  34. }
  35. public int pop()
  36. {
  37. if(empty())
  38. throw new IllegalArgumentException("The stack is empty.\nstr is not legal!!!");
  39. int x=data[top];
  40. top--;
  41. if(cap>initcap&&top+1==cap/4)
  42. updcap(cap/2);
  43. return x;
  44. }
  45. }
  46. public static void main(String args[])
  47. /*函数思路:先特判@是否在字符串正中间,若不在,显然可以直接判不合法并return。
  48. 再把@前的字符按顺序入栈。
  49. 然后依次出栈,并将出栈字符与序列中对应字符相比较,若有不同则不合法并return。
  50. 若到最后都没问题则合法。
  51. */
  52. {
  53. Scanner in=new Scanner(System.in);
  54. String s=in.nextLine();
  55. int len=s.length();
  56. if(s.charAt(len/2)!='@')
  57. {
  58. System.out.println("Not legal!!!");
  59. return;
  60. }
  61. SqStack Q=new SqStack<Integer>();
  62. for(int i=0;i<len/2;i++)
  63. Q.push(s.charAt(i));
  64. for(int i=len/2+1;i<len;i++)
  65. if(Q.pop()!=s.charAt(i))
  66. {
  67. System.out.println("Not legal!!!");
  68. return;
  69. }
  70. System.out.println("Legal!");
  71. }
  72. }

T3(核心是函数)

本题代码为T3代码。核心代码为函数。

输入的第一行是初始队列里元素个数。
输入的第二行是初始队列里的元素。

  1. import java.util.Scanner;
  2. public class hw3_3
  3. {
  4. public static class SqStack<Integer>
  5. {
  6. final int initcap=100;
  7. int cap;
  8. int[] data;
  9. int top;
  10. public SqStack()
  11. {
  12. cap=initcap;
  13. data=new int[initcap];
  14. top=-1;
  15. }
  16. public void updcap(int newcap)
  17. {
  18. cap=newcap;
  19. int[] newdata=new int[newcap];
  20. for(int i=0;i<=top;i++)
  21. newdata[i]=data[i];
  22. data=newdata;
  23. }
  24. public boolean empty()
  25. {
  26. return top==-1;
  27. }
  28. public void push(int x)
  29. {
  30. if(top==cap-1)
  31. updcap(cap<<1);
  32. top++;
  33. data[top]=x;
  34. }
  35. public int pop()
  36. {
  37. if(empty())
  38. throw new IllegalArgumentException("The stack is empty.\nstr is not legal!!!");
  39. int x=data[top];
  40. top--;
  41. if(cap>initcap&&top+1==cap/4)
  42. updcap(cap/2);
  43. return x;
  44. }
  45. }
  46. public static class CSqQueue<Integer>
  47. {
  48. final int mxsize=100;
  49. int[] data;
  50. int front,tail;
  51. public CSqQueue()
  52. {
  53. data=new int[mxsize];
  54. front=tail=0;
  55. }
  56. public boolean empty()
  57. {
  58. return front==tail;
  59. }
  60. public void push(int x)
  61. {
  62. if((tail+1)%mxsize==front)
  63. throw new IllegalArgumentException("The queue is full!!!");
  64. tail=(tail+1)%mxsize;
  65. data[tail]=x;
  66. }
  67. public int pop()
  68. {
  69. if(empty())
  70. throw new IllegalArgumentException("The queue is empty!!!");
  71. front=(front+1)%mxsize;
  72. return data[front];
  73. }
  74. }
  75. public static void main(String args[])
  76. /*函数思路:先用输入的数据创建初始队列Q(循环队列)。
  77. 然后依次将Q中元素出队并入栈S。
  78. 当出队完成后再一一将栈中元素出栈并入队,同时输出。
  79. (栈在过程中起到逆序的作用)
  80. */
  81. {
  82. Scanner in=new Scanner(System.in);
  83. CSqQueue Q=new CSqQueue<Integer>();
  84. SqStack S=new SqStack<Integer>();
  85. int n=in.nextInt();
  86. for(int i=0;i<n;i++)
  87. {
  88. int x=in.nextInt();
  89. Q.push(x);
  90. }
  91. for(int i=0;i<n;i++)
  92. S.push(Q.pop());
  93. System.out.print("The new queue:");
  94. for(int i=0;i<n;i++)
  95. {
  96. int x=S.pop();
  97. Q.push(x);
  98. System.out.print(x+" ");
  99. }
  100. System.out.println();
  101. }
  102. }

T4(核心是函数)

本题代码为T4代码。核心代码为函数。

输入的第一行是原数组里元素个数。
输入的第二行是原数组的元素。

  1. import java.util.Scanner;
  2. public class hw3_4
  3. {
  4. public static class CSqQueue<Integer>
  5. {
  6. final int mxsize=100;
  7. int[] data;
  8. int front,tail;
  9. public CSqQueue()
  10. {
  11. data=new int[mxsize];
  12. front=tail=0;
  13. }
  14. public boolean empty()
  15. {
  16. return front==tail;
  17. }
  18. public void push(int x)
  19. {
  20. if((tail+1)%mxsize==front)
  21. throw new IllegalArgumentException("The queue is full!!!");
  22. tail=(tail+1)%mxsize;
  23. data[tail]=x;
  24. }
  25. public int pop()
  26. {
  27. if(empty())
  28. throw new IllegalArgumentException("The queue is empty!!!");
  29. front=(front+1)%mxsize;
  30. return data[front];
  31. }
  32. }
  33. public static void main(String args[])
  34. /*函数思路:先建立两个循环队列:用来存偶数的队列Q2和用来存奇数的队列Q1。
  35. 然后把输入数据中的奇数位入队Q1,偶数位入队Q2。
  36. 再将Q1里的元素一一出队Q1并入队Q2。
  37. 最后Q2即为题目所求。
  38. */
  39. {
  40. Scanner in=new Scanner(System.in);
  41. CSqQueue Q1=new CSqQueue<Integer>(),Q2=new CSqQueue<Integer>();
  42. int n=in.nextInt();
  43. for(int i=0;i<n;i++)
  44. {
  45. int x=in.nextInt();
  46. if(i%2==0) Q1.push(x);
  47. else Q2.push(x);//修改了这个判断语句
  48. }
  49. while(!Q1.empty())
  50. {
  51. Q2.push(Q1.pop());
  52. }
  53. System.out.print("The new queue:");
  54. for(int i=0;i<n;i++)
  55. System.out.print(Q2.pop()+" ");
  56. System.out.println();
  57. }
  58. }

T5

本题代码为T5代码。

输入的第一行是队列里元素个数。
输入的第二行是队列里的元素。

  1. import java.util.Scanner;
  2. public class hw3_5
  3. {
  4. public static class CSqQueue<Integer>
  5. {
  6. final int MaxSize=10;//初始设定队列大小
  7. int[] data;
  8. int front,rear;
  9. boolean tag;
  10. public CSqQueue()
  11. {
  12. data=new int[MaxSize];
  13. front=rear=0;
  14. tag=false;
  15. }
  16. public boolean empty()
  17. //判断队列是否为空
  18. {
  19. return front==rear&&!tag;
  20. }
  21. public boolean full()
  22. //判断队列是否为满
  23. {
  24. return front==rear&&tag;
  25. }
  26. public int size()
  27. //返回队列大小
  28. {
  29. int x=(rear-front+MaxSize)%MaxSize;
  30. if(x==0&&tag) return MaxSize;
  31. return x;
  32. }
  33. public void push(int x)
  34. //入队操作,若队满则报错。注意入队完后,若front=rear,需改tag为true。
  35. {
  36. if(full())
  37. throw new IllegalArgumentException("The queue is full!!!");
  38. rear=(rear+1)%MaxSize;
  39. if(front==rear) tag=true;
  40. data[rear]=x;
  41. }
  42. public int pop()
  43. //出队操作,若队空则报错。注意出队前,若front=rear,需改tag为false。
  44. {
  45. if(empty())
  46. throw new IllegalArgumentException("The queue is empty!!!");
  47. if(front==rear) tag=false;
  48. front=(front+1)%MaxSize;
  49. return data[front];
  50. }
  51. public int top()
  52. //查询队首操作,若队空则提示并返回-1
  53. {
  54. if(empty())
  55. {
  56. System.out.println("The queue is empty!There is no top!");
  57. return -1;
  58. }
  59. return data[(front+1)%MaxSize];
  60. }
  61. }
  62. public static void main(String args[])
  63. //尝试每一个队列的基本运算并输出结果
  64. {
  65. Scanner in=new Scanner(System.in);
  66. CSqQueue Q=new CSqQueue<Integer>();
  67. int n=in.nextInt();
  68. for(int i=0;i<n;i++)
  69. Q.push(in.nextInt());
  70. for(int i=0;i<n;i++)
  71. System.out.println("full():"+Q.full()+" "+"pop():"+Q.pop()+" "+"top():"+Q.top()+" "+"size:"+Q.size()+" "+"empty():"+Q.empty());
  72. }
  73. }

T6

本题代码为T6代码。

输入的第一行是队列里元素个数。
输入的第二行是队列里的元素。

  1. import java.util.*;
  2. public class hw3_6
  3. {
  4. public static class SqQueue//注意如果类里只用一种数据类型,就不用加泛型标志
  5. {
  6. Stack<Integer> S1,S2;
  7. public SqQueue()
  8. {
  9. S1=new Stack<Integer>();
  10. S2=new Stack<Integer>();
  11. }
  12. public boolean empty()
  13. //判断队列是否为空
  14. {
  15. return S1.empty()&&S2.empty();
  16. }
  17. public int size()
  18. //返回队列内元素个数
  19. {
  20. return S1.size()+S2.size();
  21. }
  22. public void push(int x)
  23. //整数x入队操作。只入栈S1。
  24. {
  25. S1.push(x);
  26. }
  27. public int pop()
  28. //出队操作。若队列空则报错。
  29. //接着若S2空了,则将S1内元素全部出栈并入栈S2。
  30. //最后对S2执行一次出栈操作。
  31. {
  32. if(empty()) throw new IllegalArgumentException("The queue is empty!!!");
  33. if(S2.empty())
  34. while(!S1.empty()) S2.push(S1.pop());
  35. return S2.pop();
  36. }
  37. public int top()
  38. //取队首操作。若队列空则输出错误信息并返回-1。
  39. //接下来操作与出队很像,只是最后对S2执行的是取队首操作。
  40. {
  41. if(empty())
  42. {
  43. System.out.println("The queue is empty!There is no top!");
  44. return -1;
  45. }
  46. if(S2.empty())
  47. while(!S1.empty()) S2.push(S1.pop());
  48. return S2.peek();
  49. }
  50. }
  51. public static void main(String args[])
  52. //尝试每一个队列的基本运算并输出结果
  53. {
  54. Scanner in=new Scanner(System.in);
  55. SqQueue Q=new SqQueue();
  56. int n=in.nextInt();
  57. for(int i=0;i<n;i++)
  58. Q.push(in.nextInt());
  59. for(int i=0;i<n;i++)
  60. System.out.println("pop():"+Q.pop()+" "+"top():"+Q.top()+" "+"size:"+Q.size()+" "+"empty():"+Q.empty());
  61. }
  62. }

T7

本题代码为T7代码。

输入的第一行是栈的元素个数。
输入的第二行是栈里的元素。

  1. import java.util.*;
  2. public class hw3_7
  3. {
  4. public static class SqStack
  5. {
  6. Queue<Integer> Q1,Q2;
  7. public SqStack()
  8. {
  9. Q1=new LinkedList<Integer>();//注意Queue初始化的特殊性
  10. Q2=new LinkedList<Integer>();
  11. }
  12. public boolean empty()
  13. //判断栈是否为空
  14. {
  15. return Q1.isEmpty()&&Q2.isEmpty();
  16. }
  17. public int size()
  18. //返回栈内元素个数
  19. {
  20. return Q1.size()+Q2.size();
  21. }
  22. public void push(int x)
  23. //使整数x入栈,若二队列皆空则入队Q1,若队列Q1空则入队列Q2,若队列Q2空则入队列Q1
  24. {
  25. if(Q2.isEmpty()) Q1.offer(x);
  26. else Q2.offer(x);
  27. }
  28. public int pop()
  29. //出栈操作。若栈空则报错。
  30. //否则把非空队列元素依次出队并入队另一队列;当最后一个元素出队后,直接返回,不再入队。
  31. {
  32. int x=0;
  33. if(empty()) throw new IllegalArgumentException("The stack is empty!!!");
  34. if(Q2.isEmpty())
  35. while(!Q1.isEmpty())
  36. {
  37. x=Q1.poll();
  38. if(Q1.isEmpty()) return x;
  39. Q2.offer(x);
  40. }
  41. else
  42. while(!Q2.isEmpty())
  43. {
  44. x=Q2.poll();
  45. if(Q2.isEmpty()) return x;
  46. Q1.offer(x);
  47. }
  48. return x;
  49. }
  50. public int top()
  51. //查询栈顶操作。若栈空,输出错误信息并返回-1;否则先出栈以取出栈顶,再入栈。
  52. {
  53. if(empty())
  54. {
  55. System.out.println("The stack is empty!There is no top!");
  56. return -1;
  57. }
  58. int x=pop();
  59. push(x);
  60. return x;
  61. }
  62. }
  63. public static void main(String args[])
  64. //尝试每一个栈的基本运算并输出结果
  65. {
  66. Scanner in=new Scanner(System.in);
  67. SqStack S=new SqStack();
  68. int n=in.nextInt();
  69. for(int i=0;i<n;i++)
  70. S.push(in.nextInt());
  71. for(int i=0;i<n;i++)
  72. System.out.println("pop():"+S.pop()+" "+"top():"+S.top()+" "+"size:"+S.size()+" "+"empty():"+S.empty());
  73. }
  74. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注