Mühazirə 1 Giriş Əsas anlayışlar. "Məlumat"



Yüklə 0,95 Mb.
səhifə13/44
tarix29.04.2023
ölçüsü0,95 Mb.
#104595
növüMühazirə
1   ...   9   10   11   12   13   14   15   16   ...   44
C fakepathmu hazir struktur

İki əlaqəli siyahılar

Birəlaqəli siyahıdakı bəzi çatışmamazlıqlardan azad olmaq üçün iki əlaqəli siyhıdan istifadə olunur. Siyahıda axtarış hər iki istiqamətdə aparılır: irəli və geri. Iki əlaqəli olmasının səbəbi siyahının hər elementində iki göstəricinin (düz və əks) olmasıdır. Siyahının sonu və əvvəli də ayrıca göstəricilərlə qeyd olunur.


Çatışmamazlığı hər bir elementdə iki göstəricinin olması siyahının strukturunu mürəkkəbləşdirir və yaddaş sərfini çoxaldır. Lakin siyahı üzərində əməliyyatlarda səmərəlilik artır.
Birəlaqəlidək kimi iki əlaqəli siyahını da dövrəvi yaratmaq da mümkündür. Bunun üçün siyahının sadəcə 1-ci və sonuncu elementlərində SS(siyahının sonu)-in yerinə uyğun ünvan göstəriciləri yazılmalıdır. Bu halda isə siyahının son göstəricisi lazım olmur. Siyahıda bütün lazımi əməliyyatların aparılması üçün yalnız siyahının başlanğıc göstəricisi kifayət edir.
Bir əlaqəli siyahıda olduğu kimi iki əlaqəli siyahıda elementin daxil və xaric edilməsi alqoritmləri aparılır. Lakin bu zaman uyğun elementlərdə iki göstəricinin (əks və düz) qiymətləri dəyişdirilir. Ikiəlaqəli siyahıya aid nümunəyə baxaq.
public class DoublyLinkedList extends linkedList{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
LinkedList list = new LinkedList(); /* siyahının obyektinin yaradılması */
System.out.println("Doubly Linked List Test\n");
char ch;
do /* siyahı üzərində aparılacaq əməliyyatlar */
{
System.out.println("\nDoubly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos < 1 || pos > list.getSize() )
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" \n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
list.display(); /* siyahının çapı */
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
////////////////////////////////////////////////////////////////////////////////
public class LinkedList {
protected Node start;
protected Node end ;
public int size;
public LinkedList() /* konstruktor */
{
start = null;
end = null;
size = 0;
}
public boolean isEmpty() /* siyahının boş olduğunu yoxlayan funksiya */
{
return start == null;
}
public int getSize() /* siyahının lçüsünü qaytaran funksiya */
{
return size;
}
public void insertAtStart(int val) /* siyahının əvvəlinə element daxil etmək üçün funksiya */
{
Node nptr = new Node(val, null, null);
if(start == null)
{
start = nptr;
end = start;
}
else
{
start.setLinkPrev(nptr);
nptr.setLinkNext(start);
start = nptr;
}
size++;
}
public void insertAtEnd(int val) /* siyahının sonuna element daxil edən funksiya */
{
Node nptr = new Node(val, null, null);
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLinkPrev(end);
end.setLinkNext(nptr);
end = nptr;
}
size++;
}
public void insertAtPos(int val , int pos) /* siyahının istəniən yerinə elementi daxil edən funksiya */
{
Node nptr = new Node(val, null, null);
if (pos == 1)
{
insertAtStart(val);
return;
}
Node ptr = start;
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLinkNext();
ptr.setLinkNext(nptr);
nptr.setLinkPrev(ptr);
nptr.setLinkNext(tmp);
tmp.setLinkPrev(nptr);
}
ptr = ptr.getLinkNext();
}
size++ ;
}
public void deleteAtPos(int pos) /* siyahının istənilən yerindən elementin silinməsi */
{
if (pos == 1)
{
if (size == 1)
{
start = null;
end = null;
size = 0;
return;
}
start = start.getLinkNext();
start.setLinkPrev(null);
size--;
return ;
}
if (pos == size)
{
end = end.getLinkPrev();
end.setLinkNext(null);
size-- ;
}
Node ptr = start.getLinkNext();
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node p = ptr.getLinkPrev();
Node n = ptr.getLinkNext();
p.setLinkNext(n);
n.setLinkPrev(p);
size-- ;
return;
}
ptr = ptr.getLinkNext();
}
}
public void display() /* siyahının dolu və ya boş olduğunu göstərən funksiya */
{
System.out.print("\nDoubly Linked List = ");
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLinkNext() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ " <-> ");
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
System.out.print(ptr.getData()+ " <-> ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData()+ "\n");
}

////////////////////////////////////////////////////////////////////////////////////////////////////


class DoublyLinkedList {
private int n; // siyahıdakı elementlərin sayı
private Node pre; // birinci elementin əvvəlki göstəricisi
private Node post; // sonuncu elementin göstəricisi
public DoublyLinkedList() {
pre = new Node();
post = new Node();
pre.next = post;
post.prev = pre;
}
private class Node { // siyahı elementinin tipi
private T item;
private Node next;
private Node prev;
}
public boolean isEmpty() { return n == 0; }
public int size() { return n; }
public void add(T item) { // asiyahıya elementin daxil edilməsi
Node last = post.prev;
Node x = new Node();
x.item = item;
x.next = post;
x.prev = last;
post.prev = x;
last.next = x;
n++;
}
public ListIterator iterator() { return new DoublyLinkedListIterator(); }
private class DoublyLinkedListIterator implements ListIterator {
private Node current = pre.next;
private Node lastAccessed = null;
private int index = 0;
private DoublyLinkedListIterator() {
throw new UnsupportedOperationException("Not supported yet."); }
public boolean hasNext() { return index < n; }
public boolean hasPrevious() { return index > 0; }
public int previousIndex() { return index - 1; }
public int nextIndex() { return index; }
public T next() {
if (!hasNext()) throw new NoSuchElementException();
lastAccessed = current;
T item = current.item;
current = current.next;
index++;
return item;
}
public T previous() {
if (!hasPrevious()) throw new NoSuchElementException();
current = current.prev;
index--;
lastAccessed = current;
return current.item;
}
public void set(T item) {
if (lastAccessed == null) throw new IllegalStateException();
lastAccessed.item = item;
}
public void remove() {
if (lastAccessed == null) throw new IllegalStateException();
Node x = lastAccessed.prev;
Node y = lastAccessed.next;
x.next = y;
y.prev = x;
n--;
if (current == lastAccessed)
current = y;
else
index--;
lastAccessed = null;
}
public void add(T item) { // siyahıya elementin daxil edilməsi
Node x = current.prev;
Node y = new Node();
Node z = current;
y.item = item;
x.next = y;
y.next = z;
z.prev = y;
y.prev = x;
n++;
index++;
lastAccessed = null;
} }

Yüklə 0,95 Mb.

Dostları ilə paylaş:
1   ...   9   10   11   12   13   14   15   16   ...   44




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin