0% found this document useful (0 votes)
21 views

Unit I - Data Structure

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Unit I - Data Structure

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 109

DATA STRUCTURES

UNIT I LINEAR DATA


STRUCTURES – LIST
2

 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

 Doubly Linked Lists


Data Structure
[Wikipedia]
4

 Data Structure is a particular way of storing and organizing


data in a computer so that it can be used efficiently.
 Different kinds of data structures are suited to different kinds
of applications.
 Storing and retrieving can be carried out on data stored in
both main memory and in secondary memory.
Merriam-Webster's
Definition
5

 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

 An algorithm is a step-by-step procedure for calculations.


 An algorithm is an effective method expressed as a finite list of
well-defined instructions for calculating a function.
 The transition from one state to the next is not necessarily
deterministic; some algorithms incorporate random input.
Merriam-Webster's
Definition
7

 Procedure that produces the answer to a question


or the solution to a problem in a finite number of
steps.
 An algorithm that produces a yes or no answer is
called a decision procedure; one that leads to a
solution is a computation procedure.
 Example: A mathematical formula and the
instructions in a computer program
Data Structure
Classification
8

 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

 Problem solving with a computer means


processing data
 To process data, we need to define the data type
and the operation to be performed on the data
 The definition of the data type and the definition of
the operation to be applied to the data is part of
the idea behind an Abstract Data Type (ADT)
ADT - General
Concept
10

 The user of an ADT needs only to know that a set


of operations are available for the data type, but
does not need to know how they are applied
 Several simple ADTs, such as integer, real,
character, pointer and so on, have been
implemented and are available for use in most
languages
Data
Types
11

 A data type is characterized by:


🞑A set of values
🞑 A data representation, which is common to all these
values, and
🞑 A set of operations, which can be applied uniformly to
all these values
Primitive Data
Types
12

 Languages like „C‟ provides the following


primitive data types:
🞑 boolean
🞑 char, byte, int
🞑 float, double
 Each primitive type has:
🞑A set of values
🞑 A data representation
🞑 A set of operations
 These are “set in stone”.
ADT Definition
[Wikipedia]
13

 In computer science, an abstract data type (ADT)


is a mathematical model for a certain class of
data structures that have similar behavior.
 An abstract data type is defined indirectly, only by
the operations that may be performed on it and
by mathematical constraints on the effects (and
possibly cost) of those operations.
ADT Definition
[Wikipedia]
14
 An ADT may be implemented by specific data
types or
data structures, in many ways in
programminglanguages; or described
and many a
in
specification language.
formal
example, an abstract stack could be defined by
three operations:
🞑 push, that inserts some data item onto the structure,
🞑 pop, that extracts an item from it, and

🞑 peek, that allows data on top of


the structure to be examined without removal.
Definition from
techforum4you
15

 Abstract data types or ADTs are a mathematical


specification of a set of data and the set of
operations that can be performed on the data.
 They are abstract in the sense that the focus is on
the definitions and the various operations with
their arguments.
 The actual implementation is not defined, and
does not affect the use of the ADT.
ADT in Simple
Words
16

 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

 To implement an ADT, you need to choose:


🞑A data representation
 must be able to represent all necessary values of the
ADT
 should be private

🞑 An algorithm for each of the necessary operation:


 must be consistent with the chosen representation
 all auxiliary (helper) operations that are not in the
contract should be private
 Remember: Once other people are using it
🞑 It‟s easy to add functionality
The List
ADT
19
 The List is an
🞑 Ordered sequence of data items called
elements
🞑 A1, A2, A3, …,AN is a list of size N
🞑 size of an empty list is 0
🞑 Ai+1 succeeds Ai
preceeds
🞑 Ai-1
Ai of Ai is i
🞑 Position

🞑 First element is A1 called


“head”
🞑 Last element is AN called
“tail”
Operations on
Lists
20

 MakeEmp
ty
 PrintList
 Find
 FindKth
 Insert
 Delete
 Next
 Previous
List – An
Example
21

 The elements of a list are 34, 12, 52,


16, 12
🞑 Find (52) -> 3
🞑 Insert (20, 4) -> 34, 12, 52, 20, 16, 12
🞑 Delete (52) -> 34, 12, 20, 16, 12
🞑 FindKth (3) -> 20
List -
Implementation
22

 Lists can be implemented


using:
🞑 Arrays

🞑 Linked List
🞑 Cursor [Linked List using
Arrays]
Array
s
23

 Array is a static data structure that represents a


collection of fixed number of homogeneous data
items or
 A fixed-size indexed sequence of
elements, all of the same type.
 The individual elements are typically
stored in consecutive memory locations.
 The length of the array is determined when the
array is created, and cannot be changed.
Array
 sAny component of the array can be inspected
24 or updated by using its index.
 🞑 This is an efficient operation
 🞑 O(1) = constant time
 The array indices may be integers
(C, Java) or other discrete
data types (Pascal, Ada).
 The lower bound may be zero (C, Java), one
(Fortran), or chosen by the programmer (Pascal,
Ada)
Different Types of
Arrays
25

 One-dimensional array: only one index is used


 Multi-dimensional array: array involving more than
one index
 Static array: the compiler determines how
memory will be allocated for the array
 Dynamic array: memory allocation takes
place during execution
One Dimensional Static
Array
26

 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

void display(int array[],int num_values)


{
for (int I = 0; i<num_values; i++)
cout<< array[i] << “ ”;
}
List Implemented Using
Array
28
Operations On
Lists
29

 We‟ll consideronly few operations


and not all operations on Lists
 Let us consider Insert
 There are two possibilities:
🞑 Ordered List
🞑 Unordered List
Insertion into an Ordered
List
30
Insertion in
Detail
31
Insertio
n
32
Deletio
n
33
Find /
Search
Searching is the process of looking for a

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

 The linear search approach compares the given value


with each element in the array.
 The method continues to do so until the given value
matches an element in the list or the list is exhausted
without a match being found.
 If a match is made, the linear search returns the index
of the element in the array that matches the key.
 If no match is found, the search returns -1.
Linear
Search
36
Linear Search
Function
37

int LinearSearch (int a[], int n, int


key)
{
int i;
for(i=0; i<n; i++)
{
if (a[i] == key)
return i;
}
return -1;
}
Using the
Function
38

 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

int myArray [5] = {19,68,12,45,72};


/* To print all the elements of the array
for (int i=0;i<5;i++)
{
printf("%d", myArray[i]);
}
40
Implementing
Deletion
41
Deletion - Another
Method
42
Operations Running
Times
43

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

 Need to define a size for array


🞑 High overestimate (waste of space)
 insertion and deletion is very slow
🞑 need to move elements of the list
 redundant memory space
🞑 it is difficult to estimate the size of array
Linked
List
45
 Series of
nodes
🞑 not adjacent in memory

🞑 containthe element and a pointer to a node


containing its succesor
Avoids the linear cost of insertion and
deletion!
Singly Linked
List
46
Doubly Linked
List
47
Singly Linked
List
48
Singly-linked List -
Addition
49

 Insertion into a singly-linked list has two special


cases.
 It's insertion a new node before the head (to the
very beginning of the list) and after the tail (to the
very end of the list).
 In any other case, new node is inserted in the
middle of the list and so, has a predecessor and
successor in the list.
Empty list
case
50

 When list is empty,


which is indicated by
(head == NULL)
condition, the insertion
is quite simple.
 Algorithm sets both
head and tail to point
to the new node.
Add
first
51

 In this case, new node is inserted right before


the current head node.
Add First - Step
1
52

 It can be done in two steps:


🞑 Update the next link of the new node, to point to the
current head node.
Add First - Step
2
53

🞑 Update head link to point to the new


node.
54
Add
last
55

 In this case, new node is inserted right after the


current tail node.
 It can be done in two steps:
🞑 Update the next link of the current tail node, to point to
the new node.
🞑 Update tail link to point to the new node.
56
Insert - General
Case
57

 In general case, new node is always inserted


between two nodes, which are already in the
list. Head and tail links are not updated in this
case.
 We need to know two nodes "Previous" and
"Next", between which we want to insert the
new node.
 This also can be done in two steps:
🞑 Update link of the "previous" node, to point to the new node.
🞑 Update link of the new node, to point to the "next" node.
58
Singly-linked List -
Deletion
59

 There are four cases, which can occur while


removing the node.
 We have the same four situations, but the order of
algorithm actions is opposite.
 Notice, that removal algorithm includes the
disposal of the deleted node - unnecessary in
languages with automatic garbage collection
(Java).
List has only one
node
60

 When list has only one


node, that the head
points to the same
node as the tail, the
removal is quite
simple.
 Algorithm disposes
the node, pointed by
head (or tail) and sets
both head and tail to
NULL.
Remove
First
61

 In this case, first node (current head node) is


removed from the list.
 It can be done in two steps:
🞑 Update head link to point to the node, next to the
head.
🞑 Dispose removed node.
62
Remove
Last
63

 In this case, last node (current tail node) is


removed from the list. This operation is a bit more
tricky, than removing the first node, because
algorithm should find a node, which is previous to
the tail first.
 It can be done in three steps:
🞑 Update tail link to point to the node, before the tail. In
order to find it, list should be traversed first, beginning
from the head.
🞑 Set next link of the new tail to NULL.
🞑 Dispose removed node.
64
Remove - General
Case
65

 In general case, node to be removed is always


located between two list nodes. Head and tail
links are not updated in this case.
 We need to know two nodes "Previous" and
"Next", of the node which we want to delete.
 Such a removal can be done in two steps:
🞑 Update next link of the previous node, to point to the next
node, relative to the removed node.
🞑 Dispose removed node.
66
Advantages of Using Linked
Lists
67

 Need to know where the first node is


🞑 the rest of the nodes can be accessed
 No need to movethe elements in
the list for insertion and deletion
operations
 No memory waste
Arrays - Pros and
Cons
68

 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

 Lists can be used


 To store the records sequentially
 For creation of stacks and queues
 For polynomial handling
 To maintain the sequence of operations for
do / undo in software
 To keep track of the history of web sites
visited
Why Doubly Linked
List ?
71

 given only the pointer location, we cannot access its


predecessor in
the list.
 Another task that is difficult to perform
on a linear linked list is traversing the list in
reverse.
 Doubly linked list A linked list in which each node is linked to

both
its successor and its predecessor
 In such a case, where we need to access the node that

precedes a given node, a doubly linked list is useful.


Doubly Linked
List
72

 In a doubly linked list, the nodes are linked in both


directions. Each node of a doubly linked list
contains three parts:
🞑 Info:the data stored in the node
🞑 Next: the pointer to the following node
🞑 Back: the pointer to the preceding node
Operations on Doubly Linked
Lists
73

 The algorithms for the insertion and deletion


operations on a doubly linked list are somewhat
more complicated than the corresponding
operations on a singly linked list.
 The reason is clear: There are more pointers to
keep track of in a doubly linked list.
Inserting
Item
74

 As an example, consider the Inserting an item.


 To link the new node, after a given
node, in a singly linked list, we need to
change two pointers:
🞑 newNode->next and
🞑 location->next.
 The same operation on a doubly linked list
requires four pointer changes.
Singly Linked List
Insertion
75
Doubly Linked List
Insertion
76
The Order is
Important
77
Doubly Linked List -
Deletion
78

 One useful feature of a doubly linked list is its


elimination of the need for a pointer to a node's
predecessor to delete the node.
 Through the back member, we can alter the next
member of the preceding node to make it jump
over the unwanted node.
 Then we make the back pointer of the succeeding
node point to the preceding node.
Doubly Linked List -
Deletion
79
Special Cases of
Deletion
80

 We do, however, have to be careful about the


end cases:
🞑 If location->back is NULL, we are deleting the first
node
🞑 if location->next is NULL, we are deleting the last
node.
🞑 If both location->back and location->next are NULL,
we are deleting the only node.
Circular Linked
Lists
 In linear linked lists if a list is traversed (all the
elements visited) an external pointer to the
list must be preserved in order to be able to
reference the list again.
 Circular linked lists can be used to help the
traverse the same list again and again if
needed. A circular list is very similar to the
linear list where in the circular list the pointer
of the last node points not NULL but the first
node.
Circular Linked
Lists

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

others by either heaving a sentinel value as


the info part or having a dedicated flag variable
to specify if the node is a header node or not.
FUNCTIONS IN
CIRCULAR LISTS
 The structure definition of the circular
linked lists and the linear linked list is the
same:
struct node{ int info;
struct node *next;
};
typedef struct node *NODEPTR;
FUNCTIONS IN
CIRCULAR LISTS
 The delete after and insert after functions of the linear lists and the circular
lists are almost the same.
The delete after function: delafter( )
void delafter(NODEPTR p, int *px)
{
NODEPTR q;
if((p == NULL) || (p == p->next)){ /*the empty list contains a single node
and may be pointing itself*/ printf(“void deletion\n”);
exit(1);
}
q = p->next;
*px = q->info; /*the data of the deleted node*/
p->next = q->next;
freenode(q);
}
FUNCTIONS IN
CIRCULAR LISTS
 The insertafter function: insafter( )
void insafter(NODEPTR p, int x)
{
NODEPTR q; if(p == NULL){
printf(“void insertion\n”); exit(1);
}
q = getnode();
q->info = x; /*the data of the inserted
node*/ q->next = p->next;
p->next = q;
}
CIRCULAR LIST with header
node
 The header node in a circular list can be specified
by a
sentinel value or a dedicated flag:
 Header Node with Sentinel: Assume that info

part contains positive integers. Therefore the info


part of a header node can be -1. The following
circular list is an example for a sentinel used to
represent the header node:
struct node{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
CIRCULAR LIST with header
node
CIRCULAR LIST with header
node
Header Node with Flag: In this case a extra
variable called flag can be used to represent the
header node. For example flag in the header node
can be 1, where the flag is 0 for the other nodes.
struct node{
int flag; int info;
struct node *next;
};
typedef struct node *NODEPTR;
CIRCULAR LIST with header
node
Exampl
e
 Consider a circular linked list with a header
node, where each node contains the name,
account number and the balance of a bank
customer. The header node contains a
sentinel account number to be -99.
 (a) Write an appropriate node structure
definition for the circular linked list.
 (b) Write a function to display the full records
of the customers with negative balance.
a) struct node{
char Name[15]; int AccNo;
float Balance;
struct node *next;
};
typedef struct node *NODEPTR;

b) Assume that the list pointer points the header with the sentinel account number -99 .

void DispNegBalanca(NODEPTR *plist)


{
NODEPTR p;
p=*plist;
if(p == NULL){
printf(“There is no list!\n”); exit(1);
}
p=p->next;
while(p->AccNo!=-99){
if(p->Balance < 0.0)
printf(“The Customer Name:%s\nThe Account No:%d\nThe Balance:%.2f\n”,
p->Name, p->AccNo, p->Balance);
p=p->next;
}
}
Exampl
e
Write a function that returns the average of the
numbers in a circular list. Assume that the
following node structure is used, where the flag
variable is 1 for the header node and 0 for all the
other nodes.

struct node{ int flag; float info;


struct node *next;
};
typedef struct node *NODEPTR;
{
int count=0; float
sum =0.0; NODEPTR
p;
p=*plist;
if((p == NULL)){
printf(“Empty list\n”);
exit(1);
}
do{
float avList(NODEPTR
sum=sum + p->info; p
=p->next; count++;
*plist)/*assume that plist points
}while(p->flag !=1);
return sum/count;
the
}

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

Where the ai are nonzero coefficients and the ei are



nonnegative
integer exponents such that
em-1 > em-2 > … > e1 > e0 ≧ 0 .
🞑 We will represent each term as a node containing coefficient and
exponent fields, as well as a pointer to the next term.
Polynomials
(2/9)
 Assuming that the coefficients are integers,
the type declarations are:
typedef struct poly_node *poly_pointer; typedef struct
poly_node {
int coef;
a  3x14  2x8 1
int expon;
poly_pointer
}; link;
poly_pointer a,b,d; b  8x14  3x10 10x6
Draw poly_nodes
as:
coef expon link
Polynomials
(3/9)
 Adding Polynomials
🞑 To add two polynomials,we examine their terms
starting at the nodes pointed to by a and b.
 If the exponents of the two terms are equal
1. add the two coefficients
2. create a new term for the result.
 If the exponent of the current term in a is less than b
1. create a duplicate term of b
• attach this term to the result, called d
• advance the pointer to the next term in b.
1. We take a similar action on a if a->expon > b->expon.
🞑 Figure 4.12 generating the first three term of
d = a+b (next page)
Polynomials
(4/9)
Operating On
Polynomials
 With linked lists, it is much easier to
perform operations on polynomials such
as adding and deleting.
🞑 E.g., adding two polynomials a and b

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

(ii) p->exp < q->exp


Operating On
Polynomials

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

(iii) p->exp > q->exp


Polynomi
als (5/9)
 Add two
polynomia
ls
Polynomials
(6/9)
 Attach a node to the end of a list
void attach(float coefficient, int exponent, poly_pointer *ptr){
/* create a new node with coef = coefficient and expon = exponent, attach it to
the node pointed to by ptr. Ptr is updated to point to this new node */
poly_pointer temp;
temp = (poly_pointer) malloc(sizeof(poly_node));
/* create new node */
if (IS_FULL(temp)) {
fprintf(stderr, “The memory is full\n”);
exit(1); }
temp->coef = /* copy item to the new node
coefficient; temp- */
>expon ==exponent;
(*ptr)->link temp; /* attach */
*ptr = temp; /* move ptr to the end of the list */
}
Polynomials
(7/9)
 Analysis of
padd
A( x)(  m1 x
em 1      a0 ex0 )  B( x)(  n1 x fn 1  b x0 f0
)
a b
1. coefficient additions
0  additions  min(m, n)
where m (n) denotes the number of terms in A (B).
• exponent comparisons
extreme case:
em-1 > fm-1 > em-2 > fm-2 > … > e1 > f1 > e0 > f0 m+n-1
comparisons
• creation of new nodes
extreme case: maximum number of terms in d is m+n m
+n
new nodes summary: O(m+n)
Polynomials
(8/9)
A Suite for
Polynomials e(x) =
a(x) * b(x) + d(x) read_poly()
poly_pointer a, b, d, print_poly() padd()
e; psub()
 ...
pmult()
 a = read_poly(); b =

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

You might also like