0% found this document useful (0 votes)
9 views8 pages

DSA EE Lecture 09

The lecture covers lists and linked lists, explaining their structure and common operations. A list is defined as a sequence of elements, while linked lists consist of nodes with data and references to the next node. Pointers are introduced as a way to reference memory locations, enabling dynamic data manipulation.

Uploaded by

Shahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

DSA EE Lecture 09

The lecture covers lists and linked lists, explaining their structure and common operations. A list is defined as a sequence of elements, while linked lists consist of nodes with data and references to the next node. Pointers are introduced as a way to reference memory locations, enabling dynamic data manipulation.

Uploaded by

Shahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Tuesday, 08-Dec-2020

CS 221
Data Structures & Algorithms
Lecture-09

Dr. Muhammad Asif Khan


Assistant Professor

Department of Computer Science & IT


Sarhad University of Science and Information Technology,
Peshawar

Last Lecture Summary


• The Sorting Problem
– Insertion Sort

CS 221: Data Structures & Algorithms 2

1
Tuesday, 08-Dec-2020

Today’s Lecture
• Lists
– Pointer

• Linked Lists

CS 221: Data Structures & Algorithms 3

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

CS 221: Data Structures & Algorithms 4

2
Tuesday, 08-Dec-2020

Common Operations on Lists


1. INSERT(x,p,L): Insert x at position p in list L.
2. LOCATE(x,L): Return the position of x in list L.
3. RETRIEVE(p,L): Return the element at position p in list L.
4. DELETE(p,L): Delete the element at position p in list L.
5. NEXT(p,L): Return the position following p in list L.
6. PREVIOUS(p,L): Return the position preceding position p
in list L.
7. MAKENULL(L): Causes L to become an empty list and
returns position END(L).
8. FIRST(L): Returns the first position in the list L.
9. PRINTLIST(L): Print elements of L in order of occurrence.
CS 221: Data Structures & Algorithms 5

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.

CS 221: Data Structures & Algorithms 6

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

CS 221: Data Structures & Algorithms 7

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

Integer pointer b store the address of the integer variable a


CS 221: Data Structures & Algorithms 8

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’

CS 221: Data Structures & Algorithms 9

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;
}

CS 221: Data Structures & Algorithms 10

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.

CS 221: Data Structures & Algorithms 12

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
}

CS 221: Data Structures & Algorithms 13

Linked Lists – Cont…


• Linked lists have their own strengths and weaknesses,
– but they happen to be strong where arrays are weak

• A linked list is a technique of creating a list with the ability to


add, delete, or retrieve items
• A data structure in which each element is dynamically
allocated and in which elements point to each other to define a
linear relationship.
– Singly-/Doubly-linked
– Examples: Stack, Queue, Circular list

CS 221: Data Structures & Algorithms 14

7
Tuesday, 08-Dec-2020

Summary
• Lists
– Pointer

• Linked Lists

CS 221: Data Structures & Algorithms 15

THANK YOU

CS 221: Data Structures & Algorithms 16

You might also like