Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package listod;
- public class ListoD {
- private ListElem first;
- private ListElem last;
- public ListoD() {
- first = null;
- last = null;
- }
- public boolean isEmpty() {
- return (first == null);
- }
- public void print() {
- ListElem current = first;
- while (current != null) {
- System.out.print(current + " ");
- current = current.next;
- }
- System.out.println("");
- }
- public void insert(int value) {
- ListElem newElem = new ListElem(value); //tworzymy nowy element listy
- //lista jest pusta
- if (isEmpty()) {
- first = newElem;
- last = newElem;
- } else { //lista ma co najmniej jeden element
- ListElem current = first; //aktualny element
- while (current != null) {
- if (value < current.data) {
- break;
- }
- current = current.next;
- }
- //po wyjsciu z petli w miejsce current musimy wstawic nowy element
- if(current == null) { //tzn ze bedziemy wstawiac element na koncu listy
- last.next = newElem;
- newElem.previous = last;
- last = newElem;
- } else if(current.previous == null) { //tzn ze bedziemy wstawiac element na poczatku listy
- first.previous = newElem;
- newElem.next = first;
- first = newElem;
- } else {
- //ani pierwszy ani ostatni
- newElem.next = current;
- newElem.previous = current.previous;
- current.previous.next = newElem;
- current.previous = newElem;
- }
- }
- }
- public ListElem deleteFirst() {
- if (isEmpty()) {
- return null;
- }
- ListElem temp = first;
- if (first.next == null) {
- last = null;
- } else {
- first.next.previous = null;
- }
- first = first.next;
- return temp;
- }
- public ListElem deleteLast() {
- if (isEmpty()) {
- return null;
- }
- ListElem temp = last;
- if (first.next == null) {
- first = null;
- } else {
- last.previous.next = null;
- }
- last = last.previous;
- return temp;
- }
- public ListElem find(int value){
- if(isEmpty()) return null;
- ListElem current = first;
- while(current != null) {
- if(current.data == value){
- return current;
- }
- current = current.next;
- }
- return null;
- }
- public ListElem finds(int elem) // Wyszukiwanie elementu
- {
- if (isEmpty()) return null;
- ListElem current = first; // Rozpoczynamy od pierwszego elementu
- while (current.data != elem)
- {
- if (current.next == null) return null;
- else
- current = current.next;
- }
- return current;
- }
- public static void main(String[] args) {
- ListoD theList = new ListoD();
- theList.insert(22); // wstawiamy na początek
- theList.insert(44);
- theList.insert(33);
- theList.print(); // wypisujemy zawartość listy
- theList.insert(11); // wstawiamy na koniec
- theList.insert(33);
- theList.insert(55);
- theList.insert(2);
- theList.insert(100);
- theList.insert(99);
- theList.insert(3);
- theList.insert(1);
- theList.insert(1);
- theList.insert(1);
- theList.insert(1000);
- theList.insert(1000);
- theList.insert(1000);
- theList.print(); // wypisujemy zawartość listy
- theList.deleteFirst(); // usuwamy pierwsze dwa elementy
- theList.deleteFirst();
- theList.print(); // wypisujemy ponownie
- theList.deleteLast(); //usuwamy ostatni element
- theList.print(); // wypisujemy ponownie
- System.out.println(theList.find(-9));
- System.out.println(theList.find(22));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement