Pointer
One of the most fundamentals in programming is understanding a pointer. It is confusing even for developers. You can also face it in some programming interview.
Let's get back in C. This old programming language is going to help us in understanding a pointer. I'm going to write some examples of it.
A pointer is a variable that stores an address of a variable in memory.
int main() {
int number = 10;
int *number_pointer = &number; // Pointer assigned to an integer
printf("%d\n", *number_pointer); // 10
printf("%p", number_pointer); // 0x7ffc5302df28
return 0;
}
The code above is a general use of pointers. The result of a memory address could be different. You could try C language at https://repl.it/languages/c.
int main() {
int number = 10;
int *number_pointer = &number;
int *change_number = number_pointer;
*change_number = 99;
printf("%d\n", number); // The result would be 99.
return 0;
}
We can change the value of number because the variable of change_number contains the address of number.
String
String is an array of characters.
int main() {
char *string = "Hello World";
while (*string != '\0') {
printf("%c", *string);
string++;
}
return 0;
}
We just printed the characters or string by incrementing the pointer.
void reverse(char *string)
{
int length = 0;
char *start, *end, temporary;
start = string;
end = string;
while (*end != '\0') {
if (length > 0) end++;
length++;
}
end--;
length--;
for (int index = 0; index < length / 2; index++) {
temporary = *end;
*end = *start;
*start = temporary;
start++;
end--;
}
}
int main() {
char string[] = "Hello World";
printf("%s\n", string);
reverse(string);
printf("%s", string);
return 0;
}
Reverse string. The most common question in the programming interview.
Linked List
Linked List is a linear collection of data elements.
struct node {
int data;
struct node *next;
};
We built a struct to contain data of integer and a pointer to next struct.
struct node a, b, c, d;
a.data = 1;
a.next = &b;
b.data = 2;
b.next = &c;
c.data = 3;
c.next = &d;
d.data = 4;
d.next = NULL;
That is how we built the linked list.
int get_length(struct node *list) {
int length = 0;
while (list != NULL) {
length++;
list = list->next;
}
return length;
}
Count the length of the linked list. Almost similar to string.
struct node *reverse(struct node *list) {
if (
list == NULL ||
list->next == NULL
) {
return list;
}
struct node *current = list->next;
struct node *previous = list;
previous->next = NULL;
while (current != NULL) {
struct node *next = current->next;
current->next = previous;
previous = current;
current = next;
}
return previous;
}
Reverse Linked List. One of the most common questions in a programming interview about Linked List.
That is all I want to cover. A simple definition then followed by common examples. The most important is understanding how the pointer works.
To understand more is to practice more. There is a lot of common questions in the programming interview about pointer that I didn't cover here. Feel free to solve them.
References:
- https://en.wikipedia.org/wiki/Pointer_(computer_programming)
- https://en.wikipedia.org/wiki/Linked_list
- https://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays
- http://cslibrary.stanford.edu/