SlideShare a Scribd company logo
CS8391-DATA STRUCTURES
Unit - I
Dr. A. Kathirvel
Professor, Dept of CSE, M N M J E C, Chennai
UNIT I LINEAR DATA STRUCTURES – LIST
 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
12 July 2018
2
MNMJEC, Chennai - 600 097
12 July 2018MNMJEC, Chennai - 600 097
3
Topics
 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
12 July 2018MNMJEC, Chennai - 600 097
4
Data Structure [Wikipedia]
 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.
12 July 2018MNMJEC, Chennai - 600 097
5
Merriam-Webster's Definition
 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
12 July 2018MNMJEC, Chennai - 600 097
6
Algorithms [Wikipedia]
 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.
12 July 2018MNMJEC, Chennai - 600 097
7
Merriam-Webster's Definition
 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
12 July 2018MNMJEC, Chennai - 600 097
8
Data Structure Classification
 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
12 July 2018MNMJEC, Chennai - 600 097
9
ADT - General Concept
 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)
12 July 2018MNMJEC, Chennai - 600 097
10
ADT - General Concept
 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
12 July 2018MNMJEC, Chennai - 600 097
11
Data Types
 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
12 July 2018MNMJEC, Chennai - 600 097
12
Primitive Data Types
 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”.
12 July 2018MNMJEC, Chennai - 600 097
13
ADT Definition [Wikipedia]
 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.
12 July 2018MNMJEC, Chennai - 600 097
14
ADT Definition [Wikipedia]
 An ADT may be implemented by specific data types or
data structures, in many ways and in many
programming languages; or described in a formal
specification language.
 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.
12 July 2018MNMJEC, Chennai - 600 097
15
Definition from techforum4you
 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.
12 July 2018MNMJEC, Chennai - 600 097
16
ADT in Simple Words
 Definition:
 Is a set of operation
 Mathematical abstraction
 No implementation detail
 Example:
 Lists, sets, graphs, stacks are examples of ADT along
with their operations
12 July 2018MNMJEC, Chennai - 600 097
17
Why ADT?
 Modularity
 divide program 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
12 July 2018MNMJEC, Chennai - 600 097
18
Implementing an ADT
 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
12 July 2018MNMJEC, Chennai - 600 097
19
The List ADT
 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
 Ai-1 preceeds Ai
 Position of Ai is i
 First element is A1 called “head”
 Last element is AN called “tail”
12 July 2018MNMJEC, Chennai - 600 097
20
Operations on Lists
 MakeEmpty
 PrintList
 Find
 FindKth
 Insert
 Delete
 Next
 Previous
12 July 2018MNMJEC, Chennai - 600 097
21
List – An Example
 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
12 July 2018MNMJEC, Chennai - 600 097
22
List - Implementation
 Lists can be implemented using:
 Arrays
 Linked List
 Cursor [Linked List using Arrays]
12 July 2018MNMJEC, Chennai - 600 097
23
Arrays
 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.
12 July 2018MNMJEC, Chennai - 600 097
24
Arrays
 Any component of the array can be inspected 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)
12 July 2018MNMJEC, Chennai - 600 097
25
Different Types of Arrays
 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
12 July 2018MNMJEC, Chennai - 600 097
26
One Dimensional Static Array
 Syntax:
 ElementType arrayName [CAPACITY];
 ElementType arrayName [CAPACITY] =
{ initializer_list };
 Example in C++:
 int b [5];
 int b [5] = {19, 68, 12, 45, 72};
12 July 2018MNMJEC, Chennai - 600 097
27
Array Output Function
void display(int array[],int num_values)
{
for (int I = 0; i<num_values; i++)
cout<< array[i] << “ ”;
}
12 July 2018MNMJEC, Chennai - 600 097
28
List Implemented Using Array
12 July 2018MNMJEC, Chennai - 600 097
29
Operations On Lists
 We‟ll consider only few operations and
not all operations on Lists
 Let us consider Insert
 There are two possibilities:
 Ordered List
 Unordered List
12 July 2018MNMJEC, Chennai - 600 097
30
Insertion into an Ordered List
12 July 2018MNMJEC, Chennai - 600 097
31
Insertion in Detail
12 July 2018MNMJEC, Chennai - 600 097
32
Insertion
12 July 2018MNMJEC, Chennai - 600 097
33
Deletion
12 July 2018MNMJEC, Chennai - 600 097
34
Find / Search
 Searching is the process of looking for a 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.
12 July 2018MNMJEC, Chennai - 600 097
35
Linear Search
 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.
12 July 2018MNMJEC, Chennai - 600 097
36
Linear Search
12 July 2018MNMJEC, Chennai - 600 097
37
Linear Search Function
int LinearSearch (int a[], int n, int key)
{
int i;
for(i=0; i<n; i++)
{
if (a[i] == key)
return i;
}
return -1;
}
12 July 2018MNMJEC, Chennai - 600 097
38
Using the Function
 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)
12 July 2018MNMJEC, Chennai - 600 097
39
PrintList Operation
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]);
}
12 July 2018MNMJEC, Chennai - 600 097
40
12 July 2018MNMJEC, Chennai - 600 097
41
Implementing Deletion
12 July 2018MNMJEC, Chennai - 600 097
42
Deletion - Another Method
12 July 2018MNMJEC, Chennai - 600 097
43
PrintList O(N)
Find
Insert O(N) (on avarage half
Delete needs to be moved)
FindKth
Next O(1)
Previous
Operations Running Times
12 July 2018MNMJEC, Chennai - 600 097
44
Disadvantages of Using Arrays
 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
12 July 2018MNMJEC, Chennai - 600 097
45
Linked List
 Series of nodes
 not adjacent in memory
 contain the element and a pointer to a node
containing its succesor
 Avoids the linear cost of insertion and deletion!
12 July 2018MNMJEC, Chennai - 600 097
46
Singly Linked List
12 July 2018MNMJEC, Chennai - 600 097
47
Doubly Linked List
12 July 2018MNMJEC, Chennai - 600 097
48
Singly Linked List
12 July 2018MNMJEC, Chennai - 600 097
49
Singly-linked List - Addition
 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.
12 July 2018MNMJEC, Chennai - 600 097
50
Empty list case
 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.
12 July 2018MNMJEC, Chennai - 600 097
51
Add first
 In this case, new node is inserted right before the
current head node.
12 July 2018MNMJEC, Chennai - 600 097
52
Add First - Step 1
 It can be done in two steps:
 Update the next link of the new node, to point to the current
head node.
12 July 2018MNMJEC, Chennai - 600 097
53
Add First - Step 2
 Update head link to point to the new node.
12 July 2018MNMJEC, Chennai - 600 097
54
12 July 2018MNMJEC, Chennai - 600 097
55
Add last
 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.
12 July 2018MNMJEC, Chennai - 600 097
56
12 July 2018MNMJEC, Chennai - 600 097
57
Insert - General Case
 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.
12 July 2018MNMJEC, Chennai - 600 097
58
12 July 2018MNMJEC, Chennai - 600 097
59
Singly-linked List - Deletion
 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).
12 July 2018MNMJEC, Chennai - 600 097
60
List has only one node
 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.
12 July 2018MNMJEC, Chennai - 600 097
61
Remove First
 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.
12 July 2018MNMJEC, Chennai - 600 097
62
12 July 2018MNMJEC, Chennai - 600 097
63
Remove Last
 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.
12 July 2018MNMJEC, Chennai - 600 097
64
12 July 2018MNMJEC, Chennai - 600 097
65
Remove - General Case
 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.
12 July 2018MNMJEC, Chennai - 600 097
66
12 July 2018MNMJEC, Chennai - 600 097
67
Advantages of Using Linked Lists
 Need to know where the first node is
 the rest of the nodes can be accessed
 No need to move the elements in the list for
insertion and deletion operations
 No memory waste
12 July 2018MNMJEC, Chennai - 600 097
68
Arrays - Pros and Cons
 Pros
 Directly supported by C
 Provides random access
 Cons
 Size determined at compile time
 Inserting and deleting elements is time
consuming
12 July 2018MNMJEC, Chennai - 600 097
69
Linked Lists - Pros and Cons
 Pros
 Size determined during runtime
 Inserting and deleting elements is quick
 Cons
 No random access
 User must provide programming support
12 July 2018MNMJEC, Chennai - 600 097
70
Application of Lists
 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
12 July 2018MNMJEC, Chennai - 600 097
71
Why Doubly Linked List ?
 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.
12 July 2018MNMJEC, Chennai - 600 097
72
Doubly Linked List
 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
12 July 2018MNMJEC, Chennai - 600 097
73
Operations on Doubly Linked Lists
 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.
12 July 2018MNMJEC, Chennai - 600 097
74
Inserting Item
 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.
12 July 2018MNMJEC, Chennai - 600 097
75
Singly Linked List Insertion
12 July 2018MNMJEC, Chennai - 600 097
76
Doubly Linked List Insertion
12 July 2018MNMJEC, Chennai - 600 097
77
The Order is Important
12 July 2018MNMJEC, Chennai - 600 097
78
Doubly Linked List - Deletion
 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.
12 July 2018MNMJEC, Chennai - 600 097
79
Doubly Linked List - Deletion
12 July 2018MNMJEC, Chennai - 600 097
80
Special Cases of Deletion
 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.
PRIMITIVE 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;
PRIMITIVE 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 deletionn”);
exit(1);
}
q = p->next;
*px = q->info; /*the data of the deleted node*/
p->next = q->next;
freenode(q);
}
PRIMITIVE FUNCTIONS IN
CIRCULAR LISTS
The insertafter function: insafter( )
void insafter(NODEPTR p, int x)
{
NODEPTR q;
if(p == NULL){
printf(“void insertionn”);
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
Example
 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:%snThe Account No:%dnThe
Balance:%.2fn”, p->Name, p->AccNo, p->Balance);
p=p->next;
}
}
Example
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;
float avList(NODEPTR *plist)/*assume that plist points the
header node*/
{
int count=0;
float sum =0.0;
NODEPTR p;
p=*plist;
if((p == NULL)){
printf(“Empty listn”);
exit(1);
}
do{
sum=sum + p->info;
p =p->next;
count++;
}while(p->flag !=1);
return sum/count;
}
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:
 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.
01
01)( ee
m xaxaxA m
 

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;
int expon;
poly_pointer link;
};
poly_pointer a,b,d;
 Draw poly_nodes as:
coef expon link
123 814
 xxa
b x x x  8 3 1014 10 6
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
2. attach this term to the result, called d
3. advance the pointer to the next term in b.
 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 143 2 8 1 0 0
b.first 148 -3 10 10 6 0
p
q
(i) p->exp == q->exp
c.first 11 14 0
Operating On Polynomials
a.first 143 2 8 1 0 0
b.first 148 -3 10 10 6 0
p
q
(ii) p->exp < q->exp
c.first 11 14 0 -3 10 0
Operating On Polynomials
a.first 143 2 8 1 0 0
b.first 148 -3 10 10 6 0
p
q
(iii) p->exp > q->exp
c.first 11 14 0 -3 10 2 8 0
Polynomials
(5/9)
 Add two
polynomials
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 fulln”);
exit(1); }
temp->coef = coefficient; /* copy item to the new node */
temp->expon = exponent;
(*ptr)->link = temp; /* attach */
*ptr = temp; /* move ptr to the end of the list */
}
Polynomials (7/9)
 Analysis of padd
1. coefficient additions
0  additions  min(m, n)
where m (n) denotes the number of terms in A (B).
2. exponent comparisons
extreme case:
em-1 > fm-1 > em-2 > fm-2 > … > e1 > f1 > e0 > f0
m+n-1 comparisons
3. creation of new nodes
extreme case: maximum number of terms in d is m+n m + n
new nodes
summary: O(m+n)
))(())(( 0101
0101
ff
n
ee
m xbxbxBxaxaxA nm
 
 
Polynomials (8/9)
 A Suite for Polynomials
e(x) = a(x) * b(x) + d(x)
poly_pointer a, b, d, e;
...
a = read_poly();
b = read_poly();
d = read_poly();
temp = pmult(a, b);
e = padd(temp, d);
print_poly(e);
temp is used to hold a partial result.
By returning the nodes of temp, we
may use it to hold other polynomials
read_poly()
print_poly()
padd()
psub()
pmult()
Polynomials (9/9)
 Erase Polynomials
 erase frees 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);
}
}
Questions and Discussion
12 July 2018
109
MNMJEC, Chennai - 600 097

More Related Content

What's hot

Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
ShahDhruv21
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
karthikeyanC40
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
Md. Israil Fakir
 
Linklist
LinklistLinklist
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
linked list
linked list linked list
linked list
Mohaimin Rahat
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
NalinNishant3
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)eShikshak
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
stack presentation
stack presentationstack presentation
Linked lists
Linked listsLinked lists
Linked lists
SARITHA REDDY
 

What's hot (20)

Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Linklist
LinklistLinklist
Linklist
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
linked list
linked list linked list
linked list
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
stack presentation
stack presentationstack presentation
stack presentation
 
Linked lists
Linked listsLinked lists
Linked lists
 

Similar to UNIT I LINEAR DATA STRUCTURES – LIST

dsa3.ppt
dsa3.pptdsa3.ppt
dsa3.ppt
bajulusiraj
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
ssuser1f953d
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
ssuser1f953d
 
dsa (1).ppt
dsa (1).pptdsa (1).ppt
dsa (1).ppt
ssuser1f953d
 
Data structure
Data structureData structure
Data structureMohd Arif
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
kabhinavin
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
Shaista Qadir
 
Ijmet 10 01_141
Ijmet 10 01_141Ijmet 10 01_141
Ijmet 10 01_141
IAEME Publication
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
Rai University
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
Nimmi Weeraddana
 
List 2
List 2List 2
List 2
Rajendran
 
dsa.ppt
dsa.pptdsa.ppt
MPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace Data
MPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace DataMPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace Data
MPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace Data
IRJET Journal
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
Rai University
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
Rai University
 
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.pptLecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
iamsallauddin
 
AlgorithmsModelsNov13.pptx
AlgorithmsModelsNov13.pptxAlgorithmsModelsNov13.pptx
AlgorithmsModelsNov13.pptx
PerumalPitchandi
 
International Refereed Journal of Engineering and Science (IRJES)
International Refereed Journal of Engineering and Science (IRJES) International Refereed Journal of Engineering and Science (IRJES)
International Refereed Journal of Engineering and Science (IRJES)
irjes
 

Similar to UNIT I LINEAR DATA STRUCTURES – LIST (20)

dsa3.ppt
dsa3.pptdsa3.ppt
dsa3.ppt
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
 
dsa (1).ppt
dsa (1).pptdsa (1).ppt
dsa (1).ppt
 
Data structure
Data structureData structure
Data structure
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Ijmet 10 01_141
Ijmet 10 01_141Ijmet 10 01_141
Ijmet 10 01_141
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 
List 2
List 2List 2
List 2
 
List moderate
List   moderateList   moderate
List moderate
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
 
MPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace Data
MPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace DataMPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace Data
MPSKM Algorithm to Cluster Uneven Dimensional Time Series Subspace Data
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.pptLecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
Lecture 1 IntroductionToDataStructures_coursematerial_Draft0.01.ppt
 
AlgorithmsModelsNov13.pptx
AlgorithmsModelsNov13.pptxAlgorithmsModelsNov13.pptx
AlgorithmsModelsNov13.pptx
 
International Refereed Journal of Engineering and Science (IRJES)
International Refereed Journal of Engineering and Science (IRJES) International Refereed Journal of Engineering and Science (IRJES)
International Refereed Journal of Engineering and Science (IRJES)
 
5desc
5desc5desc
5desc
 

More from Kathirvel Ayyaswamy

22CS201 COA
22CS201 COA22CS201 COA
22CS201 COA
Kathirvel Ayyaswamy
 
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
Kathirvel Ayyaswamy
 
22CS201 COA
22CS201 COA22CS201 COA
22CS201 COA
Kathirvel Ayyaswamy
 
18CS3040_Distributed Systems
18CS3040_Distributed Systems18CS3040_Distributed Systems
18CS3040_Distributed Systems
Kathirvel Ayyaswamy
 
20CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 220CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 2
Kathirvel Ayyaswamy
 
18CS3040 Distributed System
18CS3040 Distributed System	18CS3040 Distributed System
18CS3040 Distributed System
Kathirvel Ayyaswamy
 
20CS2021 Distributed Computing
20CS2021 Distributed Computing 20CS2021 Distributed Computing
20CS2021 Distributed Computing
Kathirvel Ayyaswamy
 
20CS2021 DISTRIBUTED COMPUTING
20CS2021 DISTRIBUTED COMPUTING20CS2021 DISTRIBUTED COMPUTING
20CS2021 DISTRIBUTED COMPUTING
Kathirvel Ayyaswamy
 
18CS3040 DISTRIBUTED SYSTEMS
18CS3040 DISTRIBUTED SYSTEMS18CS3040 DISTRIBUTED SYSTEMS
18CS3040 DISTRIBUTED SYSTEMS
Kathirvel Ayyaswamy
 
Recent Trends in IoT and Sustainability
Recent Trends in IoT and SustainabilityRecent Trends in IoT and Sustainability
Recent Trends in IoT and Sustainability
Kathirvel Ayyaswamy
 
20CS2008 Computer Networks
20CS2008 Computer Networks 20CS2008 Computer Networks
20CS2008 Computer Networks
Kathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
Kathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
Kathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
Kathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security 18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
Kathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
Kathirvel Ayyaswamy
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
Kathirvel Ayyaswamy
 
20CS2008 Computer Networks
20CS2008 Computer Networks20CS2008 Computer Networks
20CS2008 Computer Networks
Kathirvel Ayyaswamy
 
20CS2008 Computer Networks
20CS2008 Computer Networks 20CS2008 Computer Networks
20CS2008 Computer Networks
Kathirvel Ayyaswamy
 
20CS024 Ethics in Information Technology
20CS024 Ethics in Information Technology20CS024 Ethics in Information Technology
20CS024 Ethics in Information Technology
Kathirvel Ayyaswamy
 

More from Kathirvel Ayyaswamy (20)

22CS201 COA
22CS201 COA22CS201 COA
22CS201 COA
 
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
22cs201 COMPUTER ORGANIZATION AND ARCHITECTURE
 
22CS201 COA
22CS201 COA22CS201 COA
22CS201 COA
 
18CS3040_Distributed Systems
18CS3040_Distributed Systems18CS3040_Distributed Systems
18CS3040_Distributed Systems
 
20CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 220CS2021-Distributed Computing module 2
20CS2021-Distributed Computing module 2
 
18CS3040 Distributed System
18CS3040 Distributed System	18CS3040 Distributed System
18CS3040 Distributed System
 
20CS2021 Distributed Computing
20CS2021 Distributed Computing 20CS2021 Distributed Computing
20CS2021 Distributed Computing
 
20CS2021 DISTRIBUTED COMPUTING
20CS2021 DISTRIBUTED COMPUTING20CS2021 DISTRIBUTED COMPUTING
20CS2021 DISTRIBUTED COMPUTING
 
18CS3040 DISTRIBUTED SYSTEMS
18CS3040 DISTRIBUTED SYSTEMS18CS3040 DISTRIBUTED SYSTEMS
18CS3040 DISTRIBUTED SYSTEMS
 
Recent Trends in IoT and Sustainability
Recent Trends in IoT and SustainabilityRecent Trends in IoT and Sustainability
Recent Trends in IoT and Sustainability
 
20CS2008 Computer Networks
20CS2008 Computer Networks 20CS2008 Computer Networks
20CS2008 Computer Networks
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security 18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security18CS2005 Cryptography and Network Security
18CS2005 Cryptography and Network Security
 
20CS2008 Computer Networks
20CS2008 Computer Networks20CS2008 Computer Networks
20CS2008 Computer Networks
 
20CS2008 Computer Networks
20CS2008 Computer Networks 20CS2008 Computer Networks
20CS2008 Computer Networks
 
20CS024 Ethics in Information Technology
20CS024 Ethics in Information Technology20CS024 Ethics in Information Technology
20CS024 Ethics in Information Technology
 

Recently uploaded

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 

Recently uploaded (20)

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 

UNIT I LINEAR DATA STRUCTURES – LIST

  • 1. CS8391-DATA STRUCTURES Unit - I Dr. A. Kathirvel Professor, Dept of CSE, M N M J E C, Chennai
  • 2. UNIT I LINEAR DATA STRUCTURES – LIST  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 12 July 2018 2 MNMJEC, Chennai - 600 097
  • 3. 12 July 2018MNMJEC, Chennai - 600 097 3 Topics  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
  • 4. 12 July 2018MNMJEC, Chennai - 600 097 4 Data Structure [Wikipedia]  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.
  • 5. 12 July 2018MNMJEC, Chennai - 600 097 5 Merriam-Webster's Definition  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
  • 6. 12 July 2018MNMJEC, Chennai - 600 097 6 Algorithms [Wikipedia]  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.
  • 7. 12 July 2018MNMJEC, Chennai - 600 097 7 Merriam-Webster's Definition  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
  • 8. 12 July 2018MNMJEC, Chennai - 600 097 8 Data Structure Classification  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
  • 9. 12 July 2018MNMJEC, Chennai - 600 097 9 ADT - General Concept  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)
  • 10. 12 July 2018MNMJEC, Chennai - 600 097 10 ADT - General Concept  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
  • 11. 12 July 2018MNMJEC, Chennai - 600 097 11 Data Types  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
  • 12. 12 July 2018MNMJEC, Chennai - 600 097 12 Primitive Data Types  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”.
  • 13. 12 July 2018MNMJEC, Chennai - 600 097 13 ADT Definition [Wikipedia]  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.
  • 14. 12 July 2018MNMJEC, Chennai - 600 097 14 ADT Definition [Wikipedia]  An ADT may be implemented by specific data types or data structures, in many ways and in many programming languages; or described in a formal specification language.  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.
  • 15. 12 July 2018MNMJEC, Chennai - 600 097 15 Definition from techforum4you  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.
  • 16. 12 July 2018MNMJEC, Chennai - 600 097 16 ADT in Simple Words  Definition:  Is a set of operation  Mathematical abstraction  No implementation detail  Example:  Lists, sets, graphs, stacks are examples of ADT along with their operations
  • 17. 12 July 2018MNMJEC, Chennai - 600 097 17 Why ADT?  Modularity  divide program 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
  • 18. 12 July 2018MNMJEC, Chennai - 600 097 18 Implementing an ADT  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
  • 19. 12 July 2018MNMJEC, Chennai - 600 097 19 The List ADT  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  Ai-1 preceeds Ai  Position of Ai is i  First element is A1 called “head”  Last element is AN called “tail”
  • 20. 12 July 2018MNMJEC, Chennai - 600 097 20 Operations on Lists  MakeEmpty  PrintList  Find  FindKth  Insert  Delete  Next  Previous
  • 21. 12 July 2018MNMJEC, Chennai - 600 097 21 List – An Example  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
  • 22. 12 July 2018MNMJEC, Chennai - 600 097 22 List - Implementation  Lists can be implemented using:  Arrays  Linked List  Cursor [Linked List using Arrays]
  • 23. 12 July 2018MNMJEC, Chennai - 600 097 23 Arrays  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.
  • 24. 12 July 2018MNMJEC, Chennai - 600 097 24 Arrays  Any component of the array can be inspected 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)
  • 25. 12 July 2018MNMJEC, Chennai - 600 097 25 Different Types of Arrays  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
  • 26. 12 July 2018MNMJEC, Chennai - 600 097 26 One Dimensional Static Array  Syntax:  ElementType arrayName [CAPACITY];  ElementType arrayName [CAPACITY] = { initializer_list };  Example in C++:  int b [5];  int b [5] = {19, 68, 12, 45, 72};
  • 27. 12 July 2018MNMJEC, Chennai - 600 097 27 Array Output Function void display(int array[],int num_values) { for (int I = 0; i<num_values; i++) cout<< array[i] << “ ”; }
  • 28. 12 July 2018MNMJEC, Chennai - 600 097 28 List Implemented Using Array
  • 29. 12 July 2018MNMJEC, Chennai - 600 097 29 Operations On Lists  We‟ll consider only few operations and not all operations on Lists  Let us consider Insert  There are two possibilities:  Ordered List  Unordered List
  • 30. 12 July 2018MNMJEC, Chennai - 600 097 30 Insertion into an Ordered List
  • 31. 12 July 2018MNMJEC, Chennai - 600 097 31 Insertion in Detail
  • 32. 12 July 2018MNMJEC, Chennai - 600 097 32 Insertion
  • 33. 12 July 2018MNMJEC, Chennai - 600 097 33 Deletion
  • 34. 12 July 2018MNMJEC, Chennai - 600 097 34 Find / Search  Searching is the process of looking for a 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.
  • 35. 12 July 2018MNMJEC, Chennai - 600 097 35 Linear Search  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.
  • 36. 12 July 2018MNMJEC, Chennai - 600 097 36 Linear Search
  • 37. 12 July 2018MNMJEC, Chennai - 600 097 37 Linear Search Function int LinearSearch (int a[], int n, int key) { int i; for(i=0; i<n; i++) { if (a[i] == key) return i; } return -1; }
  • 38. 12 July 2018MNMJEC, Chennai - 600 097 38 Using the Function  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)
  • 39. 12 July 2018MNMJEC, Chennai - 600 097 39 PrintList Operation 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. 12 July 2018MNMJEC, Chennai - 600 097 40
  • 41. 12 July 2018MNMJEC, Chennai - 600 097 41 Implementing Deletion
  • 42. 12 July 2018MNMJEC, Chennai - 600 097 42 Deletion - Another Method
  • 43. 12 July 2018MNMJEC, Chennai - 600 097 43 PrintList O(N) Find Insert O(N) (on avarage half Delete needs to be moved) FindKth Next O(1) Previous Operations Running Times
  • 44. 12 July 2018MNMJEC, Chennai - 600 097 44 Disadvantages of Using Arrays  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
  • 45. 12 July 2018MNMJEC, Chennai - 600 097 45 Linked List  Series of nodes  not adjacent in memory  contain the element and a pointer to a node containing its succesor  Avoids the linear cost of insertion and deletion!
  • 46. 12 July 2018MNMJEC, Chennai - 600 097 46 Singly Linked List
  • 47. 12 July 2018MNMJEC, Chennai - 600 097 47 Doubly Linked List
  • 48. 12 July 2018MNMJEC, Chennai - 600 097 48 Singly Linked List
  • 49. 12 July 2018MNMJEC, Chennai - 600 097 49 Singly-linked List - Addition  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.
  • 50. 12 July 2018MNMJEC, Chennai - 600 097 50 Empty list case  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.
  • 51. 12 July 2018MNMJEC, Chennai - 600 097 51 Add first  In this case, new node is inserted right before the current head node.
  • 52. 12 July 2018MNMJEC, Chennai - 600 097 52 Add First - Step 1  It can be done in two steps:  Update the next link of the new node, to point to the current head node.
  • 53. 12 July 2018MNMJEC, Chennai - 600 097 53 Add First - Step 2  Update head link to point to the new node.
  • 54. 12 July 2018MNMJEC, Chennai - 600 097 54
  • 55. 12 July 2018MNMJEC, Chennai - 600 097 55 Add last  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. 12 July 2018MNMJEC, Chennai - 600 097 56
  • 57. 12 July 2018MNMJEC, Chennai - 600 097 57 Insert - General Case  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. 12 July 2018MNMJEC, Chennai - 600 097 58
  • 59. 12 July 2018MNMJEC, Chennai - 600 097 59 Singly-linked List - Deletion  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).
  • 60. 12 July 2018MNMJEC, Chennai - 600 097 60 List has only one node  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.
  • 61. 12 July 2018MNMJEC, Chennai - 600 097 61 Remove First  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. 12 July 2018MNMJEC, Chennai - 600 097 62
  • 63. 12 July 2018MNMJEC, Chennai - 600 097 63 Remove Last  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. 12 July 2018MNMJEC, Chennai - 600 097 64
  • 65. 12 July 2018MNMJEC, Chennai - 600 097 65 Remove - General Case  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. 12 July 2018MNMJEC, Chennai - 600 097 66
  • 67. 12 July 2018MNMJEC, Chennai - 600 097 67 Advantages of Using Linked Lists  Need to know where the first node is  the rest of the nodes can be accessed  No need to move the elements in the list for insertion and deletion operations  No memory waste
  • 68. 12 July 2018MNMJEC, Chennai - 600 097 68 Arrays - Pros and Cons  Pros  Directly supported by C  Provides random access  Cons  Size determined at compile time  Inserting and deleting elements is time consuming
  • 69. 12 July 2018MNMJEC, Chennai - 600 097 69 Linked Lists - Pros and Cons  Pros  Size determined during runtime  Inserting and deleting elements is quick  Cons  No random access  User must provide programming support
  • 70. 12 July 2018MNMJEC, Chennai - 600 097 70 Application of Lists  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
  • 71. 12 July 2018MNMJEC, Chennai - 600 097 71 Why Doubly Linked List ?  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.
  • 72. 12 July 2018MNMJEC, Chennai - 600 097 72 Doubly Linked List  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
  • 73. 12 July 2018MNMJEC, Chennai - 600 097 73 Operations on Doubly Linked Lists  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.
  • 74. 12 July 2018MNMJEC, Chennai - 600 097 74 Inserting Item  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.
  • 75. 12 July 2018MNMJEC, Chennai - 600 097 75 Singly Linked List Insertion
  • 76. 12 July 2018MNMJEC, Chennai - 600 097 76 Doubly Linked List Insertion
  • 77. 12 July 2018MNMJEC, Chennai - 600 097 77 The Order is Important
  • 78. 12 July 2018MNMJEC, Chennai - 600 097 78 Doubly Linked List - Deletion  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.
  • 79. 12 July 2018MNMJEC, Chennai - 600 097 79 Doubly Linked List - Deletion
  • 80. 12 July 2018MNMJEC, Chennai - 600 097 80 Special Cases of Deletion  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.
  • 81. 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.
  • 82. Circular Linked Lists A Linear Linked List
  • 85. 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.
  • 86. PRIMITIVE 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;
  • 87. PRIMITIVE 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 deletionn”); exit(1); } q = p->next; *px = q->info; /*the data of the deleted node*/ p->next = q->next; freenode(q); }
  • 88. PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The insertafter function: insafter( ) void insafter(NODEPTR p, int x) { NODEPTR q; if(p == NULL){ printf(“void insertionn”); exit(1); } q = getnode(); q->info = x; /*the data of the inserted node*/ q->next = p->next; p->next = q; }
  • 89. 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;
  • 90. CIRCULAR LIST with header node
  • 91. 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;
  • 92. CIRCULAR LIST with header node
  • 93. Example  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.
  • 94. 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:%snThe Account No:%dnThe Balance:%.2fn”, p->Name, p->AccNo, p->Balance); p=p->next; } }
  • 95. Example 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;
  • 96. float avList(NODEPTR *plist)/*assume that plist points the header node*/ { int count=0; float sum =0.0; NODEPTR p; p=*plist; if((p == NULL)){ printf(“Empty listn”); exit(1); } do{ sum=sum + p->info; p =p->next; count++; }while(p->flag !=1); return sum/count; }
  • 97. 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:  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. 01 01)( ee m xaxaxA m   
  • 98. 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; int expon; poly_pointer link; }; poly_pointer a,b,d;  Draw poly_nodes as: coef expon link 123 814  xxa b x x x  8 3 1014 10 6
  • 99. 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 2. attach this term to the result, called d 3. advance the pointer to the next term in b.  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)
  • 101. 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 143 2 8 1 0 0 b.first 148 -3 10 10 6 0 p q (i) p->exp == q->exp c.first 11 14 0
  • 102. Operating On Polynomials a.first 143 2 8 1 0 0 b.first 148 -3 10 10 6 0 p q (ii) p->exp < q->exp c.first 11 14 0 -3 10 0
  • 103. Operating On Polynomials a.first 143 2 8 1 0 0 b.first 148 -3 10 10 6 0 p q (iii) p->exp > q->exp c.first 11 14 0 -3 10 2 8 0
  • 105. 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 fulln”); exit(1); } temp->coef = coefficient; /* copy item to the new node */ temp->expon = exponent; (*ptr)->link = temp; /* attach */ *ptr = temp; /* move ptr to the end of the list */ }
  • 106. Polynomials (7/9)  Analysis of padd 1. coefficient additions 0  additions  min(m, n) where m (n) denotes the number of terms in A (B). 2. exponent comparisons extreme case: em-1 > fm-1 > em-2 > fm-2 > … > e1 > f1 > e0 > f0 m+n-1 comparisons 3. creation of new nodes extreme case: maximum number of terms in d is m+n m + n new nodes summary: O(m+n) ))(())(( 0101 0101 ff n ee m xbxbxBxaxaxA nm    
  • 107. Polynomials (8/9)  A Suite for Polynomials e(x) = a(x) * b(x) + d(x) poly_pointer a, b, d, e; ... a = read_poly(); b = read_poly(); d = read_poly(); temp = pmult(a, b); e = padd(temp, d); print_poly(e); temp is used to hold a partial result. By returning the nodes of temp, we may use it to hold other polynomials read_poly() print_poly() padd() psub() pmult()
  • 108. Polynomials (9/9)  Erase Polynomials  erase frees 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); } }
  • 109. Questions and Discussion 12 July 2018 109 MNMJEC, Chennai - 600 097