《程序設計與數據結構》實驗一報告

學號 2018-2019-1 《程序設計與數據結構》實驗一報告

課程:《程序設計與數據結構》

班級: 1723
姓名: 康皓越
學號:20172326
實驗教師:王志強
實驗日期:2018年9月26日
必修/選修: 必修

1.實驗內容

  • 實驗一
    鏈表練習,要求實現下列功能:
    經過鍵盤輸入一些整數,創建一個鏈表;
    這些數是你學號中依次取出的兩位數。 再加上今天的時間。
      例如你的學號是 20172301
      今天時間是 2018/10/1, 16:23:49秒數字就是20, 17,23,1, 20, 18,10,1,16,23,49
      打印全部鏈表元素, 並輸出元素的總數。 在你的程序中,請用一個特殊變量名來紀錄元素的總數,變量名就是你的名字。 例如你叫 張三, 那麼這個變量名就是
      int nZhangSan = 0; //初始化爲 0.
    作完這一步,把你的程序簽入源代碼控制(git push)。node

  • 實驗二
    實現節點插入、刪除、輸出操做;
      繼續你上一個程序,擴展它的功能,每作完一個新功能,或者寫了超過10行新代碼,就簽入代碼,提交到源代碼服務器;
    從磁盤讀取一個文件, 這個文件有兩個數字。
    從文件中讀入數字1, 插入到鏈表第 5 位,並打印全部數字,和元素的總數。 保留這個鏈表,繼續下面的操做。
    從文件中讀入數字2, 插入到鏈表第 0 位,並打印全部數字,和元素的總數。 保留這個鏈表,並繼續下面的操做。
    從鏈表中刪除剛纔的數字1. 並打印全部數字和元素的總數。
    簽入全部代碼。git

  • 實驗三
    使用冒泡排序法或者選擇排序法根據數值大小對鏈表進行排序;
      若是你學號是單數, 選擇冒泡排序,不然選擇選擇排序。
      在排序的每個輪次中,打印元素的總數,和目前鏈表的全部元素。 在實驗二獲得的程序中繼續擴展, 用同一個程序文件,寫不一樣的函數來實現這個功能。 仍然用 nZhangSan (你的名字)來表示元素的總數。數組

  • 實驗四
    使用數組實現實驗1、二的相關內容服務器

  • 實驗五
    實驗數組實現實驗三的內容數據結構

    2. 實驗過程及結果

    實驗一

  • 用scanner方法將數組讀取後,使用Stringtokenizer方法將其分開,以後存入鏈表,打印函數

public void TailInsert(LinkedList head, LinkedList node){
        LinkedList temp;
        temp =head;
        while (temp.next != null){
            temp = temp.next;
        }
        temp.next = node;
        nkhy++;
    }

實驗二

  • 使用InputStream is = new FileInputStream方法實現從文件中讀取數據,在實現成功後,使用頭插法和插入法實現插入,以及刪除。
front=head;
        node.next=front;
        head=node;
        System.out.println("The element is :"+ head.element);
        nkhy++;
public void Insertnode(int num,LinkedList head,LinkedList node2){
        LinkedList temp = head;
        while (num != temp.element){
          temp = temp.next;
          }

          node2.next = temp.next;
          temp.next = node2;
          nkhy++;
    }
public  void DeleteNode(LinkedList Head,LinkedList node){
        LinkedList PreNode = Head, CurrentNode = Head;
        while (CurrentNode !=null){
            if(CurrentNode.element != node.element){
                PreNode = CurrentNode;
                CurrentNode = CurrentNode.next;
            }
            else
                break;
        }
        PreNode.next = CurrentNode.next;
        nkhy--;
    }

實驗三

  • 使用選擇排序對鏈表進行排序
public void selectSort(LinkedList head){
        LinkedList temp = head;
        int sort[] = new int[head.size()];
        for(int index = 0;index<head.size();index++){
            sort[index] = temp.element;
            temp = temp.next;
            }
         selectionSort(sort);
    }

    public static void selectionSort(int[] a) {

        int n = a.length;
        for (int i = 0; i < n; i++) {
            int k = i;
            // 找出最小值的小標
            for (int j = i + 1; j < n; j++) {
                if (a[j] < a[k]) {
                    k = j;
                }
            }
            // 將最小值放到排序序列末尾
            if (k > i) {
                int tmp = a[i];
                a[i] = a[k];
                a[k] = tmp;
            }
            String res = "";
            for (int index=0;index<a.length;index++){
                res += a[index] + " ";
            }
            System.out.println("the current linkedlist is :" +res);
            System.out.println("the number of the elements is :" + a.length);
            System.out.println();
        }
    }

實驗四

  • 使用數組實現插入,刪除,文件讀取,數組的特色在於沒必要過於在乎對象之間的引用關係,只需考慮空間不足時申請新的空間。對於在頭部插入元素,我選擇申請一個新的數組,其容量爲以前二倍,將新數組的0位置處放入想要插入的元素,以後,從1開始依次放入舊數組的各個元素。刪除方法同理。
public void HeadInsert(int element){

        array2[0] = element;
        for(int i =0;i<nKHY;i++){
            array2[i+1] = array[i];
        }
        nKHY++;
        array = array2;
        array2 = new int[DEFAULT_CAPACITY*2];
    }


    public void delete(int id){
        for(int index=0; index<id;index++){
         array2[index]=array[index];
        }
        for(int index=id ; index<nKHY;index++){
            array2[index] = array[index+1];

        }
        array = array2;
        array2 = new int[DEFAULT_CAPACITY*2];
        nKHY--;
    }

實驗五

  • 這裏使用插入排序法,較爲簡單。

3. 實驗過程當中遇到的問題和解決過程

  • 問題一,超出邊界
  • 解決方法:能夠看出,數字的個數爲31,也就是說,加入的數字個數超過申請的空間,在比較後,使用了hasMoreElements()方法,因而解決了方法。
  • 問題二:在從數組插入讀取的數字時,老是將末尾數字變爲零。而且刪除失敗。同時,沒有將新申請的數組在賦給原數組後將其清空,致使出現問題。同時,在將須要插入的值插入進去後,因爲index值出現問題,致使後續插入出錯
    錯誤代碼:
for(int index=0 ; index<nKHY-id+2;index++){
         array2[id+1] = array[id];
         id++;
     }

改正後:設計

for(int index = id ; id < nKHY ;id++){
         array2[id+1] = array[id];

     }

其餘(感悟、思考等)

  • 本次實驗是對鏈表、數組相關知識理解,實踐的一次考察,所以,不少代碼相對注重基礎理解,讓我進一步理解了其原理。

參考資料

相關文章
相關標籤/搜索