tailieunhanh - Lecture Algorithms and data structures: Chapter 22 - Pointers

In computer science, a balanced trees is a self-balancing tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. In this topic, we will: Introduce the idea of balance and a few examples. | Review 1 List Data Structure List operations List Implementation Array Linked List Pointer 2 Pointer Pointer Variables Dynamic Memory Allocation Functions What is a Pointer? A Pointer provides a way of accessing a variable without referring to the variable directly. The mechanism used for this purpose is the address of the variable. A variable that stores the address of another variable is called a pointer variable. 3 Pointer Variables Pointer variable: A variable that holds an address Can perform some tasks more easily with an address than by accessing memory via a symbolic name: Accessing unnamed memory locations Array manipulation etc. 4 Why Use Pointers? To operate on data stored in an array To enable convenient access within a function to large blocks data, such as arrays, that are defined outside the function. To allocate space for new variables dynamically–that is during program execution 5 Pointer Data Types and Pointer Variables Pointer variable: variable whose content is a memory address Syntax to declare pointer variable: dataType *identifier; Address of operator: Ampersand, & Dereferencing operator/Indirection operator: Asterisk, * 6 The Address-Of Operator (&) The address-of operator, &, is a unary operator that obtains the address in memory where a variable is stored. int number = 1234; int*pnumber= &number; //stores address of //number in pnumber char a = „a‟; char *pa = &a;//stores address of a in pa. 7 The Indirection Operator How pointer variable is used to access the contents of memory location? The indirection operator, *is used for this purpose. cout<< *pnumber; 8 The Indirection Operator int x= 4; 4 x 1310 px Addresses 1310 1312 1314 1316 cout<<“The number is:”<