⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applet1.java

📁 学习数据结构的最好辅助工具,快速帮助你熟悉数据结构的相关技术
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import Demo;
import GoThread;

public class Applet1 extends Applet
{
	private String sourceCode[] = {
		"Status ListInsert_L(LinkList L, int pos, ElemType e)",	// 0
		"{",													// 1
		"  p = L; j = 0;",										// 2
		"  while (p && j < pos)",								// 3
		"  {",													// 4
        "    p = p->next;",										// 5
        "    ++j;",												// 6
        "  }",													// 7
		"  if (!p || j > pos-1)",								// 8
		"    return ERROR;",									// 9
		"  s = new LNode;",									// 10
		"  s->data = e;",										// 11
		"  s->next = p->next;",									// 12
		"  p->next = s;",										// 13
		"  return OK;",											// 14
		"}" };													// 15
	private int m_pos;
	private char m_e = 'A';
	private char m_data[] = {'a', 'b', 'c', 'd', 'e', 'f'};
	private int m_numNodes = 6;
	public int m_step = 0;
	private int m_numLines = 16;
	private GoThread go;
	public boolean m_paused = true;

	// Virtual Machine and its process
	class CVirtualMachine
	{
		int p, j, s;
	};
	private CVirtualMachine m_VM;
	
	public int proceed()
	{
		switch(m_step)
		{
		case 0:
			m_pos = cPos.getSelectedIndex()+1;
			setData(tData.getText());
			m_e = tE.getText().charAt(0);
			m_LLDemo.reset(m_numNodes);
			return 2;
		case 2:
			m_VM.p = 0;
			m_LLDemo.addPointer('p');
			m_VM.j = 0;
			return 3;
		case 3:
			if( m_VM.p != Demo.NullPointer  && m_VM.j < m_pos-1 )
				return 5;
			return 8;
		case 5:
			m_VM.p = m_LLDemo.movePointer('p');
			return 6;
		case 6:
			m_VM.j ++;
			return 3;
		case 8:
			if( m_VM.p == Demo.NullPointer || m_VM.j > m_pos-1 )
				return 9;
			return 10;
		case 10:
			m_LLDemo.newNode('s');
			return 11;
		case 11:
			m_LLDemo.setData(m_e);
			return 12;
		case 12:
			m_LLDemo.setNodePointer('p');
			return 13;
		case 13:
			m_LLDemo.setPointer('p');
			return 14;
		default:
			return 0;
		}
	}

	private GridBagLayout gbLayout;
	private GridBagConstraints gbConstraints;
	
	private TextArea Source;
	public Button bNext, bReset, bGo;
	private Demo m_LLDemo;
	private TextField tData, tE;
	private Choice cPos;
	private Label l1, l2, l3, l4;
	
	private void addComponent(Component c, int column, int row, int width, int height)
	{
		gbConstraints.gridx = column;
		gbConstraints.gridy = row;
		gbConstraints.gridwidth = width;
		gbConstraints.gridheight = height;
		gbLayout.setConstraints(c, gbConstraints);
		add(c);
	}

	/**
	 * The entry point for the applet. 
	 */
	public void init()
	{
		gbLayout = new GridBagLayout();
		setLayout( gbLayout );
		gbConstraints = new GridBagConstraints();
		
		Source = new TextArea( 20, 50);
		Source.setEditable(false);
		Source.setBackground(Color.white);
		for(int i=0;i<m_numLines;i++) Source.append(sourceCode[i] + "\r\n");
		bNext = new Button("执行下一句");
		bReset = new Button("重置环境");
		bGo = new Button("连续执行");
				
		m_LLDemo = new Demo(m_numNodes, m_data);
		m_LLDemo.setSize(350,200);
		tData = new TextField("abcdef");
		cPos = new Choice();
		tE = new TextField("A");
		for(int i=1;i<7;i++)cPos.add(new Integer(i).toString());
		cPos.select(3);
		l1 = new Label("链表数据", Label.RIGHT);
		l2 = new Label("插入位置", Label.RIGHT);
		l3 = new Label("插入数据", Label.RIGHT);
		l4 = new Label();
		l1.setSize(30, 20);
		l2.setSize(30, 20);
		l3.setSize(20, 20);
		l4.setSize(30, 20);

		addComponent(Source, 0, 0, 3, 3);
		addComponent(m_LLDemo, 3, 0, 3, 1);
		addComponent(l1, 4, 1, 1, 1);
		addComponent(tData, 5, 1, 1, 1);
		addComponent(l4, 3, 2, 1, 1);
		addComponent(l2, 4, 2, 1, 1);
		addComponent(cPos, 5, 2, 1, 1);
		addComponent(l3, 4, 3, 1, 1);
		addComponent(tE, 5, 3, 1, 1);
		addComponent(bNext, 0, 3, 1, 1);
		addComponent(bReset, 1, 3, 1, 1);
		addComponent(bGo, 2, 3, 1, 1);
		
		m_VM = new CVirtualMachine();
		go = new GoThread(this);
		go.start();
	}

	public boolean action(Event p1, Object p2)
	{
		if( p1.target == bNext )
		{
			m_step = proceed();
			showSource();
		}
		else if( p1.target == tData ) setData(p1.arg.toString());
		else if( p1.target == cPos )
		{
			m_pos = new Integer(p2.toString()).intValue();
		}
		else if( p1.target == tE ) m_e = p2.toString().charAt(0);
		else if( p1.target == bReset )
		{
			m_step = 0;
			showSource();
		}
		else if( p1.target == bGo )
		{
			if(m_paused)
			{
				bNext.setEnabled(false);
				bReset.setEnabled(false);
				go.resume();
				bGo.setLabel("暂停执行");
				m_paused = false;
			}
			else
			{
				go.suspend();
				bGo.setLabel("连续执行");
				m_paused = true;
				bNext.setEnabled(true);
				bReset.setEnabled(true);
			}
		}
		return true;
	}
	
	public void showSource()
	{
		int start = 0;
		for(int i=0; i<m_step; i++) start += sourceCode[i].length() + 1;
		Source.select( start, start + sourceCode[m_step].length() );
	}
	
	public void setData(String s)
	{
		m_numNodes = s.length();
		if( m_numNodes>6 ) m_numNodes = 6;
		for(int i=0;i<m_numNodes;i++) m_data[i] = s.charAt(i);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -