DATA STRUCTURE (Linked List)
BY: BEHRANG PARHIZKAR (HANI)
Technique of creating a list with the ability to
add, delete, or retrieve items.
A linked list is a data structure which is built
from structures and pointers.
Additional operation such as finding an item
can also be provided to a more elaborate list.
BASIC OPERATION
Construction: create an empty list
Empty: check if the list is empty
Traverse: go through the list or a part of it,
accessing and processing the elements in
order.
Insert: Add an item at any point in the list.
Delete: Remove an item from the list at any
point in the list.
Collection of components, called nodes.
Every nodes (except the last node) contains the
address of next node. Therefore:
Every node in in linked list has two component .
Head Info Link Info Link Info Link Info Link
Node Node Node Node
Null
The address of first node is struct nodeType
stored in head or first
{
int info;
nodeType *link;
};
Info Link
Store the information Store the address of the next node
Address of each node
head
1200 1575 1630 1823
1200 45 1575 65 1630 83 1823 91
Memory Location Data Stored
Null
1200 45
1575 65
1630 83
1823 91
Value Explanation
Head 1200
Head -> info 45 Head is 1200 and the info in the node at
location 1200 is 45.
Head -> link 1575
Head -> link -> info 65 Head->link is 1575 and the info of the
node at location 1575 is 65.
Search the list to determine whether a
1 particular item is in the list
Insert an item in the list
2
Delete an item from the list
3
These operations require the list to be
traversed. That is, given a pointer to the first
node of the list.
POINTER (REFERENCE OPERATOR (&))
The address of variable pos assigned to P.
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).
copies to ted not the value
contained in andy but a reference
to it which is the memory address
of 1776
andy = 25; copied to fred the content of
variable andy (which is 25).
fred = andy;
ted = &andy;
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 (*))
a variable which stores a reference to another variable is called a pointer.
Pointers are said to "point to" the variable whose reference they store.
Using a pointer we can directly access the value stored in the variable which
it points to.
beth = *ted;
"beth equal to value pointed by ted") beth would take the value 25, since ted is 1776,
and the value pointed by 1776 is 25.
beth = ted; // beth equal to ted ( 1776 )
beth = *ted; // beth equal to value pointed by ted ( 25 )
Notice the difference between the reference and dereference operators:
& is the reference operator and can be read as "address of"
* is the dereference operator and can be read as "value pointed by"
Thus, they have complementary (or opposite) meanings:
A variable referenced with & can be dereferenced with *.
fv sv
5 15 p1 = &firstvalue;
1200 1210 1220 1230 1240 1250 1260
p2 = &secondvalue;
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:
The pointer terry points to a
sequence of characters
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.
Suppose that we define three pointers in this compiler:
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;
c has type char** and a value of 8092
*c has type char* and a value of 7230
**c has type char and a value of 'z'
TRAVERSING A LINKED LIST
The head always must point to the first node.
We must traverse the list using another
pointer of the same type.
Current is a pointer of the same
Outputs the data stored in each node.
type as head.
Current = head; Current = head;
while (current !=NULL) while (current !=NULL)
{ {
// Process the current node cout << current -> info << ;
current = current -> link; current = current -> link;
} }
ITEM INSERTION
Linked list before item insertion
head 45 65 83 91
Point to the node
with info 65 P
struct nodeType
{
int info;
nodeType * link;
};
ITEM INSERTION
head 45 65 83 91
newNode 50
Create a node somewhere in memory newNode = new nodeType;
and stores the address of the newly
created node in newNode.
newNode -> info = 50;
Stores 50 in the info field of the new node
ITEM INSERTION
Insert the node in the linked list at the required place.
newNode -> link = p -> link;
P -> link = newNode;
After the first statement (newNode -> link = p ->
link;)
head 45 65 83 91
newNode 50
List after the statement (newNode -> link = p -> link;) execute
ITEM INSERTION
After the second statement ( p -> link = newNode;)
45 65 83 91
head
P
newNode 50
List after the statement (p -> link = newNode) executes.
ITEM INSERTION
To insert newNode in the list we use only one pointer which is p.
Suppose that we reverse the sequence of the statements and
execute the statement in following order:
P -> link = newNode;
newNode -> link = p -> link;
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
The following statements insert newNode between p and q
P -> link = newNode;
newNode -> link = q;
ITEM INSERTION
45 65 83 91
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
List after the statement (newNode -> link = q) executes.
ITEM DELETION
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
List after the statement (q = p -> link; ) executes.
ITEM DELETION
head 45 65 83 91
P q
List after the statement (p -> link = q -> link; ) executes.
head 45 65 91
List after the statement ( delete q; ) executes.