DSA EE Lecture 09
DSA EE Lecture 09
CS 221
Data Structures & Algorithms
Lecture-09
1
Tuesday, 08-Dec-2020
Today’s Lecture
• Lists
– Pointer
• Linked Lists
Lists
• A List is a sequence of zero or more elements of a given type
(say element_type)
• Represented by a comma-separated sequence of elements:
a1, a2,…an
Where, n >= 0 and each ai is of type element_type.
• if n >= 1,
a1 is the first element
an is the last element
• if n = 0,
we have an empty list
2
Tuesday, 08-Dec-2020
Pointer
• A quick review of the terminology and rules for pointers and the linked list
code depend on these rules:
• Pointer / Pointee: A "pointer" stores a reference to another variable
sometimes known as its "pointee".
• Alternately, a pointer may be set to the value NULL, which encodes that it
does not currently refer to a pointee.
– In C and C++ the value NULL can be used as a Boolean false.
• Reference: A reference is a value that enables a program to indirectly
access a particular datum, such as a variable or a record, in the computer's
memory or in some other storage device.
• A reference is often called a pointer or address, and is said to point to the
data.
• The reference is said to refer to the datum, and accessing the datum is
called dereferencing the reference.
3
Tuesday, 08-Dec-2020
Pointer – Cont…
• A pointer is a variable that contains the address of a variable.
• The question is why we need pointer? Or why it is so
powerful?
– Using pointer we can pass argument / reference to the functions.
• Generally we pass them by value as a copy.
– So we cannot change them.
• Passing argument using pointer, we can modify them.
int a = 50 // initialize variable a
Pointer – Cont…
• To change the value of a, the conventional way is to type
int a = 100 // new initialization
• Using pointer we can directly go to the memory location of 'a' and change
the value of without disturbing ‘a’.
• We can use pointer as
int *b; // declare pointer b
• We transfer the memory location of a to b.
b = &a; // the unary operator & gives the address of an object
4
Tuesday, 08-Dec-2020
Pointer – Cont…
• Now, we can change the value of a without accessing a.
*b = 100; // change the value of 'a' using pointer ‘b’
cout<<a; // show the output of 'a‘
• Is it possible to read and change the content of a without
accessing b?
– The answer is YES. Create a pointer of pointer
int **c; // declare a pointer to a pointer
c = &b; // transfer the address of ‘b’ to ‘c’
• It can change the value of a without disturbing a & pointer b
**c = 200; // change the value of ‘a’ using pointer to a pointer ‘c’
cout<<a; // show the output of ‘a’
Pointer – Cont…
Now the complete code is:
#include<iostream>
using namespace std;
int main()
{
int a = 50; // initialize integer variable a
cout<<"The value of 'a': "<<a<<endl; // show the output of a
int * b; // declare an integer pointer b
b = &a; // transfer the address of 'a' to pointer 'b'
*b = 100; // change the value of 'a' using pointer 'b'
cout<<"The value of 'a' using *b: "<<a<<endl; // show the output of a
int **c; // declare an integer pointer to pointer 'c'
c = &b; // transfer the address of 'b' to pointer to pointer 'c'
**c = 200; // change the value of 'a' using pointer to pointer 'c'
cout<<"The value of 'a' using **c: "<<a<<endl; // show the output of a
return 0;
}
5
Tuesday, 08-Dec-2020
Pointer – Cont…
#include<iostream>
using namespace std;
int main()
{
int a = 50; // initialize integer variable a
cout<<"Value of 'a' = "<<a<<endl; // show the output of a
cout<<"Memory address of 'a': "<<&a<<endl; // show the address of a
cout<<endl;
int * b; // declare an integer pointer b
b = &a; // transfer the address of 'a' to pointer 'b'
cout<<"Value of Pointer 'b': "<<*b<<endl; // show the output of *b
cout<<"Content of Pointer 'b': "<<b<<endl; // show the content of *b
cout<<"Memory address of Pointer 'b': "<<&b<<endl; // show the address of *b
cout<<endl;
int **c; // declare an integer pointer to a pointer
c = &b; // transfer the address of 'b' to 'c'
cout<<"Value of Pointer 'c': "<<**c<<endl; // show the output of **c
cout<<"Content of Pointer 'c': "<<c<<endl; // show the content of **c
cout<<"Memory address of Pointer 'c': "<<&c<<endl; // show the address of **c
cout<<endl;
return 0;
}
CS 221: Data Structures & Algorithms 11
Pointer – Cont…
• Output
• Observe that the memory address of a and the content of pointer b is same.
• The content of pointer c and the memory address of b is same.
6
Tuesday, 08-Dec-2020
Linked lists
• One of the fundamental data structures, and can be used to implement other
data structures.
• Different numbers of nodes and each node consists of two fields.
– The first field holds the value or data and
– The second field holds the reference to the next node or null if the linked list is
empty.
Pseudo code (The singly-linked list has one link per node)
Linkedlist Node {
data // The value or data stored in the node
next // A reference to the next node, null for last node
}
7
Tuesday, 08-Dec-2020
Summary
• Lists
– Pointer
• Linked Lists
THANK YOU