线性表的链式存储结构
来源:互联网 发布:一起牛软件下载 编辑:程序博客网 时间:2024/06/15 22:37
前言
顺序存储结构的缺点:插入和删除时需要移动大量元素
链式存储结构的特点:用一组任意的存储单元存储线性表的数据元素
思路
1、定义接口
package com.stucture.list; /** * 线性表顺序存储结构的接口 * 指的是用一段地址连续的存储单元一次存储线性表的数据元素 * @ClassName: ISqList */ public interface IList<T> { /** * 获得元素 * @param loc 需要获得的第loc个元素 * @return */ public T getElem(int loc); /** * 插入元素 * @param loc 元素的插入位置 * @param t 需要插入的元素 * @return 是否成功插入 */ public boolean insertElem(int loc, T t); /** * 删除元素 * @param i 需要删除元素的位置 * @return */ public T deleteElem(int i); }
2、定义节点类
package com.stucture.list.linkList; /** * 链表中的结点 * @ClassName: Node * @author 小学徒 * @date 2013-2-27 */ public class Node<T> { private T data; //需要存储的数据信息 private Node<T> next; //后继结点 public T getData() { return data; } public void setData(T data) { this.data = data; } public Node<T> getNext() { return next; } public void setNext(Node<T> next) { this.next = next; } }
3、实现链表和基本操作
package com.stucture.list.linkList; import com.stucture.list.IList; /** * 单链表 * @ClassName: LinkList */ public class LinkList<T> implements IList<T>{ private Node<T> head; //链表的结点 private int length; //链表的长度 public LinkList(Node<T> head) { this.head = head; } //获取元素 public T getElem(int loc) { int j = 1; //计数器 Node<T> n = head; //指向第一个结点 while(n != null) { //n不为空时,循环继续寻找第loc个结点 if(j == loc) { //找到第一个元素时返回 return n.getData(); } n = n.getNext(); j++; } return null; } //插入元素 public boolean insertElem(int loc, T t) { if(length + 1 < loc) { System.out.println("非法插入"); return false; } if(head == null && loc == 1) { //当第一次插入的时候 head = new Node<T>(); //第一次使用,必须创建对象 head.setData(t); length++; } else if(head != null && loc == 1) { //但不是第一次插入,但是插入的位置是第一个时 Node<T> tempNode = new Node<T>(); //生成一个新的结点 tempNode.setData(t); tempNode.setNext(head); head = tempNode; //把头换成新插入的结点 length++; } else { //当不是第一次插入并且插入的不是第一个时 Node<T> n = this.head; int j = 1; //计数器 while(n != null && j < loc - 1) { n = n.getNext(); j++; } Node<T> tempNode = new Node<T>(); //生成一个新的结点 tempNode.setData(t); tempNode.setNext(n.getNext()); //将n的后继结点赋值给新的结点的后继 n.setNext(tempNode); length++; } return true; } //删除元素 public T deleteElem(int loc) { if(head == null || loc > length) { System.out.println("非法删除"); return null; } T old; if(head != null && loc == 1) { old = head.getData(); head = head.getNext(); } else { Node<T> n = this.head; int j = 1; //计数器 while(n != null && j < loc - 1) { n = n.getNext(); j++; } old = n.getNext().getData(); n.setNext(n.getNext().getNext()); } length--; return old; } public Node<T> getHead() { return head; } public void setHead(Node<T> head) { this.head = head; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } }
参考
http://hjj20040849.iteye.com/blog/1816941
线性表的顺序存储结构:
http://blog.csdn.net/iamcgt/article/details/78622878
阅读全文
0 0
- 线性表的链式存储结构
- 链式存储结构的线性表
- 线性表的链式存储结构
- 【数据结构】线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- 【二】线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构(三)
- 【数据结构】-线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- 线性表的链式存储结构
- Arduino构建与解析Json字符串实例
- CF C. Rumor
- 强推 | Google&微软数据科学家们给你讲Keras实战
- 微信小程序电商实战-首页(上)
- Item 8:别让异常逃离析构函数【effective C++读书笔记】
- 线性表的链式存储结构
- QtableWidget单元格设置和操作
- Java异常
- kubernetes Pull Image 鉴权过程
- dubbo+zookeeper安装配置
- CentOS 7.0 进入单用户模式修改Root密码
- Windows安全 无法打开这些文件
- vim中设置tab键的宽度
- 结构体类型-枚举类型-联合体类型