Mustaqil ish Mavzu: Dinamik turdagi ma’lumotlar tuzilmasi. Bir bog‘lamli ro‘yxat xosil qiling va uning boshidan 3ta element uchirish dasturini tuzing


Dinamik ma'lumotlar tuzilmalari tuzilishi. Afzalliklari va kamchiliklari


Download 15.96 Kb.
bet2/2
Sana22.10.2023
Hajmi15.96 Kb.
#1715098
1   2
Bog'liq
Mustaqil ish Mavzu Dinamik turdagi ma’lumotlar tuzilmasi Bir b fayllar

4.Dinamik ma'lumotlar tuzilmalari tuzilishi. Afzalliklari va kamchiliklari
Dinamik ma’lumotlar tuzilmasining har bir elementi ikki qismdan iborat:

1. Ma'lumotlar joylashtirilgan ma'lumotlar uchun maydonlar, ular uchun struktura yaratiladi. O'z navbatida, axborot maydonlarida ma'lumotlar tuzilmasi ham bo'lishi mumkin.


2. Elementlarni bir-biriga bog'laydigan bir yoki bir nechta havolalarni o'z ichiga olgan xizmat maydonlari.
Ushbu tuzilmalarni amaliy topshiriqlarda ishlatishda, faqat ma'lumot maydonlari oxirgi foydalanuvchiga ko'rinadigan qilib qo'yiladi va xizmat ko'rsatish maydonlaridan faqat dasturchi foydalanadi.
Dinamik ma’lumotlar tuzilmasining afzalliklari:
- Strukturaning o'lchami faqat mavjud RAM miqdori bilan cheklangan,
- Ma'lumotlarning tartibini o'zgartirganda ma'lumotlarni ko'chirish emas, balki faqat havolalarni to'g'rilash talab qilinadi;
- Strukturaning ajoyib moslashuvchanligi.
Kamchiliklari:
- O'zboshimchalik elementiga kirish vaqtini oldindan aniqlashning imkoni yo'q,
- Havolalarni saqlash uchun qo'shimcha xotira talab qilinadi.

Adabiyotlar
1.
Информатика: учебник – 3-е переработанное издание/ под ред. Н.В. Макаровой.- М: Финансы и статистика, 2004. – 768 с.
2.
Страуструп Б. Язык программирования C++. Специальное издание. Пер. с англ. – М.: ООО «Бином-Пресс», 2006 г. – 1104 с.: ил.
3.
Xudoyberdiyev M.X., Akbaraliyev B.B. “Ma‟lumotlat tuzilmasi va algoritmlar” fanidan amaliy mashg’ulotlar uchun topshiriqlar (uslubiy ko’rsatmalari bilan). Toshklent, 2013 y.
#include

using namespace std;


// A linked list node
struct Node
{
int data;
struct Node *next;
};
//insert a new node in front of the list
void push(struct Node** head, int node_data)
{
/* 1. create and allocate node */
struct Node* newNode = new Node;
/* 2. assign data to node */
newNode->data = node_data;
/* 3. set next of new node as head */
newNode->next = (*head);
/* 4. move the head to point to the new node */
(*head) = newNode;
}
//insert new node after a given node
void insertAfter(struct Node* prev_node, int node_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
cout<<"the given previous node is required,cannot be NULL"; return; }
/* 2. create and allocate new node */
struct Node* newNode =new Node;
/* 3. assign data to the node */
newNode->data = node_data;
/* 4. Make next of new node as next of prev_node */
newNode->next = prev_node->next;
/* 5. move the next of prev_node as new_node */
prev_node->next = newNode;
}
/* insert new node at the end of the linked list */
void append(struct Node** head, int node_data)
{
/* 1. create and allocate node */
struct Node* newNode = new Node;
struct Node *last = *head; /* used in step 5*/
/* 2. assign data to the node */
newNode->data = node_data;
/* 3. set next pointer of new node to null as its the last node*/
newNode->next = NULL;
/* 4. if list is empty, new node becomes first node */
if (*head == NULL)
{
*head = newNode;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = newNode;
return;
}
// display linked list contents
void displayList(struct Node *node)
{
//traverse the list to display each node
while (node != NULL)
{
cout";
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);
insertAfter(head->next, 50);
cout<<"Final linked list: "return 0;
}

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


Output:
Final linked list:
10–>20–>30–>40–>50–>null
http://fayllar.org
Download 15.96 Kb.

Do'stlaringiz bilan baham:
1   2




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