void append(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head_ref; /* used in step 5*/
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
new_node->prev = last;
return;
}
Yuqoridagi barcha jarayonlar:
#include
#include
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
void insertBefore(struct Node** head_ref, struct Node* next_node, int
new_data)
{
if (next_node == NULL) {
printf("the given next node cannot be NULL");
return;
}
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
else
(*head_ref) = new_node;
}
void printList(struct Node* node)
{
struct Node* last;
printf("\nTraversal in forward direction \n");
while (node != NULL) {
printf(" %d ", node->data);
last = node;
node = node->next;
}
printf("\nTraversal in reverse direction \n");
while (last != NULL) {
printf(" %d ", last->data);
last = last->prev;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 4);
insertBefore(&head, head->next, 8);
printf("Created DLL is: ");
printList(head);
getchar();
return 0;
}
Topshiriqlar:
12. Doubly linked list (ikki bog’langan ro’yxat) asosida ixtiyoriy N
qiymatdagi sonlar to’plami kiritilsin. Dasturning oxirgi qismiga yangi
node kiritilsin va natijalar ekranga chiqarilsin.
Dastur kodi:
#include
#include
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
void insertBefore(struct Node** head_ref, struct Node* next_node, int
new_data)
{
if (next_node == NULL) {
printf("the given next node cannot be NULL");
return;
}
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
if (new_node->prev != NULL)
new_node->prev->next = new_node;
else
(*head_ref) = new_node;
}
void printList(struct Node* node)
{
struct Node* last;
printf("\nTraversal in forward direction \n");
while (node != NULL) {
printf(" %d ", node->data);
last = node;
node = node->next;
}
printf("\nTraversal in reverse direction \n");
while (last != NULL) {
printf(" %d ", last->data);
last = last->prev;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 11);
push(&head, 5);
push(&head, 16);
insertBefore(&head, head->next, 10);
printf("Created DLL is: ");
printList(head);
getchar();
return 0;
}
Natija:
Xulosa:
Men bu laboratoriya mashg’ulotida mavzuga oid nazariy va amaliy bilimlarimni mustahkamladim. Avvalo algoritmlar tuzdik so’ngra ishni C++ dasturlash muhitida davom ettirdik. Turli xil amallarni bajarib kerakli natijalarni oldim.
Foydalanilgan adabiyotlar:
1. Алфред В. Ахо., Джон Э. Хопкрофт, Джефри Д. Ульман. Структура
данных и алгоритмы. //Учеб.пос., М.: Изд.дом: "Вильямс", 2000, — 384 с.
2. Adam DrozdekData structures and algorithms in C++. Fourth edition.
Cengage Learning, 2013.
3. Бакнелл Джулиан М. Фундаментальные алгоритмы и структуры
данных в Delphi//СПб: ООО «ДиаСофтЮП», 2003. 560с.
4. Narzullaev U.X., Qarshiev A.B., Boynazarov I.M. Ma’lumotlar tuzilmasi
va algoritmlar. //O’quv qo’llanma. Toshkent: Tafakkur nashriyoti, 2013 y. – 192
5. Лойко В.И. Структуры и алгоритмы обработки данных. Учебное
пособие для вузов. - Краснодар: КубГАУ. 2000. - 261 с., ил.
Do'stlaringiz bilan baham: |