2. Review on pointer
• Each variable is assigned a memory slot (the
size depends on the data type) and the
variable’s data is stored there
• A pointer is a variable used to store the
address of a memory cell.
3. Cont…
• Declaration of Pointer variables
type* pointer_name;
//or
type *pointer_name;
where type is the type of data pointed to (e.g. int, char, double)
4. • The "address of " operator (&) gives the memory address of the
variable
– Usage: &variable_name
#include <iostream>
using namespace std;
void main(){
int a, b;
a = 88;
b = 100;
cout << "The address of a is: " << &a <<
endl;
cout << "The address of b is: " << &b <<
endl;
Result is:
The address of a is: 1020
The address of b is: 1024
5. Cont..
We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),
int a = 100;
int *p = &a;
cout << a << endl;
cout << &a << endl;
cout << p << " " << *p << endl;
cout << &p << endl;
Result is:
100
1024
1024 100
1032
6. Review on Structures
Structures are aggregate data types built using
elements of primitive data types.
The struct keyword creates a new user defined
data type that is used to declare variables of an
aggregate data type.
Structure variables are declared like variables of
other types.
• Syntax: struct <structure tag> <variable name>;
E.g. struct Time timeObject,
struct Time *timeptr;
7. Cont…
Accessing Members of Structure Variables
• The Dot operator (.): to access data members of
structure variables.
• The Arrow operator (->): to access data members
of pointer variables pointing to the structure.
• E.g. Print member hour of timeObject and
timeptr.
cout<< timeObject.hour; or
cout<<timeptr->hour;
TIP: timeptr->hour is the same as (*timeptr).hour.
8. What is linked list?
Linked list is a data structure used for storing
collections of data.
linked list have the following properties:
1. successive elements are connected by pointers
2. Last elements points to Null
3. Can grow or shrink in size during program
execution
4. Can be made just as long as required(until
system memory exhausts)
9. Cont…
A linked list is made up of a chain of nodes.
Each node contains:
• the data item- holds actual elements on the
list
• a pointer to the next node- address of the
next/prev node in the list
11. Array vs. linked list
Arrays
-simple and easy to use
-faster access to the elements
-fixed size
-complex position based insertion
Linked list
- Dynamic memory allocation
- Easy position based insertion
12. Dynamic memory allocation
Dynamic memory allocation is when an
executing program requests that the operating
system give it a block of main memory.
The program then uses this memory for some
purpose. Usually the purpose is to add a node to
a data structure.
In object oriented languages, dynamic memory
allocation is used to get the memory for a new
object.
13. Types of linked lists
1.singly linked list
2. Doubly linked list
14. Singly linked list
A singly linked list can be represented by a
diagram like shown blow:
15. Cont…
Start (Head): Special pointer that points to the
first node of a linked list, so that we can keep
track of the linked list.
The last node should points to NULL to show
that it is the last link in the chain (in the linked
list).
16. Creating Linked Lists in C++
• A linked list is a data structure that is built
from structures and pointers.
• It forms a chain of "nodes" with pointers
representing the links of the chain and holding
the entire thing together.
• A linked list can be represented by a diagram
like this one:
17. Cont…
According to the above example in the figure, it is
the singly linked list which has four nodes in it, each
with a link to the next node in the series (in the
linked list).
18. Defining the data structure for a
linked list
The key part of a linked list is a structure,
which holds the data for each node (the
name, address, age or whatever for the
items in the list), and, most importantly,
a pointer to the next node.
19. Cont …
Here we have given the structure of a typical
node:
struct node {
char name[20]; // Name of up to 20 letters
int age;
float height; // In metres
node *nxt;// Pointer to next node
};struct node *start_ptr = NULL;
20. Adding a node to the list
Steps
1. Allocate a new node
2. Set the node data values and make new node
point to Null
3. Make old last node’s next pointer point to
the new node
4. *make the new last node’s prev pointer point
to the old last node.(this is only for DLL)
21. Cont…
• Insert at the front
Steps
allocate a new node
Insert new element values
Make the next pointer of the new node point
to old head(start)
Update head to point to the new node
23. cont…
• Insert at the end
Steps
Allocate a new node
Set the node data values and make the next
pointer of the new node point to null
Make old last node’s next pointer point to the
new node
Update end to point to the new node
24. Cont …
• Insertion in the middle
Steps
Create a new node
Set the node data values
Break pointer connection
Reconnect the pointers
27. Double linked list(DLL)
• A doubly linked list is one where there are
links from each node in both directions:
• each node in the list has two pointers, one to
the next node and one to the previous one -
again, the ends of the list are defined by NULL
pointers.