【数据结构】顺序表重难点总结

本文介绍了线性表中的顺序表概念,包括其逻辑和物理结构,并展示了如何使用Java实现顺序表的基本操作,如增删元素。此外,通过一个扑克牌游戏的例子,演示了顺序表在实际应用中的使用,包括买牌、洗牌和揭牌的过程。
摘要由CSDN通过智能技术生成


1. 线性表 ---- 基础概念

  1. 线性表(linear list)是n个具有相同特性的数据元素的有限序列。顺序表,链表,栈,队列都是线性表
  2. 线性表是在逻辑上是线性的,在物理上不一定是线性的。顺序表在物理和逻辑上都是线性的,链表在逻辑上是线性的,在物理上非线性。

2. 顺序表的相关功能实现 ---- 必备功能

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。

  1. 用数组实现一个空顺序表
public class MyArraylist {
	public int[] elem;
	public int usedSize;
	public final static int DEFAULT_SIZE = 10; //为数组设置初始容量
	public MyArrayList(){
		//用数组实现空顺序表
		this.elem = new int[DEFAULT_SIZE];
	}
}
  1. 增加元素
//增加元素
public void add(int val){
	if(isFull()){   //数组满了需要扩容,这里设置二倍扩容
		this.elem = Arrays.copyOf(array, 2*array.length);
	}
	//数组未满,添加元素,usedSize加 1
	elem[usedSize] = val;
	usedSize++;
}
public boolean isFull(){   //判断数组满不满
	return elem.usedSize == elem.length){
}
  1. 删除指定元素
//删除指定元素
public void delete(int value){
	//判断顺序表是否为空,为空抛出错误,或设置提示方法
	if(isEmpty()){
		System.out.println("顺序表为空,无法删除");
		return;
	}
	int index = -1;
	for(int i = 0; i < elem.length; i++){
		if(elem[i] == value){
			index = i;
		}
	}
	if(index == -1){
		System.out.println("要删除的元素不在顺序表中");
	}else{
		//将index后面的元素向前挪一位
		for(int i = index; i < elem.length; i++){
			elem[i] = elem[i+1];
		}
		usedSize--;//注意最后顺序表的usedSize要减1
	}
}
  1. 在固定位置增加元素
//在pos位置新增元素
public void addData(int pos, int value){
	//先判断pos的合法性
	if(pos < 0 || pos > usedSize){
		System.out.println("要添加的元素位置不合法!");
		return;
	}
	//判断数组满不满
	if(isFull()){
		this.elem = Arrays.copyOf(array, 2*array.length);
	}
	//添加元素,需要先把pos之后的元素向后挪一位,再添加value
	for(int i = elem.usedSize; i > pos; i++){
		elem[i] = elem[i-1];
	}
	elem[pos] = value;
	usedSize++;
}
  1. 清空顺序表
public void clear(){
	this.usedSize = 0;
}

3. 顺序表的应用 ------ 扑克牌游戏

首先,这个游戏的逻辑功能是,买牌(建立顺序表),发牌(随机将元素分为三组),揭牌

  1. 规定扑克牌基本属性,图案,牌的数目。
public class Poker {
    //买一副扑克牌,52张牌,四个花色
    private String color;
    private String num;

    public Poker(String color, String num) {
        this.color = color;
        this.num = num;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return color + " " + num;
    }
}
  1. 买一副扑克牌牌,将每一张牌的属性设置好,再加入这副扑克牌中
public class Pokers {
    //买牌,洗牌,揭牌
    public static final String[] COLOR = {"♥","♦","♣","♠"};
    public static final String[] NUM = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

    //买一副扑克牌
    public static List<Poker> buyPokers(){
        List<Poker> pokerlist = new ArrayList<>();
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 13; j++){
                //买一张牌
                Poker poker = new Poker(COLOR[i],NUM[j]);
                pokerlist.add(poker);
            }
        }
        return pokerlist;
    }
 }
  1. 洗牌,从最后一张牌开始,与随机牌牌交换,达到洗牌的目的,注意这里要从最后一张牌开始交换,以设置random可以交换的范围
 //洗牌,交换
    public static void shuffle(List<Poker> pokerList){
        Random random = new Random();
        for(int x = pokerList.size()-1; x > 0; x--){
            //random.nextInt(x),随机获取0~x-1的数字
            int index = random.nextInt(x);
            swap(pokerList,x,index);
        }
    }
    //swap函数
    public static void swap(List<Poker> pokerList, int x, int index){
        //换牌用set(设置),get(获取)
        Poker tmp = pokerList.get(x);
        pokerList.set(x,pokerList.get(index));
        pokerList.set(index,tmp);
    }

  1. 游戏测试
public static void main(String[] args){
        List<Poker> pokerList = buyPokers();
        System.out.println("买牌");
        System.out.println(pokerList);
        System.out.println("=========================");
        System.out.println("洗牌");
        shuffle(pokerList);
        System.out.println(pokerList);
        System.out.println("=========================");

        //揭牌,四个人,轮流揭牌五张
        //定义二维数组,hand的每个成员是List<Poker>类型
        List<List<Poker>> hand = new ArrayList<>();
        List<Poker> hand1 = new ArrayList<>();
        List<Poker> hand2 = new ArrayList<>();
        List<Poker> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
        //二维数组初始化
        for(int i = 0; i < 17; i++){
            for(int j = 0; j < 3; j++){
                List<Poker> handpoker = hand.get(j);
                //remove(0)返回值就是下标为0的元素
                handpoker.add(pokerList.remove(0));
            }
        }
        for(int i = 0; i < hand.size(); i++){
            System.out.println("第"+i+"个人的牌是"+hand.get(i));
        }
        System.out.println("剩余的牌");
        System.out.println(pokerList);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值