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


return; } // display linked list contents void



Download 281,69 Kb.
bet4/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

return;
}
// display linked list contents
void displayList(struct Node *node)
{
//traverse the list to display each node
while (node != NULL)
{
cout<data<<"-->";
node = node->next;
}
if(node== NULL)
cout<<"null";
}
/* main program for linked list*/
int main()
{
/* empty list */
struct Node* head = NULL;
// Insert 10.
append(&head, 10);
// Insert 20 at the beginning.
push(&head, 20);
// Insert 30 at the beginning.
push(&head, 30);
// Insert 40 at the end.
append(&head, 40); //
Insert 50, after 20.
insertAfter(head->next, 50);
cout<<"Final linked list: "<displayList(head);
return 0;
}

Chiqish:
Yakuniy bog'langan ro'yxat:
30–>20–>50–>10–>40–>null
Keyinchalik, Java-da bog'langan ro'yxatni kiritish operatsiyasini amalga oshiramiz. Java tilida bog'langan ro'yxat sinf sifatida amalga oshiriladi. Quyidagi dastur mantiqan C++ dasturiga o'xshaydi, yagona farq shundaki, biz bog'langan ro'yxat uchun sinfdan foydalanamiz.

package lesson1;

import java.util.Scanner;

public class main2 {
static class LinkedList
{
Node head; // head of list

//linked list node declaration


class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}

/* Insert a new node at the front of the list */


public void push(int new_data)
{
//allocate and assign data to the node
Node newNode = new Node(new_data);

//new node becomes head of linked list


newNode.next = head;

//head points to new node


head = newNode;
}

// Given a node,prev_node insert node after prev_node


public void insertAfter(Node prev_node, int new_data)
{
//check if prev_node is null.
if (prev_node == null)
{
System.out.println("The given node is required and cannot be null");
return;
}

//allocate node and assign data to it


Node newNode = new Node(new_data);

//next of new Node is next of prev_node


newNode.next = prev_node.next;

//prev_node->next is the new node.


prev_node.next = newNode;
}
//inserts a new node at the end of the list
public void append(int new_data)
{
//allocate the node and assign data
Node newNode = new Node(new_data);

//if linked list is empty, then new node will be the head


if (head == null)
{
head = new Node(new_data);
return;
}

//set next of new node to null as this is the last node


newNode.next = null;

// if not the head node traverse the list and add it to the last


Node last = head;
while (last.next != null)
last = last.next;

//next of last becomes new node


last.next = newNode;
return;
}

//display contents of linked list


public void displayList()
{
Node pnode = head;
while (pnode != null)
{
System.out.print(pnode.data+"-->");
pnode = pnode.next;
}
if(pnode == null)
System.out.print("null");

}

}



//Main class to call linked list class functions and construct a linked list
static class Main{
public static void main(String[] args) {
/* create an empty list */
LinkedList lList = new LinkedList();

// Insert 40.


lList.append(40);

// Insert 20 at the beginning.


lList.push(20);

// Insert 10 at the beginning.


lList.push(10);

// Insert 50 at the end.


lList.append(50);

// Insert 30, after 20.


lList.insertAfter(lList.head.next, 30);

System.out.println("\nFinal linked list: ");


lList. displayList ();
}
}
}



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