数据结构-顺序表
来源:互联网 发布:java构造器小技巧 编辑:程序博客网 时间:2024/06/06 11:01
package suanfa;
/**
*
* @author Administrator 顺序表在内存空间是一串连续的地址空间,最基础的线性表,随机存取 seqList
*/
public class SeqList {
private Integer[] array;
private int maxsize;
private int current = 0;
public SeqList(int n) {
if (n > 0) {
maxsize = n;
this.array = new Integer[n];
}
}
public boolean add(Integer n) {
if(current>maxsize){
return false;
}
array[current]= n;
current++;
return true;
}
public boolean delete(Integer location) {
if(location>=current){
return false;
}else{
for(int i= location;i<current-1;i++){
array[i]=array[i+1];
}
//写的时候忘记写这个了--记住
current--;
return true;
}
}
public Integer get(int n){
if(n<0|| n>=current){
return null;
}else{
return array[n];
}
}
public boolean update(int n,Integer m){
if(n<0|| n>=current){
return false;
}else{
array[n]=m;
return true;
}
}
public int getMaxsize() {
return maxsize;
}
public void setMaxsize(int maxsize) {
this.maxsize = maxsize;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public static void main(String[] args) {
//建立顺序表
SeqList list = new SeqList(10);
for(int i=0;i<10;i++){
list.add(i*10);
}
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
////删除索引3
list.delete(3);
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
//将顺序表索引为5的元素改为44
list.update(5, 55);
////输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
}
}
/**
*
* @author Administrator 顺序表在内存空间是一串连续的地址空间,最基础的线性表,随机存取 seqList
*/
public class SeqList {
private Integer[] array;
private int maxsize;
private int current = 0;
public SeqList(int n) {
if (n > 0) {
maxsize = n;
this.array = new Integer[n];
}
}
public boolean add(Integer n) {
if(current>maxsize){
return false;
}
array[current]= n;
current++;
return true;
}
public boolean delete(Integer location) {
if(location>=current){
return false;
}else{
for(int i= location;i<current-1;i++){
array[i]=array[i+1];
}
//写的时候忘记写这个了--记住
current--;
return true;
}
}
public Integer get(int n){
if(n<0|| n>=current){
return null;
}else{
return array[n];
}
}
public boolean update(int n,Integer m){
if(n<0|| n>=current){
return false;
}else{
array[n]=m;
return true;
}
}
public int getMaxsize() {
return maxsize;
}
public void setMaxsize(int maxsize) {
this.maxsize = maxsize;
}
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public static void main(String[] args) {
//建立顺序表
SeqList list = new SeqList(10);
for(int i=0;i<10;i++){
list.add(i*10);
}
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
////删除索引3
list.delete(3);
//输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
//将顺序表索引为5的元素改为44
list.update(5, 55);
////输出顺序表
for(int j=0;j<list.current;j++){
System.out.print(list.get(j)+" ");
}
System.out.println();
}
}
阅读全文
0 0
- 数据结构之 顺序表
- 数据结构--顺序线性表
- 数据结构---顺序表
- 数据结构之顺序表
- 数据结构顺序表
- 数据结构顺序表
- 顺序表(数据结构)
- 数据结构之顺序表
- 数据结构之顺序表
- 数据结构笔记--顺序表
- 【数据结构】顺序表
- 数据结构《顺序链表》
- 数据结构之顺序表
- 数据结构试验 顺序表
- 数据结构--顺序表
- 数据结构--顺序表
- 数据结构之顺序表
- 基本数据结构:顺序表
- WebStorm集成Mocha + Chai进行js单元测试
- leetcode[Find Mode in Binary Search Tree]//待整理多种解法
- javascript将字符串中的多个空格替换为一个空格的正则实例
- Mongodb远程登陆
- Static的作用
- 数据结构-顺序表
- Seven ways to improve example-based single image super resolution笔记
- 2017.8.7所感
- RH134-第十九节-系统恢复
- 采用MD5加密字段, 向第三方发送加密后的字段出现错误
- 基础排序算法总结(七种排序算法)
- LeetCode 5. Longest Palindromic Substring--最长回文子串
- html 与 htm的区别
- 【0x7FFFFFFF】【0x3f3f3f3f】