Created
December 9, 2015 14:00
-
-
Save kuzemkon/53d63b0b1b6b218ccdd4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// клас списку | |
public class Link { | |
public String value; | |
public int index; | |
public Link next; | |
public SampleForm form; | |
public Link(String value, int index){ | |
this.value = value; | |
this.index = index; | |
} | |
} | |
// метод стоврення списку | |
public void addItem(String value,int afterIndex){ | |
Link currentLink = firstLink; | |
Link previousLink = firstLink; | |
while (currentLink.next != null){ | |
if(currentLink.index == afterIndex){ | |
previousLink = currentLink; | |
currentLink = currentLink.next; | |
Link newItem = new Link(value, currentLink.index); | |
previousLink.next = newItem; | |
newItem.next = currentLink; | |
while (currentLink.next != null){ | |
currentLink.index = currentLink.index+1; | |
currentLink = currentLink.next; | |
} | |
break; | |
} | |
currentLink = currentLink.next; | |
} | |
} | |
// методи додавання та видалення стеку | |
public void insertFirstLink(String value, int index){ | |
Link newLink = new Link(value, index); | |
newLink.next = firstLink; | |
firstLink = newLink; | |
} | |
public Link removeLink(int index){ | |
Link currentLink = firstLink; | |
Link previousLink = firstLink; | |
while (currentLink.index != index){ | |
if(currentLink.next == null){ | |
return null; | |
}else{ | |
previousLink = currentLink; | |
currentLink = currentLink.next; | |
} | |
} | |
if(currentLink == firstLink){ | |
firstLink = firstLink.next; | |
}else{ | |
previousLink.next = currentLink.next; | |
} | |
return currentLink; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment