java實現順序表、鏈表、棧 (x)->{持續更新}

1.java實現節點java

/**
 * 節點
 * @luminous-xin
 * @param <T>
 */
public class Node<T> {
    T data;
    Node<T> next;
    public Node(Node<T> n){
        next = n;
    }
    public Node(T obj,Node<T> n){
        data = obj;
        next = n;
    }
    public T getData(){
        return data;
    }
    public Node<T> getNext(){
        return next;
    }
}

2.java實現鏈表數組

/**
 * 鏈表
 * @author luminous-xin
 * @param <T>
 */
public class ListLink<T> {
    //頭指針
    private Node<T> head;
    //單鏈表的長度
    private int length;

    //構造一個空的鏈表
    public ListLink() {
        length = 0;
        head = new Node<T>(null);
    }

    //獲取鏈表頭節點的位置
    public Node<T> getHead() {
        return head;
    }

    //在鏈表中插入一個元素
    public boolean add(T obj, int pos) {
        if (pos < 1 || pos > length + 1) {
            System.out.println("pos值不合法");
            return false;
        }
        int num = 1;
        Node<T> p = head, q = head.next;
        while (num < pos) {
            p = q;
            q = q.next;
            num++;
        }
        p.next = new Node<T>(obj, q);
        length++;
        return true;

    }

    //刪除鏈表中某各元素
    public T remove(int pos) {
        if (isEnpty()) {
            System.out.println("鏈表爲空表");
            return null;
        }
        if (pos < 1 || pos > length + 1) {
            System.out.println("pos值不合法");
            return null;
        }
        int num = 1;
        Node<T> p = head, q = head.next;
        while (num < pos) {
            p = q;
            q = q.next;
            num++;
        }
        p.next = q.next;
        length--;
        return q.data;
    }

    //獲取鏈表中一個元素的值
    public T value(int pos) {
        if (isEnpty()) {
            System.out.println("鏈表爲空表");
            return null;
        }
        if (pos < 1 || pos > length + 1) {
            System.out.println("pos值不合法");
            return null;
        }
        int num = 1;
        Node<T> p = head, q = head.next;
        while (num < pos) {
            p = q;
            q = q.next;
            num++;
        }
        return q.data;
    }

    //在鏈表中查找一個元素,返回該元素下標的索引
    public int find(T obj) {
        if (isEnpty()) {
            System.out.println("鏈表爲空表");
            return -1;
        }
        int num = 1;

        Node<T> p = head, q = head.next;
        while (q!= null) {
            if (obj.equals(q.data)) {
                return num;
            }
            p = q;
            q = q.next;
            num++;
        }
        return -1;
       /* Node<T> p = head.next;
        while (p != null) {
            if (p.data.equals(obj) == false) {
                p = p.next;
                num++;
            } else break;

        }
        if (p == null) {
            return -1;
        }
        return num;*/
    }

    //更新鏈表中某各元素
    public boolean modify(T obj, int pos) {
        if (isEnpty()) {
            System.out.println("鏈表爲空表");
            return false;
        }
        if (pos < 1 || pos > length + 1) {
            System.out.println("pos值不合法");
            return false;
        }
        int num = 1;
        Node<T> q = head.next;
        while (num < pos) {
            q = q.next;
            num++;
        }
        q.data = obj;
        return true;
    }

    //判空
    public boolean isEnpty() {
        return length == 0;
    }

    //求鏈表中數據元素的個數
    public int size() {
        return length;
    }

    //一次訪問鏈表中每一個元素並輸出
    public void nextOrder() {
        Node<T> q = head.next;
        while (q != null) {
            System.out.println(q.data);
            q = q.next;
        }
    }

    //銷燬一個已經存在的鏈表
    public void clear() {
        length = 0;
        head.next = null;
    }

    public static void main(String[] args) {
        ListLink<Integer> l = new ListLink<>();
        int i;
        int a[] = {23, 56, 12, 49, 35};
        for (i = 0; i < a.length; i++) {
            l.add(a[i], i + 1);
        }
        System.out.println("單鏈表中的數據元素爲:");
        l.nextOrder();
        System.out.println(l.find(5));

    }

3.java實現順序表spa

/**
 * 順序表
 * @author luminous-xin
 * @param <T>
 */
public class SequenceList<T> {
    final int maxSize = 10;           //順序表中一維數組的長度
    private T[] listArray;             //存儲元素的數組對象
    private int length;                 //保存順序表的當前長度
    public SequenceList(){                   //構造一個空的線性表
        length = 0;
        listArray = (T[])new Object[maxSize];
    }
    public SequenceList(int n){
        if(n <= 0){
            System.out.println("error");
            System.exit(1);
        }
        length = 0;
        listArray = (T[])new Object[n];
    }
    public boolean add(T obj , int pos){                  //在線性表中插入一個新元素
        if(pos < 1 || pos > length + 1){                //檢驗插入位置的有效性
            System.out.println("pos不合法");
            return false;
        }
        if(length == listArray.length){                 //擴容
            T[] p = (T[])new Object[length*2];
            for (int i = 0; i < length; i++) {
                listArray = p;
            }
        }
        for (int i = length; i > pos; i--) {           //插入
            listArray[i] = listArray[i-1];
        }
        listArray[pos-1] = obj;
        length++;
        return true;
    }
    public T remove(int pos){                               //在線性表中刪除一個元素
        if(isEmpty()){
            System.out.println("順序表爲空,沒法執行刪除操做");
            return null;
        }else{
            if(pos<1 || pos >length){
                System.out.println("pos不合法");
                return null;
            }

            T  x = listArray[pos-1];                          //備份被刪除的元素,將其做爲返回值返回
            for (int i = pos ; i <= length; i++) {
                listArray[i-1] = listArray[i];
            }
            length++;
            return x;
        }
    }

    public int find(T obj){                      //在線性表中查找一個元素
        if(isEmpty()){
            System.out.println("順序表爲空");
            return -1;
        }else{
            for(int i = 0 ; i <length ; i++){
                if(listArray[i].equals(obj)){
                    return i + 1;                     //返回該元素的位置
                }
            }
            return -1;
        }
    }
    public T value(int pos){                                  //獲取線性表中的一個元素
        if(isEmpty()){
            System.out.println("順序表爲空");
        }else{
            if(pos < 1 || pos > length +1){
                System.out.println("pos值不合法");
            }
        }
        return listArray[pos - 1];
    }
    public boolean modify(T obj,int pos){                      //更新線性表中的某各元素
        if(isEmpty()){
            System.out.println("順序表爲空");
            return false;
        }else{
            if(pos < 1 || pos > length  ){
                System.out.println("pos值不合法");
                return false;
            }
        }
        listArray[pos-1] = obj;
        return true;
    }
    public boolean isEmpty(){                              //判空
        return length == 0;
    }
    public int size(){                              //求線性表中數據元素的個數
        return length;
    }
    public void nextOrder(){                      //一次訪問棧中的每一個元素並輸出
        for(int i = 0 ; i< length; i++ ){
            System.out.println(listArray[i]);
        }
    }
    public void clear(){                       //銷燬一個已經存在的線性表
        length = 0;
    }


}

4.鏈表的應用,合併兩個有序鏈表指針

public class Chap2_2 {
    public static <T extends Comparable> void MergeList_L(ListLink<T> la, ListLink<T> lb ,ListLink<T> lc){
        Node<T> pa,pb,pc;
        pa = la.getHead().next;
        pb = lb.getHead().next;
        pc = lc.getHead();

        while(pa != null && pb != null){
            if(pa.data.compareTo(pb.data)<0){
                pc.next = pa;
                pc = pa ;
                pa = pa.next;
            }else{
                pc.next = pb;
                pc = pb;
                pb = pb.next;
            }

            while (pa != null){
                pc.next = pa;
                pc =  pc.next;
                pa = pa.next;
            }
            while (pb != null){
                pc.next = pb;
                pc = pb;
                pb = pb.next;
            }
            la.clear();
            lb.clear();
        }
    }
    public static void main(String[] args) {
        int i,j,k = 0;
        int[] b = {12,23,35,49,56};
        int[] a = {10,15,20};
        ListLink<Integer> la = new ListLink<>();
        ListLink<Integer> lb = new ListLink<>();
        ListLink<Integer> lc = new ListLink<>();
        for ( i = 0; i < a.length; i++) {
            la.add(a[i],i+1);
        }
        System.out.println("單鏈表a中的元素爲:");
        la.nextOrder();
        for (j = 0; j < b.length; j++) {
            lb.add(b[j],j+1);
        }
        System.out.println("單鏈表b中的數據元素爲:");
        lb.nextOrder();
        MergeList_L(la,lb,lc);
        System.out.println("單鏈表c中的元素爲:");
        lc.nextOrder();
    }
}

 

 

5.java實現順序棧code

/**
 * 順序棧
 * @author luminous-xin
 * @param <T>
 */
public class SequenceStack<T> {
    final int maxSize = 10;
    private T[] stackArray;
    private int top;
    //各方法的具體實現後有詳細描述
    public SequenceStack(){
        top = -1;
        stackArray = (T[])new Object[maxSize];
    }
    public SequenceStack(int n){
        if(n<=0){
            System.out.println("數組長度要大於零,不然退出程序運行!");
            System.exit(1);
        }
        top = -1;
        stackArray = (T[])new Object[n];
    }
    //在棧頂位置插入一個新元素
    public void push(T obj){
        if(top == stackArray.length-1){
            T[] p = (T[])new Object[top*2+2];
            for (int i = 0; i <= top; i++){
                p[i] = stackArray[i];
            }
            stackArray = p;
        }

        top++;
        stackArray[top] = obj;
    }
    //刪除棧頂元素
    public T pop(){
        if(top == -1) {
            System.out.println("數據棧已空,沒法刪除元素");
            return null;
        }
        top--;
        return stackArray[top+1];
    }
    //取棧頂數據元素
    public T getHead(){
        if (top == -1){
            System.out.println("數據棧已空,沒法刪除元素");
            return null;
        }
        return stackArray[top];
    }
    //判斷當前棧是否爲空
    public boolean isEmpty(){
        return top == -1;
    }
    //求出棧中元素的個數
    public int size(){
        return top +1;
    }
    //依次訪問棧中元素並輸出
    public void nextOrder(){
        for(int i = top ; i >= 0;i--){
            System.out.println(stackArray[i]);
        }
    }
    //銷燬一個已經存在的棧
    public void clear(){
        top = -1;
    }
}

 6.java實現鏈棧對象

/**
 * 鏈棧
 * @luminous-xin
 * @param <T>
 */
public class LinkStack<T> {
    private Node<T> top;   //棧頂指針
    private int length;     //存儲棧的長度
    public LinkStack(){     //構造一個空的棧
        length = 0;
        top = null;
    }
    public void push(T obj){        //入棧
        top = new Node<T>(obj,top);
        length++;
    }
    public T pop(){                 //出棧
        if(top == null){
            System.out.println("棧已空,沒法刪除");
            return null;
        }
        T x = top.data;
        top = top.next;
        length--;
        return x;
    }
    public T getHead(){             //取棧頂元素
        if(top == null){
            System.out.println("棧已空,沒法刪除");
            return null;
        }
        return top.data;
    }
    public int size(){              //求出棧中元素的個數
        return length;
    }
    public boolean isEmpty(){       //判斷棧是否爲空
        if (top ==null){
            return true;
        }
        return false;
    }
    public void nextOrder(){        //便利棧
        Node<T> p = top;
        while (p != null){
            System.out.println(p.data);
            p = p.next;
        }
    }

    public void clear(){            //銷燬一個棧
        top = null;
    }
}

 7.java實現順序鏈表blog

import org.omg.CORBA.Object;

/**
 * 循環隊列
 * @author luminous
 * @param <T>
 */
public class SequenceQueue<T> {
    final int maxSize = 10;
    private T queueArray[];
    private int front, rear;

    public SequenceQueue() {
        rear = front = 0;
        queueArray = (T[]) new Object[maxSize];
    }

    public void enQueue(T obj) {                            //入隊
        if ((rear + 1) % queueArray.length == front) {
            T[] p = (T[]) new Object[queueArray.length * 2];
            if (rear == ((T[]) queueArray).length) {
                for (int i = 0; i <= rear; i++) {
                    p[i] = queueArray[i];
                }
            } else {
                int i,j = 1;
                for (i = front + 1; i<queueArray.length;i++,j++)
                    p[j] = queueArray[i];
                for (i = 0;i<= rear ; i++,j++)
                    p[j] = queueArray[i];
                front = 0;
                rear = queueArray.length-1;
            }
            queueArray = p;
        }
        rear = (rear+1)%queueArray.length;
        queueArray[rear] = obj;
    }
    public T DeQueue(){                             //出隊
        if(isEmpty()){
            System.out.println("隊列已空,沒法出隊!");
            return null;
        }
        front = (front +1)%queueArray.length;
        return queueArray[front];

    }
    public T getTop(){                                  //取出頭元素
        if(isEmpty()){
            System.out.println("隊列已空,沒法讀取元素!");
            return null;
        }
        return queueArray[(front+1)%queueArray.length] ;
    }
    public boolean isEmpty(){                   //隊列的非空隊列
        return front==rear;
    }
    public int size(){                      //求隊列長度
        return (rear - front +queueArray.length)%queueArray.length;
    }
    public void nextOrder(){            //遍歷隊列
        int i,j = front ;
        for (i = 1; i < size(); i++) {
            j = (j+1)%queueArray.length;
            System.out.println(queueArray[j]);

        }
    }
    public void clear(){       //清空隊列
            front = rear =0;
    }
}
相關文章
相關標籤/搜索