You are on page 1of 10

Advanced C

- Macro
- Linked list (Danh sách liên kết)
Macro
• Based on #define preprocessor
#define Pi 3.14
#define cmp(x,y) (((x) > (y)) ? 1 : (((x) == (y)) ? 0 : -1))
# define max(n1,n2) (n1>n2) ? n1 : n2
• Behave similar to a function

• Macro: text processing feature, expanded and replaced by macro


definition
Convention
#include <stdio.h>
#define PX printf(“x is %d.\n”, x)

Preprocessor – macro – body


Macro Example
Macro Exercises
• Write a program that prints the value of those predefined symbolic
constant:
• __LINE__
• __FILE__
• __DATE__
• __TIME__
• __STDC__
Macro Exercises
• Write a program that define a macro that accepts two params and
returns their sum
#define SUM(a,b) MACRO_BODY
- The program should allow user to enter the two numbers
- The program should then display the sum output
Linked list
• Abstract data structure
• Used to store lots of different kinds of data

• Most common and simplest data structure in Linux kernel

• Sequence of data structures which are connected together via link/nodes

• Required: Pointers & memory allocation

• Grow and shrink as needed (dynamic)


Doubly linked list
• The link pointer in the last node is set to NULL

• Node: contain data and pointers to the next/prev node


Example
• Create an application that implements linked list
• Function: Insert a char at the beginning/at the end of the list, delete a
char from the beginning or a specific one
• Primary Functions:
• insertAtBeginning
• insertAtEnd
• deleteAtBeginning
• delete
• printList
Bài tập
• Tạo một chương trình thao tác với Linked list
• Chức năng
• Thêm node vào đầu danh sách
• Thêm node vào cuối danh sách
• Thêm node tại một vị trí bất kỳ trong danh sách
• Xoá node tại một vị trí bất kỳ trong danh sách
• Cập nhật giá trị node
• Tìm kiếm vị trí của node

You might also like