Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Book.LinkedList;
- /**
- * Created by Сергей on 06.07.2015.
- */
- public class Link {
- Link(int serial) {
- this.serial = serial;
- next = null;
- }
- private int serial;
- private Link next;
- public void setSerial(int serial) {
- this.serial = serial;
- }
- public int getSerial() {
- return serial;
- }
- public void setNext(Link tmp) {
- next = tmp;
- }
- public Link getNext() {
- return next;
- }
- public void display() {
- System.out.print(serial + " ");
- }
- }
- package Book.LinkedList;
- public class LinkList {
- LinkList() {
- first = null;
- }
- private Link first;
- public boolean isEmpty() {
- return first == null;
- }
- public void insertFirst(int serial) {
- Link newLink = new Link(serial);
- if(first == null ) { newLink.setNext(null); }
- else {
- newLink.setNext(first);
- }
- first = newLink;
- }
- public void deleteFirst(){
- if( isEmpty() == false){
- first = first.getNext();
- System.out.println("The link was successfully deleted!");
- }
- else {
- System.out.println("The LinkList is empty, we can't delete element!");
- }
- }
- public boolean find(int key) {
- Link current = first;
- if (current == null) {
- System.out.println("The list is empty.");
- return false;
- }
- do {
- if (current.getSerial() == key) {
- return true;
- }
- else {
- current = current.getNext();
- }
- }
- while (current != null);
- return false;
- }
- // delete chosen/ selected element
- // пока не понятно как данное удаление влияет именно на first, почему удаляется из first
- public void delete(int key) {
- Link previous = null;
- Link current = first;
- if (current == null) {
- System.out.println("The list is empty.");
- return;
- }
- do {
- if (current.getSerial() == key) {
- if(previous == null) { first = first.getNext(); return;}
- else {
- previous.setNext(current.getNext());
- return;
- }
- }
- else {
- previous = current;
- current = current.getNext();
- }
- }
- while (current != null);
- System.out.println("There isn't element with entered serial;");
- }
- public void displayList() {
- Link current = first;
- while(current != null) {
- current.display();
- current = current.getNext();
- }
- System.out.println("The linkList was successfully displayed");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement