Lecture 4 Data Structure Linked List
Lecture 4 Data Structure Linked List
Info Link
Value Explanation
Head 1200
Head -> info 45 Head is 1200 and the info in the node at
location 1200 is 45.
P = &pos;
Reference Operator
Known as Address of
When we use the name of the variable pos with the reference operator (&) we are no longer
talking about the content of the variable itself, but about its reference which is its address in
memory.
we have assigned the value 25 to variable andy (a variable
whose address in memory we have assumed to be 1776).
The variable that stores the reference to another variable (like ted in the
previous example) is what we call a pointer.
Pointers are a very powerful feature of the C++ language that has many uses in
advanced programming.
POINTER (DEREFERENCE OPERATOR (*))
p1 p2
fv sv
10 15 *p1 = 10;
1200 1210 1220 1230 1240 1250 1260
p1 p2
fv sv
10 10 *p2 = *p1;
1200 1210 1220 1230 1240 1250 1260
p1 p2
fv sv
10 20 *p1 = 20;
1200 1210 1220 1230 1240 1250 1260
p1 p2
char * terry = "hello";
memory space is reserved to contain "hello" and then a pointer to the first character of this
memory block is assigned to terry. If we imagine that "hello" is stored at the memory locations
that start at addresses 1702, we can represent the previous declaration as:
It is important to indicate that terry contains the value 1702, and not 'h' nor "hello",
although 1702 indeed is the address of both of these.
For example, we can access the fifth element of the array with any of these two expression:
*(terry+4)
terry[4]
Both expressions have a value of 'o' (the fifth element of the array).
POINTER ARITHMETICS
For example, let's assume that in a given compiler for a specific machine, char takes 1 byte,
short takes 2 bytes and long takes 4.
char *mychar;
short *myshort;
long *mylong;
and that we know that they point to memory locations 1000, 2000 and 3000 respectively.
So if we write:
mychar++;
myshort++;
mylong++;
POINTER ARITHMETICS
POINTERS TO POINTERS
char a; The new thing in this example is variable c,
char * b; which can be used in three different
levels of indirection, each one of them
char ** c; would correspond to a different value:
a = 'z';
b = &a;
c = &b;
head 45 65 83 91
head 45 65 83 91
newNode 50
head 45 65 83 91
newNode 50
List after the statement (newNode -> link = p -> link;) execute
ITEM INSERTION
45 65 83 91
head
P
newNode 50
45 65 83 91
head
P
newNode 50
List after the execution of the statement (p -> link = newNode;) followed by the
execution of the statement (newNode -> link = p -> link;)
It is clear that newNode points back to itself and the reminder of the list is lost.
ITEM INSERTION
Using two pointers, we can simplify the insertion code . Suppose q points to the
node with info 83.
head 45 65 83 91
P q
newNode 50
head
P q
50 Because we have a
newNode pointer, q, pointint to the
remaining list, the
List after the statement (p -> link = newNode) executes. remaining list is not lost.
45 65 83 91
head
P
50
q
newNode
head 45 65 83 91
P
Suppose that the node with info 83 is to be deleted from the list.
head 45 65 83 91
P
p -> link = p ->link -> link;
List after the statement (p -> link = p -> link -> link) executes.
ITEM DELETION
The node with info 83 is removed from the list.
But the memory is still occupied by this node and this memory is
inaccessible.
To deallocate the memory, we need a pointer to this node.
q = p -> link;
P -> link = q -> link;
delete q;
head 45 65 83 91
P q
head 45 65 83 91
P q
List after the statement (p -> link = q -> link; ) executes.
head 45 65 91