堆和栈的概念
栈是一种具有后进先出性质的数据结构,就如同箱子一样
堆是一种经过排序的树形数据结构,每个节点都有一个值,通常我们所说的堆的数据结构,是指二叉堆,对的特点是根节点的值最小/最大,且根结点的两个子树也是一个堆
内存中的栈和堆
- 栈:由操作系统进行自动分配和释放,存放的是对象的引用,以及局部变量等
- 堆:一般由程序员分配释放,存放的是实例对象以及成员变量的值等
-
堆栈的优缺点
- 栈的优势,读取速度比堆快,仅次于直接位于CPU中的寄存器,但是缺点是,存在栈中的数据大小与生命存起必须是确定的,缺乏灵活性,另外,栈数据可以共享
- 堆的优势是可以动态的分配内存大小,生存期也不需要预先告诉编译器,Java的垃圾收集器会自动收走这些不在使用的数据,但是缺点是,由于运行时动态内存分配,存取速度较慢
链表
链表是一种递归的数据结构,他或者为null,或者是指向一个接点的引用,该结点含有一个泛型的元素和一个指向另一条链表的引用。
单向链表
一种链式存取的数据结构,单链表中的数据是以结点的形式存在,每一个结点是由数据元素和下一个结点的存储的位置组成。单链表与数组相比的最大差别是:单链表的数据元素存放在内存空间的地址是不连续的,而数组的数据元素存放的地址在内存空间中是连续的。
package Linked;
//单向链表
public class LinkedList<T> {
}
class Node<T>{
public T data;
public Node next;
public Node() {
}
public Node(T data) {
this.data = data;
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
添加节点
/**
* 添加节点
* @param data 节点数据
* @param index 节点的位置
* @throws IllegalAccessException
*/
public void add(T data,int index) throws IllegalAccessException {
if (index < 0 ){
throw new IllegalAccessException("index is error");
}
if (index == 0){
Node node = new Node(data);
node.next = this.head;
this.head = node;
this.size++;
return;
}
Node prenode = this.head;
for (int i =0;i<index-1;i++){
prenode= prenode.next;
}
Node node = new Node(data);
if (prenode.next != null){
node.next = prenode.next;
prenode.next = node;
this.size++;
return;
}else {
prenode.next = node;
this.size++;
return;
}
}
显示链表
/**
* 展示链表
*/
public void showlist(){
if (head == null) return;
Node temp = head;
while (temp != null){
System.out.println(temp);
temp = temp.next;
}
}
删除节点
/**
* 删除节点
* @param index
* @throws IllegalAccessException
*/
public void delete(int index) throws IllegalAccessException {
if (head == null) {
System.out.println("链表为空");
return;
}
if (index <0 || index > size)
throw new IllegalAccessException("index is erroe");
Node temp = head;
if (index == 0){
head = head.next;
this.size--;
}
for (int i =0; i< index-1;i++){
temp = temp.next;
}
temp.next = temp.next.next;
this.size--;
}
更新节点
/**
* 更新结点
* @param data
* @param index
* @throws IllegalAccessException
*/
public void updata(T data,int index) throws IllegalAccessException {
if (index < 0 || index > size) throw new IllegalAccessException("index is error");
if (head == null){
System.out.println("链表为空");
return;
}
Node temp = head;
Node temp1 = new Node(data);
for (int i = 0; i< index; i++){
temp = temp.next;
}
temp.data = temp1.data;
}
获取节点总数
/**
* 获取节点总数
* @return
*/
public int getSize(){
return size;
}
查找倒数第n个节点
/**
* 查找倒数第n个节点
* @param index
* @return
* @throws IllegalAccessException
*/
public Node<T> findnode(int index) throws IllegalAccessException {
if (index < 0 || index > size)
throw new IllegalAccessException("index is error");
Node temp = head;
for (int i =0; i< size-index+1;i++){
temp = temp.next;
}
return temp;
}
反转链表
/**
* 反转链表
*/
public void reverseList(){
if(head == null || head.next == null)
return;
Node cur = head; //当前节点
Node next = null; //下一个节点
Node reversenode = null;
while (cur != null){
next = cur.next;//next指向cur的下一个节点
cur.next = reversenode;//断开
reversenode = cur;//形成回路
cur = next;//继续
}
head = reversenode;
}
合并有序链表
/**
* 合并有序链表
* @param head1
* @param head2
* @return
*/
public static Node merge(Node head1,Node head2){
if (head1 == null && head2 == null) return null;
if (head1 == null)
return head2;
if (head2 == null)
return head1;
Node temp = null;
if((int)head1.data <=(int) head2.data){
temp = head1;
temp.next = merge(head1.next,head2);
}else {
temp = head2;
temp.next = merge(head1,head2.next);
}
return temp;
}
完整的代码
package Linked;
//单向链表
public class LinkedList<T> {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
LinkedList linkedList1 = new LinkedList();
LinkedList linkedList2 = new LinkedList();
try {
linkedList.add(1,0);
linkedList.add(2,1);
linkedList.add(3,1);
linkedList.add(4,3);
linkedList.showlist();
System.out.println("---------------");
linkedList.delete(0);
linkedList.showlist();
System.out.println("---------------");
linkedList.updata(22,0);
linkedList.getSize();
linkedList.showlist();
System.out.println("---------------");
System.out.println(linkedList.findnode(1));
System.out.println("---------------");
linkedList.reverseList();
linkedList.showlist();
System.out.println("---------------");
linkedList1.add(5,0);
linkedList1.add(6,1);
linkedList1.add(7,2);
linkedList1.add(8,3);
linkedList2.add(1,0);
linkedList2.add(2,1);
linkedList2.add(3,2);
linkedList2.add(4,3);
Node head1 = linkedList1.head;
Node head2 = linkedList2.head;
Node mergenode = merge(head1,head2);
while(mergenode!=null){
System.out.println(mergenode.data);
mergenode = mergenode.next;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private int size;
private Node head;
/**
* 添加节点
* @param data 节点数据
* @param index 节点的位置
* @throws IllegalAccessException
*/
public void add(T data,int index) throws IllegalAccessException {
if (index < 0 ){
throw new IllegalAccessException("index is error");
}
if (index == 0){
Node node = new Node(data);
node.next = this.head;
this.head = node;
this.size++;
return;
}
Node prenode = this.head;
for (int i =0;i<index-1;i++){
prenode= prenode.next;
}
Node node = new Node(data);
if (prenode.next != null){
node.next = prenode.next;
prenode.next = node;
this.size++;
return;
}else {
prenode.next = node;
this.size++;
return;
}
}
/**
* 展示链表
*/
public void showlist(){
if (head == null) return;
Node temp = head;
while (temp != null){
System.out.println(temp);
temp = temp.next;
}
}
/**
* 删除结点
* @param index
* @throws IllegalAccessException
*/
public void delete(int index) throws IllegalAccessException {
if (head == null) {
System.out.println("链表为空");
return;
}
if (index <0 || index > size)
throw new IllegalAccessException("index is erroe");
Node temp = head;
if (index == 0){
head = head.next;
this.size--;
}
for (int i =0; i< index-1;i++){
temp = temp.next;
}
temp.next = temp.next.next;
this.size--;
}
/**
* 更新结点
* @param data
* @param index
* @throws IllegalAccessException
*/
public void updata(T data,int index) throws IllegalAccessException {
if (index < 0 || index > size) throw new IllegalAccessException("index is error");
if (head == null){
System.out.println("链表为空");
return;
}
Node temp = head;
Node temp1 = new Node(data);
for (int i = 0; i< index; i++){
temp = temp.next;
}
temp.data = temp1.data;
}
/**
* 获取节点总数
* @return
*/
public int getSize(){
return size;
}
/**
* 查找倒数第n个节点
* @param index
* @return
* @throws IllegalAccessException
*/
public Node<T> findnode(int index) throws IllegalAccessException {
if (index < 0 || index > size)
throw new IllegalAccessException("index is error");
Node temp = head;
for (int i =0; i< size-index+1;i++){
temp = temp.next;
}
return temp;
}
/**
* 反转链表
*/
public void reverseList(){
if(head == null || head.next == null)
return;
Node cur = head; //当前节点
Node next = null; //下一个节点
Node reversenode = null;
while (cur != null){
next = cur.next;//next指向cur的下一个节点
cur.next = reversenode;//断开
reversenode = cur;//形成回路
cur = next;//继续
}
head = reversenode;
}
/**
* 合并有序链表
* @param head1
* @param head2
* @return
*/
public static Node merge(Node head1,Node head2){
if (head1 == null && head2 == null) return null;
if (head1 == null)
return head2;
if (head2 == null)
return head1;
Node temp = null;
if((int)head1.data <=(int) head2.data){
temp = head1;
temp.next = merge(head1.next,head2);
}else {
temp = head2;
temp.next = merge(head1,head2.next);
}
return temp;
}
}
class Node<T>{
public T data;
public Node next;
public Node() {
}
public Node(T data) {
this.data = data;
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
'}';
}
}
p.next = merge(head1,head2.next);
}
return temp;
}
}
class Node{
public T data;
public Node next;
public Node() {
}
public Node(T data) {
this.data = data;
}
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
'}';
}
}