0% found this document useful (0 votes)
20 views2 pages

Bài Thực Hành: Sap Xep Danh Sách Liên Ket Đơn

The document defines a struct Node to represent nodes in a linked list and a struct List to represent the list. It contains functions to initialize a list with 10 nodes containing data from 1 to 10, print the list, and sort the list in descending order by swapping the data fields of nodes when a higher value is encountered. The main function initializes a list L, prints it, calls the sorting function on it, and prints it again to show it is now in descending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Bài Thực Hành: Sap Xep Danh Sách Liên Ket Đơn

The document defines a struct Node to represent nodes in a linked list and a struct List to represent the list. It contains functions to initialize a list with 10 nodes containing data from 1 to 10, print the list, and sort the list in descending order by swapping the data fields of nodes when a higher value is encountered. The main function initializes a list L, prints it, calls the sorting function on it, and prints it again to show it is now in descending order.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Bà i thực hà nh: Sap xep danh sá ch liê n ket đơn

#include <stdio.h>
#include <conio.h>

typedef struct Node


{
int data;
Node *next;
};

typedef struct List


{
Node *first;
Node *last;
};

struct List L, L1;


struct Node *p, *q;

int khoi_tao(List &l)


{
l.first = l.last = NULL;
for (int i = 1; i <= 10; i++)
{
p = new Node;
p->data = i;
p->next = NULL;
if(l.first == NULL)
l.first = p;
else
l.last->next = p;
l.last = p;
}
}
int in_an(List l)
{
p = l.first;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}

int sap_xep(List &l)


{
int tg;
p = l.first;
while (p != NULL)
{
q = p->next;
while (q != NULL)
{
if (q->data > p->data)
{
tg = p->data;
p->data = q->data;
q->data = tg;
}
q = q->next;
}
p = p->next;
}
}

int main()
{
khoi_tao(L);
printf("Danh sach ban dau\n");
in_an(L);
printf("\nDanh sach sau khi sap xep giam dan\n");
sap_xep(L);
in_an(L);
}

You might also like