Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Node {
- private int position;
- private int value;
- private static int count = 0;
- private Node head = null;
- private Node next = null;
- public Node( Node tmp) {
- if(head == null) {
- this.head = tmp;
- }
- else {
- Node item = head;
- while(item.next != null){
- item = item.next;
- }
- item.next = tmp;
- }
- }
- public Node( int position, int value) {
- this.position = position;
- this.value = value;
- count++;
- }
- public int getPosition(){
- return this.position;
- }
- public int getValue(){
- return this.value;
- }
- public void insertPos(int position, int value) {
- new Node (new Node (position, value));
- }
- public void deletePos(int position) {
- Node item = head;
- Node preNode = head; // 9 'un öncesi 7 için pre diyorum.
- preNode = searchPos(position, item);
- if(preNode == head) {
- head = head.next;
- preNode = null;
- }
- else {
- item = preNode.next.next; // 11 i veriyor.
- preNode.next = null;
- }
- }
- public Node searchPos(int hasElem, Node tmp){ // 3-> 5 -> 7 -> 9 -> 11 -> 13 -> 15 bu id ler içerisinde 9 var mı yok mu
- // var ise değer olarak true, yok ise false döndür;
- if(head.getPosition() == hasElem) {
- return tmp;
- }
- else {
- while (tmp.next != null) {
- if (tmp.next.getPosition() == hasElem) {
- return tmp;
- }
- tmp = tmp.next;
- }
- }
- return null;
- }
- public Node getElement(int position) {
- Node tmp = head;
- while(tmp.next != null) {
- if(tmp.getPosition() == position) {
- return tmp;
- }
- tmp = tmp.next;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement