[個人記錄]小白書學習第6章數據結構基礎算法競賽入門經典第一版)




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();
		
		//二叉樹有空再補
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章