Professional Documents
Culture Documents
Data Structures
Poornima.S
Definition:
Classification of DS
1) Primitive DS 2) Non-primitive DS
List Operations
printList : print the list
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 K position
Array Implementation
0 1000
1 1002
2 1004
3 1006
One contiguous block of
4 1008 memory allocated for
5 1010 the array to store 10
6 1012
7 1014
elements.
8 1016
9 1018
Memory representation
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Array Implementation
Operations
Limitations of Arrays
• Size has to be known in advance
• Memory required may be much larger than the number of
elements actually used ->memory wastage
• Inserting or deleting the elements can take time up to O(n)
0 1000
1 1002
2 1004
3 1006
One contiguous block of
4 1008 memory allocated for
5 1010 the array to store 10
6 1012
7 1014
elements.
8 1016
9 1018
Memory representation
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Dynamic Memory Allocation (Contd.)
To access any element, you need to know its address.
Now, if you know the address of the first element, you
cannot calculate the address of the rest of the elements.
This is because, all the elements are spread at random
locations in the memory.
1002 2
1008 5
1020 7
1030 11
1036 3
Memory representation
1008 5 1020
1020 7 1030
1030 11
1036 3 1008
Memory representation
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Defining Linked Lists
Linked list:
Is a dynamic data structure.
Allows memory to be allocated as and when it is required.
Consists of a chain of elements, in which each element is
referred to as a node.
Data Link
Node
10 3352
Data 10 5689
Data 10 1012
Data 10
Data
Types
i) Singly LL
- contains 2 parts : data & next address
- used for traversing the nodes in forward direction
ii) Doubly LL
- contains 3 parts : previous addr, data & next address
- used for traversing the nodes in both forward &
backward directions
Operations
i) Insertion
ii) Deletion
iii) Traverse
iv) Find
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Representing a Singly-Linked List
Operations of List
IsEmpty(): determine whether or not the list is empty
InsertNode(): insert a new node at a particular position
FindNode(): find a node with a given value
DeleteNode(): delete a node with a given value
DisplayList(): print all the nodes in the list
10 10
20 NULL 10 NULL
Temp
20 10 Addr 10
10 NULL
(head)
Temp
20 10 Addr 10
10 NULL
(head)
START
10 15 17 20
10
10 15 17 20
10
10 15 17 20
10
10 15 17 20
Insertion complete
2 3
2 3 5 7
Traversal complete
while(curr!=NULL)
{
”;
cout<<curr->data<<“
curr=curr->next;
}
}
current
current = START
current
Memory released
current = START
START = START -> next
Write functions for find() & findkth() to search the element in the list.
find()
{
findkth()
{
Linked lists are more complex to code and manage than arrays, but they have
some distinct advantages.
START
10 15 19 21 23 25 32
•The information
•The address of the next node in sequence
•The address of the previous node
struct Node
{
int data;
struct Node *prevaddr,
struct Node *nextaddr;
Insertion complete
START
10 15 9 11 23 5 12
LAST
10 15 9 11 23 5 12
current
previous = NULL
current
previous = NULL
previous current
910 6
coefficient
exponent
address of the next node
Polynomial 3: 9x6
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)
9 106 4 105