Chapter4_Data_Structures_and_Algorithms_part1
Chapter4_Data_Structures_and_Algorithms_part1
Embedded Programmings
Chapter 4: Data
Structures & Algorithms
© DIA 2020.2
Content
4.1. Introduction of data structures
4.2. Arrays and dynamic memory management
4.3. Vector structure
4.4. List structure
4.5. Sorting algorithms
4.6. Recursion
4.7. Bitwise operation
4.8. Event-driven programming
char *s1;
s1 = string_duplicate("this is a string");
...
free(s1);
Chapter 4: Data Structures & Algorithms © DIA 2020.2 22
Common errors
The pointer points to an undefined value
“memory corruption”
The pointer points to NULL
Program halts
Free a pointer pointing to a memory block which is not
dynamic memory like stack, constant data
Not free memory after using (memory leak).
Access elements which are not in the range of the
allocated array
/* Private interface */
/* initial vector capacity */
static const int StartSize = 1;
/* geometric growth of vector capacity */
static const float GrowthRate = 1.5;
Item A Data A
Item B Data B
Item C Data C
Item X Data X
Data A Data B
Data B Data T
Data C Data C
Data X Data X
pHead pHead
Data A Data A
Data B Data B
Data C Data C
Data X Data X
Advantages:
A DLL can be traversed in both forward and backward direction
The delete operation in DLL is more efficient
We can quickly insert a new node before a given node
Basic Operations
Initializing, using it and then de-initializing the stack
Two primary operations:
push() − Pushing (storing) an element on the stack
pop() − Removing (accessing) an element from the stack
Chapter 4: Data Structures & Algorithms © DIA 2020.2 68
Stack
typedef struct Stack {
double buffer[MAXSIZE]; /* Stack buffer. */
int count; /* Number of elements in stack. */
} Stack;
1 2 3
6 7 8 9 3 4 5
6 7 8 9 A B 5
struct Dictionary t {
/* table is an array of pointers to entries */
struct Nlist *table[HASHSIZE];
};