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


return; } // display linked list contents void



Yüklə 281,69 Kb.
səhifə4/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

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 ();
}
}
}



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