O’zbekiston Respublikasi Axborot Texnologiyalari va Kommunikatsiyalarini rivojlantirish Vazirligi Muhammad Al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti


while (second_last->next->next != NULL) second_last = second_last->next; // Delete the last node delete



Yüklə 281,69 Kb.
səhifə6/7
tarix18.12.2022
ölçüsü281,69 Kb.
#76058
1   2   3   4   5   6   7
Mavzu Chiziqli ikki bog’lamli ro’yxatlar va ularni dasturda ama

while (second_last->next->next != NULL)
second_last = second_last->next;
// Delete the last node
delete (second_last->next);
// set next of second_last to null
second_last->next = NULL;
return head;
}
// create linked list by adding nodes at head
void push(struct Node** head, int new_data)
{
struct Node* newNode = new Node;
newNode->data = new_data;
newNode->next = (*head);
(*head) = newNode;
}
// main function
int main()
{
/* Start with the empty list */
Node* head = NULL;
// create linked list
push(&head, 2);
push(&head, 4);
push(&head, 6);
push(&head, 8);
push(&head, 10);
Node* temp;
cout<<"Linked list created "<for (temp = head; temp != NULL; temp = temp->next)
cout << temp->data << "-->";
if(temp == NULL)
cout<<"NULL"< //delete first node
head = deleteFirstNode(head);
cout<<"Linked list after deleting head node"<for (temp = head; temp != NULL; temp = temp->next)
cout << temp->data << "-->";
if(temp == NULL)
cout<<"NULL"< //delete last node
head = removeLastNode(head);
cout<<"Linked list after deleting last node"<for (temp = head; temp != NULL; temp = temp->next)
cout << temp->data << "-->";
if(temp == NULL)
cout<<"NULL";
return 0;
}

Chiqish:
Bog'langan ro'yxat yaratildi
10–>8–>6–>4–>2–
>NULL
Bosh tugunni o'chirishdan keyin bog'langan ro'yxat
8–>6–>4–>2–
>NULL
Oxirgi tugunni o'chirishdan keyin bog'langan ro'yxat
8–>6–>4–>NULL
Keyingi - bog'langan ro'yxatdagi tugunlarni o'chirish uchun Java dasturi. Amalga oshirish mantig'i C++ dasturida qo'llaniladigan mantiq bilan bir xil. Faqatgina farq shundaki, bog'langan ro'yxat sinf sifatida e'lon qilinadi.

class Main {
// Linked list node /
static class Node {
int data;
Node next;
};
// delete first node of linked list
static Node deleteFirstNode(Node head)
{
if (head == null)
return null;
// Move the head pointer to the next node
Node temp = head;
head = head.next;
return head;
}
// Delete the last node in linked list
static Node deleteLastNode(Node head)
{
if (head == null)
return null;
if (head.next == null) {
return null;
}
// search for second last node
Node second_last = head;
while (second_last.next.next != null)
second_last = second_last.next;
// set next of second last to null
second_last.next = null;
return head;
}
// Add nodes to the head and create linked list
static Node push(Node head, int new_data)
{
Node newNode = new Node();
newNode.data = new_data;
newNode.next = (head);
(head) = newNode;
return head;
}
//main function
public static void main(String args[])
{
// Start with the empty list /
Node head = null;
//create linked list
head = push(head, 1);
head = push(head, 3);
head = push(head, 5);
head = push(head, 7);
head = push(head, 9);
Node temp;
System.out.println("Linked list created :");
for (temp = head; temp != null; temp = temp.next)
System.out.print(temp.data + "-->");
if(temp == null)
System.out.println("null");
head = deleteFirstNode(head);
System.out.println("Linked list after deleting head node :");
for (temp = head; temp != null; temp = temp.next)
System.out.print(temp.data + "-->");
if(temp == null)
System.out.println("null");
head = deleteLastNode(head);
System.out.println("Linked list after deleting last node :");
for (temp = head; temp != null; temp = temp.next)
System.out.print(temp.data + "-->");
if(temp == null)
System.out.println("null");
}
}

Chiqish:
Bog'langan ro'yxat yaratildi:
9–>7–>5–>3–>1–
>null
Bosh tugunni o'chirishdan keyin bog'langan ro'yxat:
7–>5–>3–>1–
>null
Oxirgi tugunni o'chirishdan keyin bog'langan ro'yxat:
7–>5–>3–>null

Yüklə 281,69 Kb.

Dostları ilə paylaş:
1   2   3   4   5   6   7




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

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin