[个人记录]小白书学习第6章数据结构基础算法竞赛入门经典第一版)
2018-03-14 21:59
676 查看
6.1.1卡片游戏(队列)
6.1.2铁轨(栈)
6.2.1移动小球(双向链表数据结构参考网上代码瞎改而成)
package six;
public class sxList{
public class Node{
int value;//值
Node pre;//前一个节点
Node next;//下一个节点
public Node() {
}
public Node(int value) {
this.value=value;
}
public Node(int value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
Node header;//头节点
Node tail;//尾节点
int size;
public sxList(){
this.header=null;
this.tail=null;
this.size=0;
}
//以指定元素建立链表
public sxList(int value) {
header=new Node(value,null,null);
tail=header;
size++;
}
//插入,尾插法
public void add(int value) {
if(header==null) {
header=new Node(value,null,null);
// 只有一个节点,header、tail都指向该节点
tail=header;
}
else {
// 创建新节点,新节点的pre指向原tail节点
Node n=new Node(value,tail,null);
tail.next=n;
tail=n;
}
size++;
}
// 查找链式线性表中指定元素,依据value找Node
public Node locate(int value) {
Node temp=header;
while(temp.getValue()!=value){
temp=temp.getNext();
}
return temp;
}
//打印
public void p() {
if(header!=null) {
Node t=header;
while(t!=null) {
System.out.print(t.getValue());
t=t.getNext();
}
}
}
public Node getHeader() {
return header;
}
public void setHeader(Node header) {
this.header = header;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
}
package six;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
import six.sxList.Node;
public class six_s {
public static void main(String[] args) {
/**
* 第6章 数据结构基础
*/
//6.1.2铁轨(栈)
/*Scanner sc=new Scanner(System.in);
Stack<Integer> s=new Stack<Integer>();
while(sc.hasNext()) {
int n=sc.nextInt();
int A[]=new int[n+1];
int a=1,b=1;//a列车号
int ok=1;
for(int i=1;i<=n;i++)
A[i]=sc.nextInt();
while(b<=n) {
if(a==A[b]) {
a++;b++;
}else if(!s.isEmpty()&&s.peek()==A[b]) {//peek()查看堆栈顶部的对象,但不从堆栈中移除它。
b++;s.pop();
}else if(a<=n) {
s.push(a);
a++;
}else {
ok=0;break;
}
}
System.out.printf("%s\n",ok==1?"yes":"no");
}
*/
//6.1.1卡片游戏(队列)
/*Scanner sc1=new Scanner(System.in);
int n=sc1.nextInt();
LinkedList<Integer> list=new LinkedList<Integer>();
for(int i=1;i<n+1;i++) {
list.add(i);
}
while(n>0){
int f=list.remove();
System.out.print(f+" ");
n--;
list.addLast(list.poll());
}*/
//6.2.1移动小球(双向链表参考网上自己修改而成)
sxList sx=new sxList();
Scanner sc2=new Scanner(System.in);
int n=sc2.nextInt();
int m=sc2.nextInt();
for(int i=1;i<n+1;i++){
sx.add(i);
}
for(int j=0;j<m;j++) {
String a=sc2.next();
int x=sc2.nextInt();
int y=sc2.nextInt();
//以下if里的部分可封装成方法,自己写的不懂考虑完没有
if(a.equals("A")) {
Node t=sx.locate(x);
Node t1=t.getPre();
Node t4=t.getNext();
if(t1==null) {//如果x是header
t4.setPre(null);
sx.setHeader(t4);
}
else {
t1.setNext(t4);
t4.setPre(t1);
}
Node t2=sx.locate(y);
Node t3=t2.getPre();
if(t3==null) {//如果y是header
t.setPre(null);
sx.setHeader(t);
}
else {
t.setPre(t3);
t3.setNext(t);
}
t.setNext(t2);
t2.setPre(t);
}
else {
Node t=sx.locate(x);//x的位置
Node t1=sx.locate(y);//y的位置
Node t2=t.getPre();
Node t3=t.getNext();
if(t2==null) {//如果x是header
t3.setPre(null);
sx.setHeader(t3);
}else if(t3==null) {//如果x是tail
t2.setNext(null);
sx.setTail(t2);
}
else {
t2.setNext(t3);
t3.setPre(t2);
}
Node t4=t1.getNext();
if(t4==null) {//如果y是tail
t.setNext(null);
sx.setTail(t);
}
else {
t.setNext(t4);
t4.setPre(t);
}
t.setPre(t1);
t1.setNext(t);
}
}
sx.p();
//二叉树有空再补
}
}
6.1.1卡片游戏(队列)
6.1.2铁轨(栈)
6.2.1移动小球(双向链表数据结构参考网上代码瞎改而成)
package six;
public class sxList{
public class Node{
int value;//值
Node pre;//前一个节点
Node next;//下一个节点
public Node() {
}
public Node(int value) {
this.value=value;
}
public Node(int value,Node pre,Node next) {
this.value=value;
this.pre=pre;
this.next=next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
Node header;//头节点
Node tail;//尾节点
int size;
public sxList(){
this.header=null;
this.tail=null;
this.size=0;
}
//以指定元素建立链表
public sxList(int value) {
header=new Node(value,null,null);
tail=header;
size++;
}
//插入,尾插法
public void add(int value) {
if(header==null) {
header=new Node(value,null,null);
// 只有一个节点,header、tail都指向该节点
tail=header;
}
else {
// 创建新节点,新节点的pre指向原tail节点
Node n=new Node(value,tail,null);
tail.next=n;
tail=n;
}
size++;
}
// 查找链式线性表中指定元素,依据value找Node
public Node locate(int value) {
Node temp=header;
while(temp.getValue()!=value){
temp=temp.getNext();
}
return temp;
}
//打印
public void p() {
if(header!=null) {
Node t=header;
while(t!=null) {
System.out.print(t.getValue());
t=t.getNext();
}
}
}
public Node getHeader() {
return header;
}
public void setHeader(Node header) {
this.header = header;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
}
package six;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
import six.sxList.Node;
public class six_s {
public static void main(String[] args) {
/**
* 第6章 数据结构基础
*/
//6.1.2铁轨(栈)
/*Scanner sc=new Scanner(System.in);
Stack<Integer> s=new Stack<Integer>();
while(sc.hasNext()) {
int n=sc.nextInt();
int A[]=new int[n+1];
int a=1,b=1;//a列车号
int ok=1;
for(int i=1;i<=n;i++)
A[i]=sc.nextInt();
while(b<=n) {
if(a==A[b]) {
a++;b++;
}else if(!s.isEmpty()&&s.peek()==A[b]) {//peek()查看堆栈顶部的对象,但不从堆栈中移除它。
b++;s.pop();
}else if(a<=n) {
s.push(a);
a++;
}else {
ok=0;break;
}
}
System.out.printf("%s\n",ok==1?"yes":"no");
}
*/
//6.1.1卡片游戏(队列)
/*Scanner sc1=new Scanner(System.in);
int n=sc1.nextInt();
LinkedList<Integer> list=new LinkedList<Integer>();
for(int i=1;i<n+1;i++) {
list.add(i);
}
while(n>0){
int f=list.remove();
System.out.print(f+" ");
n--;
list.addLast(list.poll());
}*/
//6.2.1移动小球(双向链表参考网上自己修改而成)
sxList sx=new sxList();
Scanner sc2=new Scanner(System.in);
int n=sc2.nextInt();
int m=sc2.nextInt();
for(int i=1;i<n+1;i++){
sx.add(i);
}
for(int j=0;j<m;j++) {
String a=sc2.next();
int x=sc2.nextInt();
int y=sc2.nextInt();
//以下if里的部分可封装成方法,自己写的不懂考虑完没有
if(a.equals("A")) {
Node t=sx.locate(x);
Node t1=t.getPre();
Node t4=t.getNext();
if(t1==null) {//如果x是header
t4.setPre(null);
sx.setHeader(t4);
}
else {
t1.setNext(t4);
t4.setPre(t1);
}
Node t2=sx.locate(y);
Node t3=t2.getPre();
if(t3==null) {//如果y是header
t.setPre(null);
sx.setHeader(t);
}
else {
t.setPre(t3);
t3.setNext(t);
}
t.setNext(t2);
t2.setPre(t);
}
else {
Node t=sx.locate(x);//x的位置
Node t1=sx.locate(y);//y的位置
Node t2=t.getPre();
Node t3=t.getNext();
if(t2==null) {//如果x是header
t3.setPre(null);
sx.setHeader(t3);
}else if(t3==null) {//如果x是tail
t2.setNext(null);
sx.setTail(t2);
}
else {
t2.setNext(t3);
t3.setPre(t2);
}
Node t4=t1.getNext();
if(t4==null) {//如果y是tail
t.setNext(null);
sx.setTail(t);
}
else {
t.setNext(t4);
t4.setPre(t);
}
t.setPre(t1);
t1.setNext(t);
}
}
sx.p();
//二叉树有空再补
}
}
相关文章推荐
- 【索引】算法竞赛入门经典-第6章 数据结构基础
- 算法竞赛入门经典:第六章 数据结构基础 6.9 根据二叉树的后序和中序确定前序序列
- 关于 算法竞赛入门经典 的一些学习经历与体会
- 算法竞赛入门经典第七章暴力求解法——枚举排列(记录向)
- 算法竞赛入门经典:第六章 数据结构基础 6.2铁轨
- [个人记录]小白书学习2.4习题(算法竞赛入门经典第一版)
- 算法竞赛入门经典:第六章 数据结构基础 6.3移动小球
- 算法竞赛入门经典:第五章 基础题目选解 5.3 周期串
- 算法竞赛入门经典 第三章 学习笔记
- 【算法零基础入门】 学习笔记一 什么是数据结构
- [个人记录]小白书学习习题3-5~3-7(算法竞赛入门经典第一版)
- 关于算法竞赛入门经典一书的思考学习——枚举排序和子集生成!
- 数据结构入门学习系列-8(栈的经典算法用例)
- 算法竞赛入门经典:第六章 数据结构基础 6.10黑白图像
- 算法竞赛入门经典:第五章 基础题目选解 5.11果园里的树
- 【索引】算法竞赛入门经典-第5章 基础题目选解
- 算法竞赛入门经典:第六章 数据结构基础 6.5小球下落
- 算法竞赛入门经典:第六章 数据结构基础 6.12迷宫路径
- 算法竞赛入门经典:第五章 基础题目选解 5.9 Cantor数
- 【算法竞赛入门经典第二版学习】第三章习题