Javascript模仿C語言的鏈表實現(增刪改查),並且使用控制台輸入輸出

來源:互聯網
上載者:User

標籤:鏈表   asc   out   log   長度   cti   return   rem   字元   

Js新手最近在研究Js資料結構,剛好看到鏈表實現這一塊兒,覺得有些資料和自己理解的有衝突,於是藉著自己以前一點點C語言的基礎,用Javascript模仿了C的鏈表實現,並且用了process.stdin和process.stdout的控制台輸入輸出。祝賀新手第一次發帖。哈哈哈。

代碼如下:

LinkList_node.js

  1 //節點類的建構函式  2 function Node(element){  3     this.element = element;  4     this.next = null;  5 }  6 //鏈表類的建構函式  7 function LList(){  8     this.head = new Node("head");  9     this.length = 0; 10 } 11 //尋找某個位置為pos的元素並在控制台列印 12 LList.prototype.find = function(pos){ 13     //檢查邊界 14     if(pos < 0|| pos > this.length) 15         return false; 16  17     var querynode = new Node(); 18     //遍曆到pos位置 19     for(var i = 0,querynode = this.head;i < pos;i++){ 20             querynode = querynode.next; 21     } 22     process.stdout.write(querynode.element.toString()); 23 } 24 //在位置為pos的地方插入元素element 25 LList.prototype.insert = function(element,pos){ 26     var newNode = new Node(element); 27     var querynode = new Node(); 28     //檢查邊界 29     if(pos < 0|| pos > this.length) 30         return false; 31     //插入頭部 32     if(pos == 0){ 33         newNode.next = this.head.next; 34         this.head.next = newNode; 35     } 36     //插入尾部 37     else if(pos == this.length){ 38         querynode = this.head; 39         for(var i = 0;i < this.length;i++){ 40             querynode = querynode.next; 41         } 42         querynode.next = newNode; 43         newNode.next = null; 44     } 45     //插入中間 46     else{ 47         querynode = this.head; 48         for(var i = 0;i < pos;i++){ 49             querynode = querynode.next; 50         } 51         newNode.next = querynode.next; 52         querynode.next = newNode; 53     } 54     //鏈表長度加1 55     this.length++; 56 } 57 //刪除位元置為pos的元素 58 LList.prototype.remove = function(pos){ 59     var querynode = new Node(); 60     //遍曆到pos位置的前一個元素 61     for(var i = 0,querynode = this.head;i < pos-1;i++){ 62             querynode = querynode.next; 63     } 64     //將前一個元素的next指向下一個的下一個元素,也就是跳過了中間那個元素 65     querynode.next = querynode.next.next; 66     //鏈表長度減1 67     this.length--; 68 } 69 //把位置為pos的元素值修改為element 70 LList.prototype.modify = function(pos,element){ 71     //檢查邊界 72     if(pos < 0|| pos > this.length) 73         return false; 74  75     var querynode = new Node(); 76     //尋找到pos位置 77     for(var i = 0,querynode = this.head;i < pos;i++){ 78             querynode = querynode.next; 79     } 80     //修改元素值 81     querynode.element = element; 82 } 83 //遍曆並且輸出鏈表元素的值 84 LList.prototype.display = function(){ 85     var querynode = new Node(); 86     for(var i = 0,querynode = this.head;i < this.length;i++){ 87             querynode = querynode.next; 88             //使用process.stdout從控制台輸出 89             process.stdout.write(querynode.element.toString()); 90     } 91 } 92 //以下是測試程式 93 var mylist = new LList(); 94 console.log("請輸入資料:"); 95 //使用process.stdin從控制台輸入 96 process.stdin.on("data",function(element){ 97     var arr = element.toString().split(","); 98     mylist.insert(arr[0],arr[1]); 99     mylist.insert(2,0);100     mylist.insert(3,0);101     mylist.insert(4,0);102     mylist.insert(5,0);103     console.log("鏈表元素為:")104     mylist.display();105     console.log();106     console.log("尋找位置2的元素為:")107     mylist.find(2);108     console.log();109     console.log("刪除位元置為1:")110     mylist.remove(1);111     mylist.display();112     console.log();113     console.log("修改位置為1的元素為9:")114     mylist.modify(2,9);115     mylist.display();116     console.log();117 });

測試結果如下:

從控制台輸入的是字串"1,0"

 

Javascript模仿C語言的鏈表實現(增刪改查),並且使用控制台輸入輸出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.