0% found this document useful (0 votes)
65 views5 pages

Code Danh Sách LK Đơn

The document discusses creating and manipulating singly linked lists in C++. It includes examples of initializing an empty linked list, adding nodes to the front and back of the list using FIFO and LIFO approaches, sorting nodes into a linked list, and removing the first node containing a given value. The code samples demonstrate basic linked list operations like traversing the list and printing out node values.
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)
65 views5 pages

Code Danh Sách LK Đơn

The document discusses creating and manipulating singly linked lists in C++. It includes examples of initializing an empty linked list, adding nodes to the front and back of the list using FIFO and LIFO approaches, sorting nodes into a linked list, and removing the first node containing a given value. The code samples demonstrate basic linked list operations like traversing the list and printing out node values.
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/ 5

Khởi tạ o danh sá ch liê n ket đơn theo cau trú c FIFO

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

typedef struct Node


{
int data;
Node *next;
};

struct Node *first, *last, *p;

int main()
{
int n;

first = NULL;

printf("So luong so nguyen n = "); scanf("%d", &n);


for (int i = 1; i <= n; i++)
{
p = new Node;
printf("Nhap so nguyen thu %d = ", i);
scanf("%d", &p->data);
p->next = NULL;

if (first == NULL) first = p;


else last->next = p;
last = p;
}

printf("Danh sach so nguyen la: ");


p = first;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
Khởi tạ o danh sá ch liê n ket đơn theo cau trú c LIFO
#include <conio.h>
#include <stdio.h>

typedef struct Node


{
int data;
Node *next;
};

struct Node *first, *p;

int main()
{
first = NULL;

int n;
printf("So luong so nguyen n = "); scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
p = new Node;
printf("Nhap so nguyen thu %d = ", i);
scanf("%d", &p->data);
p->next = first;
first = p;
}

printf("Danh sach so nguyen la: ");


p = first;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
Khởi tạ o danh sá ch liê n ket đơn tă ng dan
#include <conio.h>
#include <stdio.h>

typedef struct Node


{
int data;
Node *next;
};
struct Node *first, *p, *q, *t;

int main()
{
first = NULL;
int n;
printf("So luong so nguyen n = "); scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
p = new Node;
printf("Nhap so nguyen thu %d = ", i);
scanf("%d", &p->data);
q = first;
while ((q != NULL) && (q->data < p->data))
{
t = q;
q = q->next;
}
p->next = q;
if (q == first)
first = p;
else
t->next = p;
}

printf("Danh sach so nguyen la: ");


p = first;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
Tao mot danh sach LK don gom cac so nguyen nhap tu ban phim
Loai bo phan tu dau tien chua so nguyen duoc nhap tu ban phim khoi DS

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

typedef struct Node


{
int data;
Node *next;
};

struct Node *first, *p, *q;

int In_DS()
{
// In danh sach
p = first;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
int main()
{
first = NULL;

// Tao danh sach


int n;
printf("So luong so nguyen n = "); scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
p = new Node;
printf("Nhap so nguyen thu %d = ", i);
scanf("%d", &p->data);
p->next = first;
first = p;
}
// In danh sach truoc khi loai bo
printf("Danh sach truoc khi loai bo: ");
In_DS();
// Tim kiem phan tu thoa man
int x;
printf("\nNhap gia tri can loai bo: "); scanf("%d", &x);
p = first;
while ((p != NULL) && (p->data != x))
{
q = p;
p = p->next;
}

// Loai bo
if (p != NULL)
{
if (p == first)
first = first->next;
else
q->next = p->next;
delete p;
}
else
printf("\nKhong ton tai phan tu can xoa!");

// In danh sach sau khi loai bo


printf("\nDanh sach sau khi loai bo: ");
In_DS();
}

You might also like