Unit I - Data Structure
Unit I - Data Structure
Abstract Data Types (ADTs) List ADT array based implementation – linked
list implementation –– singly linked lists- circularly linked lists- doubly-
linked lists – applications of lists –Polynomial Manipulation – All
operations (Insertion, Deletion, Merge, Traversal)
TEXTBOOKS:
1. Mark Allen Weiss, “Data Structures and
Algorithm Analysis in C”, 2nd Edition, Pearson Education,1997.
2. Reema Thareja, “Data Structures Using C”, Second Edition,
Oxford University Press, 2011
Topi
cs
3
Introduction
Definitions
Classification of Data
Structures
Arrays and Linked Lists
Abstract Data Types [ADT]
🞑 The List ADT
Array-based Implementation
Linked List Implementation
Way in which data are stored for efficient search and retrieval.
The simplest data structure is the one-
dimensional (linear) array.
Data items stored non-consecutively in memory may be linked
by pointers.
Many algorithms have been developed for storing data
efficiently
Algorithms
[Wikipedia]
6
Primitive / Non-primitive
🞑 Basic Data Structures available / Derived
from Primitive Data Structures
Homogeneous / Heterogeneous
🞑 Elements are of the same type / Different types
Static / Dynamic
🞑 memory is allocated at the time of compilation / run-time
Linear / Non-linear
🞑 Maintain a Linear relationship between element
ADT - General
Concept
9
Definition:
🞑 Isa set of operation
🞑 Mathematical abstraction
🞑 No implementation detail
Example:
🞑 Lists,sets, graphs, stacks are examples of ADT
along with their operations
Why
ADT?
17
Modularity
🞑 divideprogram into small functions
🞑 easy to debug and maintain
🞑 easy to modify
🞑 group work
Reuse
🞑 do some operations only once
Easy to change the
implementation
🞑 transparent to the program
Implementing an
ADT
18
MakeEmp
ty
PrintList
Find
FindKth
Insert
Delete
Next
Previous
List – An
Example
21
🞑 Linked List
🞑 Cursor [Linked List using
Arrays]
Array
s
23
Syntax:
🞑 ElementType arrayName [CAPACITY];
🞑 ElementType arrayName [CAPACITY] =
{ initializer_list };
Example in C++:
🞑 int b [5];
🞑 int b [5] = {19, 68, 12, 45, 72};
Array Output
Function
27
34
specific element in an array
For example, discovering whether a certain
score is included in a list of scores.
Searching, like sorting, is a
common task in computer
programming.
There are many algorithms and
data structures devoted to searching.
The most common one is the linear search.
Linear
Search
35
LinearSearch (a,n,item,loc)
Here "a" is an array of the size n.
This algorithm finds the location of the element
"item" in the array "a".
If search item is found, it sets loc to the index of
the element; otherwise, it sets loc to -1
index=linearsearch(array, num, key)
PrintList
Operation
39
PrintLi O(N
st )
Find
Insert O(N) (on avarage half
needs to be
Delet moved)
e
FindKt
h Next O(1
)
Previo
us
Disadvantages of Using
Arrays
44
Pros
🞑 Directly
supported by C
🞑 Provides random access
Cons
🞑 Size determined at compile time
🞑 Inserting and deleting elements is
time consuming
Linked Lists - Pros and
Cons
69
Pros
🞑 Size determined during runtime
🞑 Inserting and deleting elements is quick
Cons
🞑 No random access
🞑 User must provide programming
support
Application of
Lists
70
both
its successor and its predecessor
In such a case, where we need to access the node that
A Linear Linked
List
Circular Linked
Lists
Circular Linked
Lists
Circular Linked
Lists
In a circular linked list there are two methods to
know if a node is the first node or not.
🞑 Either a external pointer, list, points the first node
or
🞑 A header node is placed as the first node of the
circular list.
The header node can be separated from the
b) Assume that the list pointer points the header with the sentinel account number -99 .
header node*/
Polynomials
(1/9)
Representing Polynomials As Singly Linked
Lists
🞑 The manipulation of symbolic polynomials, has a classic example of
list processing.
🞑 In general, we want to represent the polynomial:
e e
0
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 10 10 6 0
q
(i) p->exp == q->exp
c.first 11 14 0
Operating On
Polynomials
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 10 10 6 0
c.first 11 14 0 -3 10 0
a.first 3 14 2 8 1 0 0
b.first 8 14 -3 10 10 6 0
q
c.first 11 14 0 -3 10 2 8 0
read_poly();
d = read_poly(); temp is used to hold a partial result.
temp = pmult(a, b); By returning the nodes of temp, we
may use it to hold other
e = padd(temp, d); polynomials
print_poly(e);
Polynomials
(9/9)
Erase Polynomials
🞑 erasefrees the nodes in temp
void erase (poly_pointer *ptr){
/* erase the polynomial pointed to by ptr */
poly_pointer temp;
while ( *ptr){ temp = *ptr;
*ptr = (*ptr) -> link; free(temp);
}
}
Thank You