线性表中的顺序表和链表是最基本数据结构,这两种数据结构中最基本的方法便是插入、删除,链表还有定位。要说掌握了数据结构的线性表,那么你必须能够随时写出线性表的实现,这样才算掌握。线性表看着简单,但是要真正掌握,还是需要付出一定的努力的。下面是我自己写的线性表的实现,希望通过不断练习加深巩固数据结构的基础知识!
1.顺序表
package List; class SeqList { private int defaultSize = 10; private int maxSize ; private int size; private Object[] list; public SeqList() { init(defaultSize); } public SeqList(int size) { init(size); } private void init(int size) { maxSize = size; size = 0; list = new Object[maxSize]; } //插入 public void insert(int i, Object obj) { //省略2个条件判断 for(int j = size; j > i; j--) { list[j] = list[j - 1]; } list[i] = obj; size++; } //删除 public Object delete(int i) { Object obj = list[i]; for(int j = i; j < size - 1; j++) { list[j] = list[j + 1]; } size--; return obj; } //获取元素 public Object getData(int i) { return list[i]; } //测试 /* 插入元素:1 插入元素:2 插入元素:3 插入元素:4 插入元素:5 顺序表为:1 2 3 4 5 顺序表为:3 4 5 */ public static void main(String args[])throws Exception { SeqList list = new SeqList(); for(int i = 0; i < 5; i++) { list.insert(i, new Integer(i+1)); System.out.println("插入元素:" + (i + 1)); } System.out.print("顺序表为:"); for(int i = 0; i < list.size; i++) { System.out.print(list.getData(i) + " "); } System.out.println(); list.delete(0); list.delete(0); System.out.print("顺序表为:"); for(int i = 0; i < list.size; i++) { System.out.print(list.getData(i) + " "); } System.out.println(); } }2.链表
package List; import java.util.Scanner; class Node { private Object element; private Node next; //构造头结点 public Node(Node next) { this.next = next; } //构造普通节点 public Node(Node next, Object element) { this.element = element; this.next = next; } public Object getElement() { return element; } public void setElement(Object element) { this.element = element; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } public class LinkedList { private Node head; private Node current; private int size; public LinkedList() { head = current = new Node(null); size = 0; } //定位 public void index(int i ) { //省略判断条件 //定位到头结点 if(i == -1) { current = head; return ; } current = head.getNext(); int j = 0; while( current != null && j < i) { current = current.getNext(); j++; } } //插入 public void insert(int i, Object obj) { //省略判断 index(i - 1); current.setNext(new Node(current.getNext(), obj)); size++; } //删除 public Object delete(int i) { //省略判断 index(i - 1); Object obj = current.getNext().getElement(); current.setNext( current.getNext().getNext()); return obj; } //返回元素 public Object getData(int i) { //省略判断 index(i); return current.getElement(); } public int size() { return size; } //测试 /* 请输入第1个整数:2 请输入第2个整数:4 请输入第3个整数:6 请输入第4个整数:8 请输入第5个整数:10 单链表元素为:2 4 6 8 10 大小为:5 删除第1、2个后的数据为:4 8 10 程序结束! */ public static void main(String args[])throws Exception { Scanner reader = new Scanner(System.in); int num = 0; LinkedList list = new LinkedList(); for(int i = 0; i < 5; i++) { System.out.print("请输入第" + (i+1) + "个整数:"); num = reader.nextInt(); list.insert(i, num); } System.out.print("单链表元素为:"); list.index(0); while(list.current != null) { System.out.print(list.current.getElement() + " "); list.current = list.current.getNext(); } System.out.println(); System.out.println("大小为:" + list.size()); list.delete(0); list.delete(1); System.out.print("删除第1、2个后的数据为:"); list.index(0); while(list.current != null) { System.out.print(list.current.getElement() + " "); list.current = list.current.getNext(); } } }3.循环链表
package List; import java.util.Scanner; class CycleNode { private Object element; private CycleNode next; public CycleNode(CycleNode next) { this.next = next; } public CycleNode(CycleNode next, Object element) { this.next = next; this.element = element; } public Object getElement() { return element; } public void setElement(Object element) { this.element = element; } public CycleNode getNext() { return next; } public void setNext(CycleNode next) { this.next = next; } } public class CycleLinkedList { private CycleNode current; private CycleNode head; private int size; public CycleLinkedList() { current = head = new CycleNode(null); head.setNext(head); size = 0; } //定位 public void index(int i) { //... if(i == -1) { current = head; } current = head.getNext(); int j = 0; while(current.getNext() != head && j < i) { current = current.getNext(); j++; } } //插入 public void insert(int i, Object obj) { //... index(i - 1); current.setNext(new CycleNode(current.getNext(), obj)); size++; } //删除 public void delete(int i) { //... index(i - 1); current.setNext(current.getNext().getNext()); size--; } //获取元素 public Object getData(int i) { index(i); return current.getElement(); } //为空 public boolean isEmpty() { return size == 0; } //测试 /* 请输入第1个数:2 请输入第2个数:4 请输入第3个数:6 请输入第4个数:8 请输入第5个数:10 您输入的数为:2 4 6 8 10 是否为空:false 大小:5 链表最后一个数的下一个数是:2 */ public static void main(String args[])throws Exception { CycleLinkedList cycleLinList = new CycleLinkedList(); Scanner reader = new Scanner(System.in); for(int i = 0; i < 5; i++) { System.out.print("请输入第" + (i + 1) + "个数:"); int num = reader.nextInt(); cycleLinList.insert(i, num); } System.out.print("您输入的数为:"); for(int i = 0; i < cycleLinList.size; i++) { System.out.print(cycleLinList.getData(i) + " "); } System.out.println(); System.out.println("是否为空:" + cycleLinList.isEmpty()); System.out.println("大小:" + cycleLinList.size); System.out.print("链表最后一个数的下一个数是:"); cycleLinList.index(cycleLinList.size - 1); //cycleLinList.current.next指向头结点,但头结点无值,输出null。 //必须再next才能到达第一个值 System.out.print(cycleLinList.current.getNext().getNext().getElement()); } }4.双向循环链表
package List; import java.util.Scanner; class DoubleNode { private Object element; private DoubleNode prior; private DoubleNode next; public DoubleNode(DoubleNode prior, DoubleNode next) { this.prior = prior; this.next = next; } public DoubleNode(DoubleNode prior, DoubleNode next, Object element) { this.prior = prior; this.next = next; this.element = element; } public Object getElement() { return element; } public void setElement(Object element) { this.element = element; } public DoubleNode getPrior() { return prior; } public void setPrior(DoubleNode prior) { this.prior = prior; } public DoubleNode getNext() { return next; } public void setNext(DoubleNode next) { this.next = next; } } public class DoubleLinkedList { private DoubleNode current; private DoubleNode head; private int size; public DoubleLinkedList() { current = head = new DoubleNode(null, null); head.setNext(head); head.setPrior(head); size = 0; } //定位 public void index(int i) { //... if(i == -1) { current = head; return ; } current = head.getNext(); int j = 0; while(current != head && j < i) { current = current.getNext(); j++; } } //插入 public void insert(int i, Object obj) { //... index(i - 1); DoubleNode element = new DoubleNode(current, current.getNext(), obj); current.getNext().setPrior(element); current.setNext(element); size++; } //删除 public Object delete(int i) { //... index(i - 1); Object obj= current.getNext().getElement(); current.getNext().getNext().setPrior(current); current.setNext(current.getNext().getNext()); size--; return obj; } //获取元素 public Object getData(int i) { //... index(i); return current.getElement(); } //测试 /* 请输入第1个数:2 请输入第2个数:4 请输入第3个数:6 请输入第4个数:8 请输入第5个数:10 您输入的数为:2 4 6 8 10 第1个数的前后分别是:null 4 删除第2个元素后的数为:2 6 8 10 */ public static void main(String args[])throws Exception { DoubleLinkedList doubleList = new DoubleLinkedList(); Scanner reader = new Scanner(System.in); //输入1 2 3 4 5 for(int i = 0; i < 5; i++) { System.out.print("请输入第" + (i + 1) + "个数:"); int num = reader.nextInt(); doubleList.insert(i, num); } System.out.print("您输入的数为:"); for(int i = 0; i < doubleList.size; i++) { System.out.print(doubleList.getData(i) + " "); } System.out.println(); //输出:null 2 System.out.print("第1个数的前后分别是:"); doubleList.index(0); System.out.print(doubleList.current.getPrior().getElement() + " "); System.out.println(doubleList.current.getNext().getElement() + " "); //删除第一个元素 2 doubleList.delete(1); System.out.print("删除第2个元素后的数为:"); for(int i = 0; i < doubleList.size; i++) { System.out.print(doubleList.getData(i) + " "); } System.out.println(); } }我对线性表的实现做了一些总结,有兴趣的可以看一下: 线性表、堆栈、队列的实现总结