Mustaqil ish Mavzu: Chiziqli ikki bog’lamli ro’yxatlar va ularni dasturda amalga oshirish Bajardi: Sobirov U. Guruh: tt2M21-01 Mavzu: Chiziqli ikki bog’lamli ro’yxatlar va ularni dasturda amalga oshirish Reja


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


Download 341.5 Kb.
bet6/7
Sana25.01.2023
Hajmi341.5 Kb.
#1119082
1   2   3   4   5   6   7
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 :");

Download 341.5 Kb.

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




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