The basis of the list

definition

private class Node
{
	Item item;
	Node next;
}
//构造
Node first = new Node();
Node second = new Node();
Node third = new Node();
//赋值
first.item = to;
second.item = be;
third.item = or;
//指向 third 指向null
first.next = second;
second.next = third;

operating

//表头插入节点
Node oldfirst = first;
first = new Node();
first.item = not;
first.next = oldfirst;
//表头删除节点
first = first.next;
//表尾插入节点
Node oldlast = last;
last = new Node();
last.item = not;
oldlast.next= last;

//其他位置插入节点和删除操作

//遍历数组
for(int i=0; i<n;i++)
{ 处理 a[i];}
//遍历链表
for(Node x =fisrt,; x!=null; x=x.next)
{ 处理 x.item}


Realization stack

//栈的实现(下压)
private Node node;(最近添加的元素)
public void push(Item item)
{	
	Node oldnode = node;
	node = new Node ();
	node.next = oldnode;
	node.item = item;
}
public void pop()
{ Item item = node.item;
  node = node.next;
  return item;
}

//FIFO(先进先出队列)
private Node first;//(栈顶,最早添加的元素)
private Node last;//(尾巴,最近添加的元素)
private int N;//队中元素的数量。
public void enqueue(Item item)
{
	Node oldlast = last;
	last = new Node();
	last.item = item;
	oldlast.next = null;
	if(isEmpty) first = last;
	else oldlast.next = last;
	N++;
}

public void dequeue()
{
	Item item =first.item;
	first = first.next;
	if(isEmpty) last=null;
	N--;
	return item;
}
Published 12 original articles · won praise 0 · Views 901

Guess you like

Origin blog.csdn.net/EPFL_Panda/article/details/100665258