SlideShare a Scribd company logo
Data Structures and Algorithms
UNIT-II-List Structure
Syllabus
Operations on List ADT – Create, Insert, Search, Delete, Display
elements; Implementation of List ADT – Array, Cursor based and
Linked Types – Singly, Doubly, Circular; Applications - Sparse Matrix,
Polynomial Arithmetic, Joseph Problem
Operations on List ADT
ADT = properties + operations
• An ADT describes a set of objects sharing the same properties and
behaviors
• The properties of an ADT are its data (representing the internal state of each
object
• double d; -- bits representing exponent & mantissa are its data or state
• The behaviors of an ADT are its operations or functions (operations on each
instance)
• sqrt(d) / 2; //operators & functions are its behaviors
The List ADT
 A sequence of zero or more elements A1, A2, A3, … AN
 N: length of the list
 A1: first element
 AN: last element
 Ai: position i
 If N=0, then empty list
 Linearly ordered
 Ai precedes Ai+1
 Ai follows Ai-1
Operations
 makeEmpty: create an empty list
 find: locate the position of an object in a list
 list: 34,12, 52, 16, 12
 find(52)  3
 insert: insert an object to a list
 insert(x,3)  34, 12, 52, x, 16, 12
 remove: delete an element from the list
 remove(52)  34, 12, x, 16, 12
 findKth: retrieve the element at a certain position
 printList: print the list
Implementation of an ADT
• Choose a data structure to represent the ADT
• E.g. arrays, records, etc.
• Each operation associated with the ADT is implemented by one or
more subroutines
• Two standard implementations for the list ADT
• Array-based
• Linked list
Array Implementation
• Elements are stored in contiguous array positions
1D Array Representation
• 1-dimensional array x = [a, b, c, d]
• map into contiguous memory locations
2D Arrays
• The elements of a 2-dimensional array a declared as:
int a[3][4] ;
• may be shown as a table
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
2D Array Representation
Linear List Array Representation
• use a one-dimensional array called alphabet[]
• List = (a, b, c, d, e)
• Store element I of list in alphabet[i]
To add an element
To remove an element
Array Applications
• Stores Elements of Same Data Type
• Array Used for Maintaining multiple variable names using single name
• Array Can be Used for Sorting Elements
• Array Can Perform Matrix Operation
• Array Can be Used in CPU Scheduling
• Array Can be Used in Recursive Function
Array Implementation...
🞂 Requires an estimate of the maximum size of the list
⮚ waste space
🞂 printList and find: O(n)
🞂 findKth: O(k)
🞂 insert and delete: O(n)
🞂 e.g. insert at position 0 (making a new element)
🞂 requires first pushing the entire array down one spot to make room
🞂 e.g. delete at position 0
🞂 requires shifting all the elements in the list up one
🞂 On average, half of the lists needs to be moved for either
operation
• Array Declaration
• Int arr[10];
• b=10;
• int arr1[b];
• int [b+5];
• 2-D Array
• int arr2[4][5];
Dynamic array declaration
• int size_arr;
• scanf("&d",&size_arr);
• int *arr1 = (int*)malloc(sizeof(int)*size_arr);
• int p11[size];
• for(i = 0; i < size; i++ )
• { scanf("%d",&p11[i])
• }
• Or
• for(i = 0; i < size; i++ )
• { scanf("%d",(p11+i));
• }
Multi-dimensional SPARSE Matrix
• A matrix is a two-dimensional data object made of m
rows and n columns, therefore having m X n values.
When m=n, we call it a square matrix.
• There may be a situation in which a matrix contains
more number of ZERO values than NON-ZERO values.
Such matrix is known as sparse matrix.
• When a sparse matrix is represented with 2-
dimensional array, we waste lot of space to represent
that matrix.
• For example, consider a matrix of size 100 X 100
containing only 10 non-zero elements.
Contd…
• In this matrix, only 10 spaces are filled with non-zero values
and remaining spaces of matrix are filled with zero.
• totally we allocate 100 X 100 X 2 = 20000 bytes of space to
store this integer matrix.
• to access these 10 non-zero elements we have to make
scanning for 10000 times.
• If most of the elements in a matrix have the value 0, then the
matrix is called sparSe matrix.
Example For 3 X 3 Sparse Matrix:
•
| 1 0 0 |
| 0 0 0 |
| 0 4 0 |
Sparse Matrix Representations
• A sparse matrix can be represented by using TWO representations,
those are as follows...
• Triplet Representation
• Linked Representation
TRIPLET REPRESENTATION (ARRAY REPRESENTATION)
• In this representation, only non-zero values are considered along with
their row and column index values.
• The array index [0,0] stores the total number of rows, [0,1] index
stores the total number of columns and [0,1] index has the total
number of non-zero values in the sparse matrix.
• For example, consider a matrix of size 5 X 6 containing 6 number of
non-zero values.
TRIPLET REPRESENTATION
Array Implementation
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",Arr[i][j]);
}
printf("n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(Arr[i][j]!=0)
{
B1[s1][0]=Arr[i][j];
B1[s1][1]=i;
B1[s1][2]=j;
s1++;} }
}
Linked List Representation
• In linked list representation, a linked list data structure is used to
represent a sparse matrix.
• In linked list, each node has four fields. These four fields are defined
as:
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index – (row,column)
• Next node: Address of the next node
Linked List Representation
Node Declaration
Data structure and algorithm list structures
Cursor Implementation
• Some programming languages doesn’t support pointers.
• But we need linked list representation to store data.
• Solution:
• Cursor implementation – implementing linked list on an array.
• Main idea is:
• Convert a linked list based implementation to an array based
implementation.
• Instead of pointers, array index could be used to keep track of node.
• Convert the node structures (collection of data and next pointer) to
a global array of structures.
• Need to find a way to perform dynamic memory allocation
performed using malloc and free functions.
Declarations for cursor implementation of
linked lists
Initializing Cursor Space
• Cursor_space[list] =0;
• To check whether cursor_space is empty:
• To check whether p is last in a linked list

More Related Content

Similar to Data structure and algorithm list structures (20)

PPTX
DS Unit 1.pptx
chin463670
 
PPTX
data structure unit -1_170434dd7400.pptx
coc7987515756
 
PPTX
2. Array in Data Structure
Mandeep Singh
 
PPTX
arrays.pptx
NehaJain919374
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PPTX
Array
PralhadKhanal1
 
PPTX
arrays.pptx
HarmanShergill5
 
PPTX
Arrays.pptx
Koteswari Kasireddy
 
PDF
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PDF
R training2
Hellen Gakuruh
 
PPTX
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
PPSX
C Programming : Arrays
Gagan Deep
 
PDF
cluod.pdf
ssuser92d367
 
PPTX
U2.pptx Advanced Data Structures and Algorithms
snehalkulkarni78
 
PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PDF
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
PDF
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
PPTX
Data Structures - Array presentation .pptx
IshanKapoor26
 
PPTX
ARRAYS.pptx
MamataAnilgod
 
DS Unit 1.pptx
chin463670
 
data structure unit -1_170434dd7400.pptx
coc7987515756
 
2. Array in Data Structure
Mandeep Singh
 
arrays.pptx
NehaJain919374
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
arrays.pptx
HarmanShergill5
 
Arrays.pptx
Koteswari Kasireddy
 
R training2
Hellen Gakuruh
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
C Programming : Arrays
Gagan Deep
 
cluod.pdf
ssuser92d367
 
U2.pptx Advanced Data Structures and Algorithms
snehalkulkarni78
 
Unit 6. Arrays
Ashim Lamichhane
 
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
Data Structures - Array presentation .pptx
IshanKapoor26
 
ARRAYS.pptx
MamataAnilgod
 

Recently uploaded (20)

PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PDF
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
PPTX
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PPTX
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PDF
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
PDF
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PPTX
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
Engineering Quiz ShowEngineering Quiz Show
CalvinLabial
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Ad

Data structure and algorithm list structures

  • 1. Data Structures and Algorithms UNIT-II-List Structure
  • 2. Syllabus Operations on List ADT – Create, Insert, Search, Delete, Display elements; Implementation of List ADT – Array, Cursor based and Linked Types – Singly, Doubly, Circular; Applications - Sparse Matrix, Polynomial Arithmetic, Joseph Problem
  • 3. Operations on List ADT ADT = properties + operations • An ADT describes a set of objects sharing the same properties and behaviors • The properties of an ADT are its data (representing the internal state of each object • double d; -- bits representing exponent & mantissa are its data or state • The behaviors of an ADT are its operations or functions (operations on each instance) • sqrt(d) / 2; //operators & functions are its behaviors
  • 4. The List ADT  A sequence of zero or more elements A1, A2, A3, … AN  N: length of the list  A1: first element  AN: last element  Ai: position i  If N=0, then empty list  Linearly ordered  Ai precedes Ai+1  Ai follows Ai-1
  • 5. Operations  makeEmpty: create an empty list  find: locate the position of an object in a list  list: 34,12, 52, 16, 12  find(52)  3  insert: insert an object to a list  insert(x,3)  34, 12, 52, x, 16, 12  remove: delete an element from the list  remove(52)  34, 12, x, 16, 12  findKth: retrieve the element at a certain position  printList: print the list
  • 6. Implementation of an ADT • Choose a data structure to represent the ADT • E.g. arrays, records, etc. • Each operation associated with the ADT is implemented by one or more subroutines • Two standard implementations for the list ADT • Array-based • Linked list
  • 7. Array Implementation • Elements are stored in contiguous array positions
  • 8. 1D Array Representation • 1-dimensional array x = [a, b, c, d] • map into contiguous memory locations
  • 9. 2D Arrays • The elements of a 2-dimensional array a declared as: int a[3][4] ; • may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]
  • 11. Linear List Array Representation • use a one-dimensional array called alphabet[] • List = (a, b, c, d, e) • Store element I of list in alphabet[i]
  • 12. To add an element
  • 13. To remove an element
  • 14. Array Applications • Stores Elements of Same Data Type • Array Used for Maintaining multiple variable names using single name • Array Can be Used for Sorting Elements • Array Can Perform Matrix Operation • Array Can be Used in CPU Scheduling • Array Can be Used in Recursive Function
  • 15. Array Implementation... 🞂 Requires an estimate of the maximum size of the list ⮚ waste space 🞂 printList and find: O(n) 🞂 findKth: O(k) 🞂 insert and delete: O(n) 🞂 e.g. insert at position 0 (making a new element) 🞂 requires first pushing the entire array down one spot to make room 🞂 e.g. delete at position 0 🞂 requires shifting all the elements in the list up one 🞂 On average, half of the lists needs to be moved for either operation
  • 16. • Array Declaration • Int arr[10]; • b=10; • int arr1[b]; • int [b+5]; • 2-D Array • int arr2[4][5];
  • 17. Dynamic array declaration • int size_arr; • scanf("&d",&size_arr); • int *arr1 = (int*)malloc(sizeof(int)*size_arr); • int p11[size]; • for(i = 0; i < size; i++ ) • { scanf("%d",&p11[i]) • } • Or • for(i = 0; i < size; i++ ) • { scanf("%d",(p11+i)); • }
  • 18. Multi-dimensional SPARSE Matrix • A matrix is a two-dimensional data object made of m rows and n columns, therefore having m X n values. When m=n, we call it a square matrix. • There may be a situation in which a matrix contains more number of ZERO values than NON-ZERO values. Such matrix is known as sparse matrix. • When a sparse matrix is represented with 2- dimensional array, we waste lot of space to represent that matrix. • For example, consider a matrix of size 100 X 100 containing only 10 non-zero elements.
  • 19. Contd… • In this matrix, only 10 spaces are filled with non-zero values and remaining spaces of matrix are filled with zero. • totally we allocate 100 X 100 X 2 = 20000 bytes of space to store this integer matrix. • to access these 10 non-zero elements we have to make scanning for 10000 times. • If most of the elements in a matrix have the value 0, then the matrix is called sparSe matrix. Example For 3 X 3 Sparse Matrix: • | 1 0 0 | | 0 0 0 | | 0 4 0 |
  • 20. Sparse Matrix Representations • A sparse matrix can be represented by using TWO representations, those are as follows... • Triplet Representation • Linked Representation
  • 21. TRIPLET REPRESENTATION (ARRAY REPRESENTATION) • In this representation, only non-zero values are considered along with their row and column index values. • The array index [0,0] stores the total number of rows, [0,1] index stores the total number of columns and [0,1] index has the total number of non-zero values in the sparse matrix. • For example, consider a matrix of size 5 X 6 containing 6 number of non-zero values.
  • 24. Linked List Representation • In linked list representation, a linked list data structure is used to represent a sparse matrix. • In linked list, each node has four fields. These four fields are defined as: • Row: Index of row, where non-zero element is located • Column: Index of column, where non-zero element is located • Value: Value of the non zero element located at index – (row,column) • Next node: Address of the next node
  • 28. Cursor Implementation • Some programming languages doesn’t support pointers. • But we need linked list representation to store data. • Solution: • Cursor implementation – implementing linked list on an array. • Main idea is: • Convert a linked list based implementation to an array based implementation. • Instead of pointers, array index could be used to keep track of node. • Convert the node structures (collection of data and next pointer) to a global array of structures. • Need to find a way to perform dynamic memory allocation performed using malloc and free functions.
  • 29. Declarations for cursor implementation of linked lists
  • 30. Initializing Cursor Space • Cursor_space[list] =0; • To check whether cursor_space is empty: • To check whether p is last in a linked list