《数据结构编程习题代码.docx》由会员分享,可在线阅读,更多相关《数据结构编程习题代码.docx(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1、 判断是否为回文Import java.util.Scanner;/输入字符前缀import java.util.Stack;public class Main public static void main(String args) Scanner in = new Scanner(8ystem.in);(isSymmetrical(in.next();文字输入)private static Boolean isSymmetrical(String s) 仓U建布尔值前缀Stack stackl = new Stack();/fiJtt 1Stack stack2 = new Stack
2、。;仓ll 建栈 2char vaules = s.toCharArray。;/创建一个数组int length = 0;/计数器for (inti = O;i vaules.length; i+)(stackl .push(vaulesi);/将数组中的数据入栈1length+;计算此栈的长度)boolean answer = true;仓U建布尔值int z = length % 2;/字符长度取余数,并记Zif (z = 0) 即当字符串长度为偶数时(for (int x = 0; x (length / 2); x+)(stack2.push(stack1.pop();/将栈 1 中的
3、数据出栈,并入栈至栈2) else if亿=1) 如果字符串长度为奇数for (int x = 0; x (length / 2); x+)(stack2.push(stack1.pop();/将栈 1 一半数据出栈,并入栈2)stack1.pop();/将乘馀的栈1中的数据出栈)while (!stack1.isEmpty()如果栈 1 不为空(if (stackl .pop().equals(stack2.pop()/Stt 1 数据等于栈2数据answer = true;贝ij输出 “True”else(answer = false;否则输出 “False”)return answer;
4、/输出答案2、 进制转换:十进制转为二进制import java.util.Scanner;/输入数据前缀import java.utiLStack;public class Main public static void main(String args) Scannersc = new Scanner(System.in);/输入数据前缀int i = sc.nextlnt。;/输入数字前缀Stack n = new Stackvlnteger();创建一个栈String str = 申请一个空间while (i 0) 当输入的数字0时n.push(i%2);将此数字除2后的余数入栈 i/
5、2;并将商继续除2,循环)while (!n.isEmpty() 如果栈不为空str = str + n.pop();则将栈中数据出栈,并存 在“str”中)System.out.println(str);/输出答案)3、 约瑟夫生死游戏:package Test;import java . util . Scanner;publicclassjosephl publicstaticvoid main( String args) Scannersc= newScanner( System . in);int n/ k;n= sc . nextlnt();k= sc . nextlnt();ki
6、ll ( n k);)privatestaticvoid kill( int n, int k)if (n0)(System . out . print( 0 n);)elseNode head = newNode(l);CircularListList= newCircularList( head)Node p = head;for (int i =2;i = n;i+ + )(p = List . insertAfter ( p, i);)p setNext( head);int count = 0;Node q = head;while ( count! = n/ 2 ) for (in
7、t i= l;i = k - l;i+ + )q= List . remove( q);count+ +;)List . print();)class CircularList private Node head;public CircularList( Node head) super ();this . head = head;head . setNext( head);)public CircularList( ) public Node getHead()returnhead;)publicvoid setHead( Node head)this . headhead;)publicv
8、oid print( ) Node p = head;doSystem . out . print( p . getData();p= p . getNext();if ( p! = head)System . out . print( ); while ( p! = head);)public Node insertAfter( Node p, Object data) Node newNode= newNode( data, p . getNext();p . setNext( newNode);return newNode;)public Node remove( Node p) if
9、(p= = head)head= head . getNext();)Node q=head;while ( q! = null& & q . getNext( ) ! = p)q= q . getNext();q . setNext( p . getNext();return p . getNext();)class Node private Object data ;private Node next ;public Node( Object data, Node next) this . data = data;this . next = next;)public Node( Object data) this . data= data;)public Node( ) public Object getData( )returndata;)publicvoid setData( Object data) this . data = data;)public Node getNext()returnnext;)publicvoid setNext( Node next)this . next = next;