栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构和算法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构和算法

这里写目录标题
  • 队列
    • 数组实现
      • 顺序队列
      • 循环队列
  • 链表
    • 单链表
    • 双链表

队列 数组实现 顺序队列
public class ArrayQueue {
    public static void main(String[] args) {
        int key=' ';
        ArrayQueueProcess q=new ArrayQueueProcess(3);
        Scanner sc=new Scanner(System.in);
        boolean loop=true;
        while(loop){
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key=sc.next().charAt(0);
            switch(key){
                case 'a':
                    System.out.println("请输入元素:");
                    int a=sc.nextInt();
                    q.AddQueue(a);
                    break;
                case 'e':
                    System.out.println("bye~~~");
                    loop=false;
                    break;
                case 's':
                        q.ShowQueue();
                    break;
                case 'g':
                  q.GetQueue();
                    break;
                case'h':
                        q.GetHead();
                    break;
                default:
                    break;
            }
        }
    }
}
class ArrayQueueProcess{
    private  int MaxSize;
    private int rear;
    private int front;
    private int arr[];
    ArrayQueueProcess(int n){
        MaxSize=n;
        front=0;
        rear=0;
        arr=new int [MaxSize];
    }
    public boolean  IsFull(){
        return rear==MaxSize;
    }
    public boolean IsEmpty(){
        return rear==front;
    }

    public void AddQueue(int n){
        if(IsFull()){
            System.out.println("full~~~~~~~~");
            return;
        }
        arr[rear]=n;
        rear++;
    }

    public void GetQueue(){
        if(IsEmpty()){
            System.out.println("Empty~~~~~~~~");
            return;
        }
        else{
            int value = arr[front];
            front ++;
            System.out.println(front);
        }


    }

    public void ShowQueue(){
        if(IsEmpty()){
            System.out.println("Empty~~~~~~~~");
            return;
        }
        for(int i=0;i 
循环队列 

public class CycleArrayQueue {
    public static void main(String[] args) {
        int key=' ';
        CycleArrayQueueProcess q=new CycleArrayQueueProcess(4);
        Scanner sc=new Scanner(System.in);
        boolean loop=true;
        while(loop){
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key=sc.next().charAt(0);
            switch(key){
                case 'a':
                    System.out.println("请输入元素:");
                    int a=sc.nextInt();
                    q.AddQueue(a);
                    break;
                case 'e':
                    System.out.println("bye~~~");
                    loop=false;
                    break;
                case 's':
                    q.ShowQueue();
                    break;
                case 'g':
                    q.GetQueue();
                    break;
                case'h':
                    q.GetHead();
                    break;
                default:
                    break;
            }
        }
    }
}
class CycleArrayQueueProcess{
    private  int MaxSize;
    private int rear;
    private int front;
    private int arr[];
    CycleArrayQueueProcess(int n){
        MaxSize=n;
        front=0;
        rear=0;
        arr=new int [MaxSize];
    }
    public boolean IsFull(){
        return (rear+1)%MaxSize==front;
    }
    public boolean IsEmpty(){
        return rear==front;
    }
    public void AddQueue(int n){
          if(IsFull()){
              System.out.println("full~~~~~~");
              return;
          }
          arr[rear]=n;
          rear=(rear+1)%MaxSize;
    }
    public void GetQueue(){
        if(IsEmpty()){
            System.out.println("empty~~~~~");
        }
        else {
            int value = arr[front];
            front = (front + 1) % MaxSize;
            System.out.println(value);
        }
    }
   public void ShowQueue(){
       if(IsEmpty()){
           System.out.println("empty~~~~~");
       }
        else{
            for(int i=0;i<(rear+MaxSize-front)%MaxSize;i++){
                System.out.println("arr["+(i%MaxSize)+"]"+"="+arr[i%MaxSize]);
            }
       }
   }
   public void GetHead(){
       if(IsEmpty()){
           System.out.println("empty~~~~~");
       }
       else{
           System.out.println(arr[front]);
       }
   }
}
链表 单链表
public class SinglelinkedListDemo {
    public static void main(String[] args) {
        Node node1=new Node(1,"马斯克");
        Node node2=new Node(2,"贝索斯");
        Node node3=new Node(3,"扎克伯格");
        Node node4=new Node(2,"马云");
     SinglelinkedList s=new SinglelinkedList();
//     s.AddNode(node1);
//     s.AddNode(node2);
//     s.AddNode(node3);
        s.AddNodeByOrder(node1);
        s.AddNodeByOrder(node3);
        s.AddNodeByOrder(node2);
        System.out.println("before");
        s.Showlink();
        s.delNode(1);
        System.out.println("after");
        s.Showlink();
        s.update(node4);
        System.out.println("after");
        s.Showlink();
    }
}


class SinglelinkedList {
    Node head = new Node(0, "");

    public void AddNode(Node node){
        Node temp=head;
        while(true){
            if(temp.next==null){
                break;
            }
            temp=temp.next;
        }
        temp.next=node;
    }

   public void AddNodeByOrder(Node node){
        Node temp=head;
        boolean flag=false;
        while(true){
            if(temp.next==null){
                break;
            }
            if(temp.next.no==node.no){
                flag=true;
                break;
            }
            if(temp.next.no>node.no){
                break;
            }
            temp=temp.next;
        }
        if(flag){
                System.out.println(temp.next.data+"already exist");
        }
        else{
            node.next=temp.next;
            temp.next=node;
        }

    }

    public void delNode(int no){
        boolean flag=true;
        Node temp=head;
        while (true){
            if(temp==null){
                break;
            }
            if(temp.next.no==no){
                flag=false;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            System.out.println("Sorry! we did not find this element");
        }
        else{
            temp.next=temp.next.next;
        }

    }

    public void update(Node node){
        if(head.next == null) {
            System.out.println("empty");
            return;
        }
   Node temp=head.next;
   boolean flag=false;
        while (true){
            if(temp==null){
                break;
            }
            if(temp.no==node.no){
                flag=false;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            System.out.println("Sorry! we did not find this element");
        }
        else{
            temp.data=node.data;

        }

    }
    public void Showlink(){
        Node temp=head.next;
        if(head.next==null){
            System.out.println("empty~~~~");
            return;
        }
        else{
            while(true){
                if(temp==null){
                    break;
                }
                else{
                    System.out.println(temp.no+"    "+temp.data);
                    temp=temp.next;
                }
            }
        }
    }


}

class Node{
    int no;
    String data;
    Node next;

Node(int no,String data){
    this.no=no;
    this.data=data;
}
}
双链表
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/682893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号