数据结构--链表的操作(顺序结构)
2016-05-16 19:23
453 查看
package list;
interface List{
public void clear();
public boolean isEmpty();
public int length();
public Object get(int i);
public void insert(int i,Object x);
public void remove(int i);
public int indexOf(Object x);
public void display();
}
class SqList implements List{
public Object[] listElem;
//顺序表当前长度
private int curLen;
//顺序表的最大容量
//public static final int MaxSize = 20;
//构造一个容量为MaxSize的空顺序表函数
public SqList(int MaxSize) {
curLen =0;
listElem = new Object[MaxSize];
}
@Override
public void clear() {
curLen =0;
}
@Override
public boolean isEmpty() {
return curLen==0;
}
@Override
public int length() {
return curLen;
}
@Override
public Object get(int i) {
if (i<0||i>curLen-1) {
try {
throw new Exception("第"+i+"个元素不存在");
} catch (Exception e) {
e.printStackTrace();
}
}
return listElem[i];
}
@Override
public void insert(int i, Object x) {
if (i<0||i>=listElem.length) {
try {
throw new Exception("插入的位置不合法,请重新选择插入位置!");
} catch (Exception e) {
e.printStackTrace();
}
}else{
for(int k=curLen;k>i;k--){
listElem[k]=listElem[k-1];
}
listElem[i]=x;
curLen++;
}
}
@Override
public void remove(int i) {
if (i<0||i>listElem.length-1) {
try {
throw new Exception("删除的位置不合法");
} catch (Exception e) {
e.printStackTrace();
}
}
else {
for(int k =i+1;k<listElem.length;k++){
listElem[k-1]=listElem[k];
}
}
curLen--;
}
@Override
public int indexOf(Object x) {
int i=0;
while(i<curLen&&!listElem[i].equals(x)){
i++;
}
if(i<curLen)
return i;
else
return -1;
}
public int directprior(int i){
int temp=0;
if (i<0||i>listElem.length) {
try {
throw new Exception("前驱不存在");
} catch (Exception e) {
e.printStackTrace();
}
}
else
temp=i-1;
return temp;
}
@Override
public void display()
{
for(int i=0;i<curLen;i++)
System.out.print(listElem[i]+" ");
System.out.println("");
}
}
public class IList{
public static void main(String [] args){
SqList List=new SqList(10);
List.insert(0, 'a');
List.insert(1, 'b');
List.insert(2, 'c');
List.insert(3, 'd');
List.insert(4, 'e');
List.insert(5, 'f');
List.insert(6, 'g');
List.display();
System.out.println("第一次出现e的位置是:");
int order =List.indexOf('e');
System.out.println(order+1);
int key=List.directprior(3);
System.out.println("位置3前面的值是:"+List.listElem[key]);
}
}
interface List{
public void clear();
public boolean isEmpty();
public int length();
public Object get(int i);
public void insert(int i,Object x);
public void remove(int i);
public int indexOf(Object x);
public void display();
}
class SqList implements List{
public Object[] listElem;
//顺序表当前长度
private int curLen;
//顺序表的最大容量
//public static final int MaxSize = 20;
//构造一个容量为MaxSize的空顺序表函数
public SqList(int MaxSize) {
curLen =0;
listElem = new Object[MaxSize];
}
@Override
public void clear() {
curLen =0;
}
@Override
public boolean isEmpty() {
return curLen==0;
}
@Override
public int length() {
return curLen;
}
@Override
public Object get(int i) {
if (i<0||i>curLen-1) {
try {
throw new Exception("第"+i+"个元素不存在");
} catch (Exception e) {
e.printStackTrace();
}
}
return listElem[i];
}
@Override
public void insert(int i, Object x) {
if (i<0||i>=listElem.length) {
try {
throw new Exception("插入的位置不合法,请重新选择插入位置!");
} catch (Exception e) {
e.printStackTrace();
}
}else{
for(int k=curLen;k>i;k--){
listElem[k]=listElem[k-1];
}
listElem[i]=x;
curLen++;
}
}
@Override
public void remove(int i) {
if (i<0||i>listElem.length-1) {
try {
throw new Exception("删除的位置不合法");
} catch (Exception e) {
e.printStackTrace();
}
}
else {
for(int k =i+1;k<listElem.length;k++){
listElem[k-1]=listElem[k];
}
}
curLen--;
}
@Override
public int indexOf(Object x) {
int i=0;
while(i<curLen&&!listElem[i].equals(x)){
i++;
}
if(i<curLen)
return i;
else
return -1;
}
public int directprior(int i){
int temp=0;
if (i<0||i>listElem.length) {
try {
throw new Exception("前驱不存在");
} catch (Exception e) {
e.printStackTrace();
}
}
else
temp=i-1;
return temp;
}
@Override
public void display()
{
for(int i=0;i<curLen;i++)
System.out.print(listElem[i]+" ");
System.out.println("");
}
}
public class IList{
public static void main(String [] args){
SqList List=new SqList(10);
List.insert(0, 'a');
List.insert(1, 'b');
List.insert(2, 'c');
List.insert(3, 'd');
List.insert(4, 'e');
List.insert(5, 'f');
List.insert(6, 'g');
List.display();
System.out.println("第一次出现e的位置是:");
int order =List.indexOf('e');
System.out.println(order+1);
int key=List.directprior(3);
System.out.println("位置3前面的值是:"+List.listElem[key]);
}
}
相关文章推荐
- 数据结构--栈的顺序存储
- 数据结构--归并排序
- 数据结构(绪论)
- 数据结构学习---线性表
- 数据结构上机实验五——01背包问题
- 共同学习Java源代码--数据结构--ArrayList类(三)
- Redis中的数据结构-String与List
- C/C++ 数据结构---线性顺序存储数据:查询快,增删慢
- (考研)数据结构及算法
- 【数据结构】图形的多种表示方法及其java实现之相邻矩阵法
- NoSQL初学者:15分钟学会Redis数据结构
- 数据结构之查找
- 初学redis二(Redis的数据结构)
- 数据结构学习笔记07散列查找(哈希表)
- 数据结构习题 (整理中)
- 理解红黑树
- 数据结构之魔方程序
- C++数据结构-单向列表如何操作
- C 侵入式数据结构
- 数据结构实验之链表三:链表的逆置