數據結構與算法19-迭代器-遞歸

 以用戶交互的形式呈現迭代器的功能,迭代器更加靈活的對鏈表進行增刪改查。java

迭代器上刪除一項後,要將指針放到後一項,不能放在前一項。執行插入方法後,將current指向新插入的鏈結點。this

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Link
{
	public Link next;
	public long ddata;
	public Link(long dd)
	{
		ddata = dd;
	}
	public void displayLink()
	{
		System.out.print(ddata+" ");
	}
}
class LinkList
{
	private Link first;
	public LinkList()
	{
		first = null;
	}
	public Link getFirst()
	{
		return first;
	}
	public void setFirst(Link f)
	{
		first = f;
	}
	public boolean isEmpty()
	{
		return first == null;
	}
	public ListIterator getIterator(){
		return new ListIterator(this);
	}
	public void displayList()
	{
		Link current = first;
		while(current != null)
		{
			current.displayLink();
			current = current.next;
		}
		System.out.println(" ");
	}
}
class ListIterator
{
	private Link current;
	private Link previous;
	private LinkList ourList;
	public ListIterator(LinkList list)
	{
		ourList = list;
		reset();
	}
	public void reset()
	{
		current = ourList.getFirst();
		previous = null;
	}
	public boolean atEnd()
	{
		return (current.next==null);
	}
	public void nextLink()
	{
		previous = current;
		current = current.next;
	}
	public Link getCurrent()
	{
		return current;
	}
	public void insertAfter(long dd)
	{
		Link newLink = new Link(dd);
		if(ourList.isEmpty())
		{
			ourList.setFirst(newLink);
			current = newLink;
		}
		else
		{
			newLink.next = current.next;
			current.next = newLink;
			nextLink();
		}
	}
	public void insertBefore(long dd)
	{
		Link newLink = new Link(dd);
		if(previous == null)
		{
			newLink.next = ourList.getFirst();
			ourList.setFirst(newLink);
			reset();
		}
		else
		{
			newLink.next = previous.next;
			previous.next = newLink;
			current = newLink;
		}
	}
	public long deleteCurrent()
	{
		long value = current.ddata;
		if(previous == null)
		{
			ourList.setFirst(current.next);
			reset();
		}
		else
		{
			previous.next = current.next;
			if(atEnd())
				reset();
			else
				current = current.next;
		}
		return value;
		
	}
}
class InterItarApp
{
public static void main(String[] args) throws IOException
{
	LinkList theList = new LinkList();
	ListIterator iter1 = theList.getIterator();
	long value;
	iter1.insertAfter(20);
	iter1.insertAfter(40);
	iter1.insertAfter(80);
	iter1.insertBefore(60);
	while(true)
	{
		System.out.print("enter first letter of show,reset, ");
		System.out.print("next,get,before,after,delete:");
		System.out.flush();
		int choice = getChar();
		switch(choice)
		{
		case 's':
			if(!theList.isEmpty())
				theList.displayList();
			else
				System.out.println("list is empty");
			break;
		case 'r':
			iter1.reset();
			break;
		case 'n':
			if(!theList.isEmpty()&&!iter1.atEnd())
				iter1.nextLink();
			else
				System.out.println("can't go to next link");
			break;
		case 'g':
			if(!theList.isEmpty())
			{
				value = iter1.getCurrent().ddata;
				System.out.println("returned "+value);
			}
			else
				System.out.println("list is empty");
			break;
		case 'b':
			System.out.print("enter value to insert: ");
			System.out.flush();
			value = getInt();
			iter1.insertBefore(value);
			break;
		case 'a':
			System.out.print("enter value to insert: ");
			System.out.flush();
			value = getInt();
			iter1.insertAfter(value);
			break;
		case 'd':
			if(!theList.isEmpty())
			{
				value = iter1.deleteCurrent();
				System.out.println("deleted "+ value);
			}
			else
				System.out.println("can't delete");
			default:
				System.out.println("invalid entry");
			
		}
	}
	
}
private static long getInt() throws IOException {
	String s = getString();
	return Integer.parseInt(s);
}
private static String getString() throws IOException{
	InputStreamReader isr = new InputStreamReader(System.in);
	BufferedReader br = new BufferedReader(isr);
	String s = br.readLine();
	return s;
}
private static int getChar() throws IOException {
	String s = getString();
	return s.charAt(0);
}

}
enter first letter of show,reset, next,get,before,after,delete:s
20 40 60 80  
enter first letter of show,reset, next,get,before,after,delete:r
enter first letter of show,reset, next,get,before,after,delete:n
enter first letter of show,reset, next,get,before,after,delete:n
enter first letter of show,reset, next,get,before,after,delete:g
returned 60
enter first letter of show,reset, next,get,before,after,delete:b
enter value to insert: 55
enter first letter of show,reset, next,get,before,after,delete:a
enter value to insert: 77
enter first letter of show,reset, next,get,before,after,delete:s
20 40 55 77 60 80  
enter first letter of show,reset, next,get,before,after,delete:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class TriangleApp
{
	static int theNumber;
	public static void main(String[] args) throws IOException
	{
		System.out.print("enter a number: ");
		theNumber = getInt();//將getInt獲得的s賦值給theNumber
		int theAnswer = triangle(theNumber);//將triangle中獲得的theNumber賦值給theAnswer
		System.out.println("Triangle=" + theAnswer);//輸出theAnswer
	}
	public static int triangle(int n)
	{
		if(n==1)                //若是n=1
			return 1;           //返回1
		else                          
			return(n+triangle(n-1));    //不然返回n+triangle的n-1的結果
	}
	
	public static int getInt() throws IOException
	{
		String s = getString();           //getString的值賦值給s
		return Integer.parseInt(s);        //返回給s
	}
	public static String getString() throws IOException
	{
		InputStreamReader isr = new InputStreamReader(System.in);   //創建輸入的isr
		BufferedReader br = new BufferedReader(isr);                //將isr輸入br中
		String s = br.readLine();                                   //將br輸入s
		return s;                                                   //返回s
	}
}
enter a number: 555
Triangle=154290

調用遞歸方法,本身調用本身指針

如下爲詳細過程code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class TriangleApp
{
	static int theNumber;
	public static void main(String[] args) throws IOException
	{
		System.out.print("enter a number: ");
		theNumber = getInt();
		int theAnswer = triangle(theNumber);
		System.out.println("Triangle=" + theAnswer);
	}
	public static int triangle(int n)
	{
		System.out.println("enter:n=" + n);
		if(n==1)
		{
			System.out.println("returning 1");
			return 1;
		}
		else
		{
			int temp = n + triangle(n-1);
			System.out.println("returning "+temp);
			return temp;
		}
	}
	
	public static int getInt() throws IOException
	{
		String s = getString();
		return Integer.parseInt(s);
	}
	public static String getString() throws IOException
	{
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String s = br.readLine();
		return s;
	}
}

 triangle()方法調用自身時,它的參數從10開始,每次減一,這個發那個發反覆進入自身,直到方法的參數減少到1,因而方法返回,這會引起一系列的返回序列。這個方法向鳳凰涅盤同樣的返回,脫離被放棄的那一層,每當返回時,這個方法把調用它的參數n與其調用下一層方法的返回值相加。遞歸

注意:在最內層返回1以前,實際上在同一時刻有10個不一樣的triangle()方法實例存在,最外層傳入的參數是10,最內層傳入的參數是1.get

enter a number: 10
enter:n=10
enter:n=9
enter:n=8
enter:n=7
enter:n=6
enter:n=5
enter:n=4
enter:n=3
enter:n=2
enter:n=1
returning 1
returning 3
returning 6
returning 10
returning 15
returning 21
returning 28
returning 36
returning 45
returning 55
Triangle=55

基於循環的方法效率更高input

變位數it

輸入一個單詞,將字母進行排列,將全部結果都排列組合出來;io

利用遞歸,從第一個單詞開始向右移動,每兩個字母換一次位置,直到將全部可能都列出。class

doAnagram()每次調用本身的時候,詞的大小都減小一個字母,而且開始i的位置向右移動一位。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class AnagramApp {
	static int size;
	static int count;
	static char[] arrChar = new char[100];
	public static void main(String[] args) throws IOException
	{
		System.out.print("enter a word: ");
		String input = getString();
		size = input.length();
		count = 0;
		for(int j=0;j<size;j++)
		arrChar[j] = input.charAt(j);
		doAnagram(size);
	}
	private static void doAnagram(int newSize) {
		// TODO Auto-generated method stub
		if(newSize==1)
			return;
		for(int j=0;j<newSize;j++)
		{
			doAnagram(newSize-1);
			if(newSize==2)
				displayWord();
			rotate(newSize);
		}
	}
	private static void rotate(int newSize) {
		// TODO Auto-generated method stub
		int j;
		int position = size - newSize;
		char temp = arrChar[position];
		for(j=position+1;j<size;j++)
		{
			arrChar[j-1] = arrChar[j];
			arrChar[j] = temp;
		}
	}
	private static void displayWord() {
		// TODO Auto-generated method stub
		if(count < 99)
			System.out.print(" ");
		if(count < 9)
			System.out.print(" ");
		System.out.print(++count+" ");
		for(int j=0;j<size;j++)
			System.out.print(arrChar[j]);
		System.out.print(" ");
		System.out.flush();
		if(count%6==0)
			System.out.println("");
	}
	private static String getString() throws IOException {
		// TODO Auto-generated method stub
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String s = br.readLine();
		return s;
	}
}
enter a word: dog
  1 dog   2 dgo   3 ogd   4 odg   5 gdo   6 god
相關文章
相關標籤/搜索