## 算法：背包，栈，队列的简单实现，链式结构 原

gaomq

```/**
* Created by GAOMINGQIAN on 2017/6/4.
* 背包的实现
*/
public class Bag<Item> implements Iterable<Item>{
//链表的头结点
private Node first;
public class Node{
Item item;
Node next;
}
Node oldFirst=first;
first=new Node();
first.item=item;
first.next=oldFirst;
}
@Override
public Iterator<Item> iterator() {
}
private  Node currentNode=first;
@Override
public boolean hasNext() {
return currentNode!=null;
}
@Override
public Item next() {
Item item=currentNode.item;
currentNode=currentNode.next;
return item;
}
}
}```

```/**
* Created by GAOMINGQIAN on 2017/6/4.
* 栈的实现
*/
public class Stack<Item> {
//元素的数量
private int N=0;
//栈顶
private Node first = null;
public class Node {
//存放的元素
private Item item;
private Node next;
}
//放元素
public void push(Item item) {
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
N++;
}
public Item pop() {
if(isEmpty()){
throw  new NullPointerException("已经没有元素");
}
Item item = first.item;
first = first.next;
N--;
return item;
}
public boolean isEmpty(){
return N==0;
}
public int size(){
return N;
}
}```

```/**
* Created by GAOMINGQIAN on 2017/6/4.
* 队列的实现
*/
public class Queue<Item> {
//存放的元素个数
private int N = 0;
//头
private Node first;
//尾
private Node last;

public class Node {
Item item;
Node next;
}
public void enqueue(Item item) {
Node oldLast = last;
last = new Node();
last.item = item;
if (isEmpty())
first = last;
else
oldLast.next=last;
N++;
}
public Item dequeue() {
Item item = first.item;
first=first.next;
if (isEmpty()){
last=null;
}
N--;
return item;
}
public boolean isEmpty() {
return N == 0;
}
public int size(){
return N;
}
}```

### gaomq

IAM四十二
2017/10/22
0
0
C 算法与数据结构 队列

2016/03/28
19
0

SkyHive
2017/10/14
0
0

Jonson
2016/04/21
27
0
《大话数据结构》读书笔记(1)

0
0

12分钟前
2
0
Linux如何查看进程、杀死进程、启动进程等常用命令

30分钟前
3
0
ASP.NET Core MVC 静态文件配置

whltian
41分钟前
1
0
linux之自定义命令

48分钟前
7
0
linux基础

50分钟前
7
0