You are on page 1of 12

Linked List

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 8.1 Week No: 8 Semester:


Lecturer: Name & email
Lecture Outline

1. Linked List
2. Array vs. Linked List
3. Representation of Linked List in memory
4. Traversing a Linked List
Linked List
Definition and example

Definition: Linked list is a data structure consisting of a group of memory space


which together represent a list i.e. a sequence of data.

Each data is stored in a separate memory space/block (called cell/node)

Each memory block contains the data along with link/location/address to the
memory location for the next data in the list.
0 1 2 3 4

int

Data Link Data Link Data Link Data Link Data Link
Linked List
Array vs. Linked List

A sequence of data can also be represented as an array. But in an array, data are
stored consecutively in the memory.

For example, an array to contain 5 integer values of type int called mimo could


be represented like this:

A linked list is a sequence of data. But in a linked list the data are not stored
consecutively in the memory
Linked List
Array vs. Linked List (Representation in memory)

Address Memory
Address Memory FF00 17 FF0X
FF00 13 … …
FF01 14 FFF1 14 FFFF

FF02 11 … …
FF0X 15 NULL
FF03 17
… …
FF04 15
FF1F 13 FFF1
start
Array representation … …
FFFF 11 FF00
Linked List representation
Linked List
Applications in computer science

• Implementation of stacks and queues


• Implementation of graphs : Adjacency list representation of graphs is most
popular which uses linked list to store adjacent vertices.
• Dynamic memory allocation : We use linked list of free blocks.
• Maintaining directory of names
• Performing arithmetic operations on long integers
• Manipulation of polynomials by storing constants in the node of linked list
• representing sparse matrices
Linked List
Applications in real life problem

• Image viewer – Previous and next images are linked, hence can be accessed by
next and previous button.
• Previous and next page in web browser – We can access previous and next url
searched in web browser by pressing back and next button since, they are
linked as linked list.
• Music Player – Songs in music player are linked to previous and next song. you
can play songs either from starting or ending of the list.
Linked List
Representation of a node

Representation of a NODE in C/C++


struct ListNode{
int data;
ListNode *next;
};
ListNode node;

node

data next
Linked List
Traversal

Address Memory
FF00 17 FF0X
… …
FFF1 14 FFFF
… …
FF0X 15 NULL
… …
FF1F 13 FFF1
start
… …
FFFF 11 FF00
Linked List representation
Linked List
Traversal (Algorithm and simulation)

Algorithm
Input: Head (the address of first node)
Curr = Head
Step 1: if Curr == NULL exit otherwise access current node (with address Curr)
Step 2: move Curr to next node and go to step 1

Head
12 3 14 5 16 NULL

Curr Curr Curr Curr Curr Curr


References

1. https://en.wikipedia.org/wiki/Linked_list
Books
 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard
 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,

You might also like