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

PDF
DSA UNIT II ARRAY AND LIST - notes
swathirajstar
 
PPTX
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
PPTX
TMK_DSA_Unit 2 part1(array and stack).pptx
dhruvkareliya1004
 
PPT
02 Arrays And Memory Mapping
Qundeel
 
DOCX
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
PPTX
Abstract Algebra and Category Theory
Naveenkumar Muguda
 
PPTX
DSA Unit II array.pptx
sayalishivarkar1
 
PPTX
Data structure and algorithm list structures
gangaresearch21
 
DSA UNIT II ARRAY AND LIST - notes
swathirajstar
 
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
TMK_DSA_Unit 2 part1(array and stack).pptx
dhruvkareliya1004
 
02 Arrays And Memory Mapping
Qundeel
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
Abstract Algebra and Category Theory
Naveenkumar Muguda
 
DSA Unit II array.pptx
sayalishivarkar1
 
Data structure and algorithm list structures
gangaresearch21
 

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
 
Ad

Recently uploaded (20)

PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
Information Retrieval and Extraction - Module 7
premSankar19
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Software Testing Tools - names and explanation
shruti533256
 
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