You're here: Snippet Directory » HTML/JavaScript (65)
Language:

Linkedlist.js

Language: English
Programming Language: JavaScript
Published by: gutlam [not registered]
Last Update: 5/15/2006
Views: 544


Description

I implemented a linklist which is written by javascript, purpose on minimize the work load on the server side. such like rearrage the order the questions.

Code

1 var DataSource = new Array(); 2 var prevPointer = new Array(); 3 var nextPointer = new Array(); 4 var Head = null; 5 var Tail = null; 6 var CurrentPos; 7 function NodeExecution(cmd,cmdData1,cmdData2){ 8 if (arguments[0]=='add'){ 9 Add_Node(arguments[1]); 10 } 11 if (arguments[0]=='delete'){ 12 Delete_Node(arguments[1]); 13 } 14 if (arguments[0]=='order'){ 15 Insert_Node(arguments[1],arguments[2]); 16 } 17 } 18 function Add_Node(Storedata){ 19 CurrentPos=DataSource.length; 20 if (CurrentPos==0){//input a new node into the linked list 21 DataSource[0]=Storedata; 22 prevPointer[0]=null; 23 nextPointer[0]=null; 24 Head = 0; 25 Tail = 0; 26 } 27 else{ 28 for (var i=0;i<CurrentPos;i++){//try to find a node index which is empty 29 if (DataSource[i]==null){ 30 CurrentPos=i; 31 } 32 } 33 DataSource[CurrentPos]=Storedata;//input the data element first 34 if (Head==null&&Tail==null){//no node found 35 prevPointer[CurrentPos]=null; 36 nextPointer[CurrentPos]=null; 37 Head = CurrentPos; 38 Tail = CurrentPos; 39 } 40 else{//node found , alway add a node at the end of the linked list 41 prevPointer[CurrentPos]=Tail; 42 nextPointer[Tail]=CurrentPos; 43 nextPointer[CurrentPos]=null; 44 Tail = CurrentPos; 45 } 46 } 47 } 48 function Delete_Node(nodeIndex){ 49 if (prevPointer[nodeIndex]!=null){//not pointed to the Head 50 nextPointer[prevPointer[nodeIndex]]=nextPointer[nodeIndex]; 51 } 52 if (nextPointer[nodeIndex]!=null){//not pointed to the Tail 53 prevPointer[nextPointer[nodeIndex]]=prevPointer[nodeIndex]; 54 } 55 if (prevPointer[nodeIndex]==null && nextPointer[nodeIndex]==null){//this node is the last element in this linked list 56 Head=null; 57 Tail=null; 58 } 59 else { 60 if (prevPointer[nodeIndex]==null){ 61 Head=nextPointer[nodeIndex]; 62 } 63 if (nextPointer[nodeIndex]==null){ 64 Tail=prevPointer[nodeIndex]; 65 } 66 } 67 68 DataSource[nodeIndex]=null; 69 prevPointer[nodeIndex]=null; 70 nextPointer[nodeIndex]=null; 71 } 72 function Insert_Node(SelfNodeIndex,InsertNodeIndex){ 73 if (prevPointer[InsertNodeIndex]==null && nextPointer[InsertNodeIndex]==null && InsertNodeIndex!=-1){ 74 return false;//exit function 75 } 76 if (SelfNodeIndex==InsertNodeIndex){ 77 return false; 78 } 79 80 var tmpStoredata=DataSource[SelfNodeIndex]; 81 Delete_Node(SelfNodeIndex); 82 if (InsertNodeIndex==-1){//add to the end of the list 83 Add_Node(tmpStoredata); 84 } 85 else{ 86 87 DataSource[SelfNodeIndex]=tmpStoredata;//input the data element first 88 89 if (prevPointer[InsertNodeIndex]==null){//point to the head 90 Head=SelfNodeIndex; 91 } 92 else{ 93 nextPointer[prevPointer[InsertNodeIndex]]=SelfNodeIndex; 94 } 95 96 prevPointer[SelfNodeIndex]=prevPointer[InsertNodeIndex]; 97 nextPointer[SelfNodeIndex]=InsertNodeIndex; 98 prevPointer[InsertNodeIndex]=SelfNodeIndex; 99 100 101 } 102 } 103

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS