Mavzu: Chiziqli ikki bog’lamli ro’yxatlar va ularni dasturda amalga oshirish


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



Download 281,69 Kb.
bet6/7
Sana12.12.2022
Hajmi281,69 Kb.
#883880
1   2   3   4   5   6   7
Bog'liq
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

Download 281,69 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish