0% found this document useful (0 votes)
6 views11 pages

Program_LinkedList

The document contains three C programming practices focused on implementing linked lists. Each practice includes functions for initializing the list, creating nodes, inserting and deleting nodes at both the head and tail, and printing the list. The third practice also introduces functions for inserting and deleting nodes at specific positions in the list.

Uploaded by

nqpthinh0203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Program_LinkedList

The document contains three C programming practices focused on implementing linked lists. Each practice includes functions for initializing the list, creating nodes, inserting and deleting nodes at both the head and tail, and printing the list. The third practice also introduces functions for inserting and deleting nodes at specific positions in the list.

Uploaded by

nqpthinh0203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Practice 1

//Practice 1

#include <stdio.h>

#include <stdlib.h>

//Câu a

//Định nghĩa node

typedef struct NodeType {

int data;

struct NodeType* next;

}Node;

//Định nghĩa Linked list

typedef struct LinkedListType{

Node* head;

}LinkedList;

//Khởi tạo linked list

void init(LinkedList* list) {

list->head = NULL;

//Hàm tạo node mới

Node* makeNode(int data){

Node* new_node = (Node*)malloc(sizeof(Node));

if (new_node == NULL) {

printf("Lỗi cấp phát bộ nhớ!\n");

exit(1);

new_node->data = data;
new_node->next = NULL;

return new_node;

//Hàm chèn node mới vào đầu danh sách

void insertHead(int data, LinkedList* list){

Node* new_node = makeNode(data);

new_node->next = list->head;

list->head = new_node;

//Hàm in linked list

void printList(LinkedList* list){

Node* node = list->head;

while (node != NULL) {

printf("Node address: %p | ", &(node->data));

printf("data = %d| ", node->data);

printf("next node address = %p\n ", node->next);

node = node->next;

printf("\n");

void deleteHead(LinkedList* list){

Node* node = list->head;

list->head = list->head->next;

free(node);

//Hàm main
int main(){

LinkedList list;

init(&list);

//Tạo danh sách tự động

for(int i = 0; i < 10; i++)

insertHead(i, &list);

//In danh sách

printList(&list);

//Câu b -> Chèn số 15 vào đầu danh sách

insertHead(15,&list);

printf("*********The list after insert 15 at head********\n");

printList(&list);

//Câu c -> xóa phần tử ở đầu danh sáchsách

deleteHead(&list);

printf("*********The list after delete 15 at head********\n");

printList(&list);

return 0;

}
Practice 2
//Practice 2

#include <stdio.h>

#include <stdlib.h>

typedef struct NodeType {

int data;

struct NodeType* next;

}Node;

//Định nghĩa Linked list

typedef struct LinkedListType{

Node* head;

}LinkedList;

//Khởi tạo linked list

void init(LinkedList* list) {

list->head = NULL;

//Hàm tạo node mới

Node* makeNode(int data){

Node* new_node = (Node*)malloc(sizeof(Node));

if (new_node == NULL) {

printf("Lỗi cấp phát bộ nhớ!\n");

exit(1);

new_node->data = data;

new_node->next = NULL;

return new_node;
}

//Hàm chèn node mới vào đầu danh sách

void insertHead(int data, LinkedList* list){

Node* new_node = makeNode(data);

new_node->next = list->head;

list->head = new_node;

//Chèn node mới vài cuối danh sách

void insertTail(int data, LinkedList* list){

Node* new_node = makeNode(data);

if (list->head == NULL) {

list->head = new_node;

return;

Node* node = list->head;

while (node->next != NULL)

node = node->next;

node->next = new_node;

//Hàm in linked list

void printList(LinkedList* list){

Node* node = list->head;

while (node != NULL) {

printf("Node address: %p | ", &(node->data));

printf("data = %d| ", node->data);


printf("next node address = %p\n ", node->next);

node = node->next;

printf("\n");

//Xóa ở đầu

void deleteHead(LinkedList* list){

Node* node = list->head;

list->head = list->head->next;

free(node);

//Xóa ở cuối

void deleteTail(LinkedList* list){

Node* node = list->head;

while (node->next->next != NULL) {

node = node->next;

free(node->next);

node->next = NULL;

//Hàm main

int main(){

LinkedList list;

init(&list);

int array[10] ={2,12,9,0,11,3,4,8};

int i = 0, n = 7;

for(i = n; i>=0; i--)


{

insertHead(array[i], &list);

//In danh sách

printList(&list);

insertHead(19,&list);

printf("*********The list after insert 19 at head********\n");

printList(&list);

insertTail(-3,&list);

printf("*********The list after insert -3 at Tail********\n");

printList(&list);

deleteHead(&list);

printf("*********The list after delete at head********\n");

printList(&list);

printf("The list after delete the last item\n");

deleteTail(&list);

printList(&list);

return 0;

Homework
//Homework 01

#include <stdio.h>

#include <stdlib.h>
typedef struct NodeType {

int data;

struct NodeType* next;

}Node;

//Định nghĩa Linked list

typedef struct LinkedListType{

Node* head;

}LinkedList;

//Khởi tạo linked list

void init(LinkedList* list) {

list->head = NULL;

//Hàm tạo node mới

Node* makeNode(int data){

Node* new_node = (Node*)malloc(sizeof(Node));

if (new_node == NULL) {

printf("Lỗi cấp phát bộ nhớ!\n");

exit(1);

new_node->data = data;

new_node->next = NULL;

return new_node;

//Hàm chèn node mới vào đầu danh sách

void insertHead(int data, LinkedList* list){


Node* new_node = makeNode(data);

new_node->next = list->head;

list->head = new_node;

//Hàm chèn phần tử

void Insert(int data, int k, LinkedList* list){

if(k == 1){

insertHead(data, list);

return;

Node* new_node = makeNode(data);

Node* node = list->head;

int pos = 1;

while(node != NULL)

pos++;

if(pos == k){

new_node->next = node->next;

node->next = new_node;

break;

} else {

node = node->next;

if (node == NULL)

printf("Vi tri %d khong ton tai trong list\n", k);

}
}

//Hàm xóa phần tử

void deleteHead(LinkedList* list){

Node* node = list->head;

list->head = list->head->next;

free(node);

void Delete(int k, LinkedList* list){

if(k == 1){

deleteHead(list);

return;

Node* node = list->head;

int pos = 1;

while(node != NULL && pos < k){

node = node->next;

pos++;

if (node == NULL) {

printf("Vi tri %d khong ton tai trong list\n", k);

} else {

free(node->next);

node->next = node->next;

//Hàm in linked list

void printList(LinkedList* list){


Node* node = list->head;

while (node != NULL) {

printf("Node address: %p | ", &(node->data));

printf("data = %d| ", node->data);

printf("next node address = %p\n ", node->next);

node = node->next;

printf("\n");

int main()

LinkedList list;

init(&list);

int array[10] ={2,3,4,5,6,7};

int i = 0, n = 5;

for(i = n; i>=0; i--)

insertHead(array[i], &list);

printList(&list);

Insert(1,9,&list);

printList(&list);

Delete(3,&list);

printList(&list);

return 0;

You might also like