(链)表的详细介绍,及实现代码
仅概述顺序表、单链表
线性表的相关概念:
在计算机应用出,线性表是一种常见的数据类塑,诸如在文件、内存、数据库等管理系统中经常需要对属于线性表的数据类型进行处理。
单链表的相关概念:
采用单向链式的存储方式存储的线性表称为单链表,由此建立一个实现类,实现线性表抽象类中定义的各接口函数的功能,该实现类称为单链表类。
表的顺序存储及表的链式存储基础实现代码如下:
接口定义:
public interface List {
public void init();
public int length();
public boolean empty();
public boolean full();
public Object getElement(int location);
public boolean insert(int i, Object o);
public Object delete(int i);
}
public interface List_1 {
public void clear();
public Object gete(int i);
public int leng();
public int loct(Object el);
public boolean inst(int loc, Object el);
public Object dele(int loc);
public boolean full();
public boolean empt();
}
节点定义:
public class Node {
Object data;
Node next;
public Node(Object d, Node n) {
data = d;
next = n;
}
public Node() {
this(null, null);
}
public Object getdata() {
return data;
}
public void setdata(Object el) {
data = el;
}
public Node getnext() {
return next;
}
public void setnext(Node p) {
next = p;
}
}
测试类
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// SqList list= new SqList(10);
// list.insert(1, 'a');
// list.insert(2, 'b');
// list.insert(3, 'c');
// list.print();
// list.delete(1);
// list.print();
LinkList LL1 = new LinkList();
for (int i = 0; i < 10; i++)
LL1.inst(i + 1, new Integer(i + 1));
LL1.prnt();
System.out.println();
LL1.inst(5, new Integer(99));
LL1.prnt();
System.out.println();
LL1.sort();
LL1.prnt();
}
}
功能实现
public class LinkList {
Node head;
int size;
public LinkList() {
head = new Node();
size = 0;
}
public LinkList(Object[] a) {
Node p;
int i, n = a.length;
head = new Node();
for (i = n - 1; i >= 0; i--) {
p = new Node(a[i], head.next);
head.setnext(p);
}
size = n;
}
public void clear() {
head = new Node();
size = 0;
}
public Node index(int i) {
Node p;
int j;
if ((i < 0) || (i > size))
p = null;
else if (i == 0)
p = head;
else {
p = head.next;
j = 1;
while ((p != null) && (j < i)) {
p = p.next;
j++;
}
}
return p;
}
public Object gete(int i) {
if ((i < 1) || (i > size))
return (null);
Node p = index(i);
return p.getdata();
}
public int leng() {
return size;
}
public int loct(Object el) {
Node p;
int j;
p = head.next;
j = 1;
while (p != null) {
if (p.data.equals(el))
break;
p = p.next;
j = j + 1;
}
if (p != null)
return (j);
else
return (0);
}
public boolean inst(int loc, Object el) {
if ((loc < 1) || (loc > size + 1))
return false;
Node p = index(loc - 1);
p.setnext(new Node(el, p.next));
size++;
return true;
}
public Object dele(int i) {
if ((size == 0) || (i < 1) || (i > size))
return null;
Node p = index(i - 1);
Object el = p.next.getdata();
p.setnext(p.next.next);
size--;
return el;
}
public boolean full() {
return false;
}
public boolean empt() {
return head.next == null;
}
public void orderinst(Node ip) {
Node p, q;
Object data;
data = ip.data;
if ((head.next == null)
|| (((Integer) data).intValue() <= ((Integer) head.next.data)
.intValue())) {
ip.next = head.next;
head.next = ip;
} else {
p = head.next;
q = null;
while ((p != null)
&& (((Integer) p.data).intValue() < ((Integer) data)
.intValue())) {
q = p;
p = p.next;
}
ip.next = q.next;
q.next = ip;
}
}
public void sort() {
Node p, s;
p = head.next;
head.next = null;
while (p != null) {
s = p;
p = p.next;
orderinst(s);
}
}
public void prnt() {
Node t;
t = head.next;
while (t != null) {
System.out.print(t.data + " ");
t = t.next;
}
}
}
class SqList implements List {
Object[] elem;
int maxLength;
int currentLength;
public SqList(){
}
public SqList(int length){
maxLength = length;
elem = new Object[maxLength];
currentLength=0;
}
@Override
public Object delete(int i) {
// TODO Auto-generated method stub
if((i<1)|(i>currentLength))
{
//System.out.println("");
return null;
}
if(empty())
{
//System.out.println("");
return null;
}
Object temp = elem[i-1];
for(int j=i+1;j<=currentLength-1;j++)
{
elem[j-1]=elem[j];
}
currentLength -=1;
return temp;
}
@Override
public boolean empty() {
// TODO Auto-generated method stub
if(currentLength==0)
return true;
return false;
}
@Override
public boolean full() {
// TODO Auto-generated method stub
if(currentLength==maxLength)
return true;
return false;
}
@Override
public Object getElement(int location) {
// TODO Auto-generated method stub
if((location<0)|(location>currentLength))
{
//System.out.println("");
return null;
}
if(empty())
{
//System.out.println("");
return null;
}
return elem[location];
}
@Override
public void init() {
// TODO Auto-generated method stub
currentLength=0;
}
@Override
public boolean insert(int i, Object o) {
// TODO Auto-generated method stub
if((i<1)|(i>currentLength+1))
{
//System.out.println("");
return false;
}
if(full())
{
//System.out.println("");
return false;
}
for(int j=(currentLength-1);j>=i;j--)
{
elem[j+1]=elem[j];
}
elem[i-1]=o;
currentLength +=1;
return true;
}
@Override
public int length() {
// TODO Auto-generated method stub
return currentLength;
}
public void print()
{
for(int i=0 ;i<currentLength;i++)
{
System.out.print(elem[i]+"\t");
}
System.out.println();
}
}