SlideShare una empresa de Scribd logo
1 de 41
Descargar para leer sin conexión
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).
Elavarasi.K
AP/CSE
S.A Engineering College
06-06-2018 CS8351 - DATA STRUCTURES 1
Introduction to DS
• Data structures are generally based on the ability of a computer to
fetch and store data at any place in its memory, specified by a pointer
• Thus, the array and record data structures are based on computing the
addresses of data items with arithmetic operations
• What is Program?
• A Set of Instructions
Data Structures + Algorithms
Data Structure = A Container stores Data
Algorithm = Logic + Control
CS8351 - DATA STRUCTURES06-06-2018 2
• Data Structure
• Data: are simply a value are set of values of different type which is called data
types like string, integer, char etc.
• Structure: Way of organizing information, so that it is easier to use
In simple words we can define data structures as
• Its a way organizing data in such a way so that data can be easier to use.
• Data Structure ..
• A data structure is a particular way of organizing data in a computer so that it
can be used efficiently.
• A scheme for organizing related pieces of information.
06-06-2018 CS8351 - DATA STRUCTURES 3
Classification of Data Structure
06-06-2018 CS8351 - DATA STRUCTURES 4
• Primitive Data Structure:
• A Primitive Data Structure used to represent the standard data types of any one of the computer
languages (integer, Character, float etc.).
• Non - Primitive Data Structure :
• A Primitive Data Structure can be constructed with the help of any one of the primitive data
structure.
• It can be structure and it is having a specific functionality.
• It can be designed by user.
• It can be classified as Linear and Non-Linear Data Structure.
06-06-2018 CS8351 - DATA STRUCTURES 5
Linear Data Structures:
A linear data structure traverses the data elements sequentially, in which only one data element
can directly be reached.
Ex: Arrays, Linked Lists ,Stacks, Queues
Non-Linear Data Structures:
Every data item is attached to several other data items in a way that is specific for reflecting
relationships. The data items are not arranged in a sequential structure.
Ex: Trees, Graphs ,Heaps
Operations on Data Structures
• The basic operations that are performed on data structures are as follows:
• Traversal: Traversal of a data structure means processing all the data elements
present in it exactly once.
• Insertion: Insertion means addition of a new data element in a data structure.
• Deletion: Deletion means removal of a data element from a data structure if it is
found.
• Searching: Searching involves searching for the specified data element in a data
structure.
• Sorting: Arranging data elements of a data structure in a specified order is called
sorting.
• Merging: Combining elements of two similar data structures to form a new data
structure of the same type, is called merging.
06-06-2018 CS8351 - DATA STRUCTURES 6
Abstract Data Types (ADTs)
• An Abstract Data type refers to set of data values and associated operations
that are specified accurately, independent of any particular implementation.
(Or)
• ADT is a user defined data type which encapsulates a range of data values
and their functions.
(Or)
• An Abstract Data Type is a mathematical model of a data structure. It
describes a container which holds a finite number of objects where the
objects may be associated through a given binary relationship.
Ex: List, Stack ,Queue
06-06-2018 CS8351 - DATA STRUCTURES 7
The LIST ADT
• A list or sequence is an abstract data type that represents a countable number of
ordered values, where the same value may occur more than once.
A sequence of zero or more elements
A1, A2, A3, … AN
N: length of the list
A1: first element
AN: last element
Ai: position i
If N=0, then empty list
Linearly ordered
Ai precedes Ai+1
Ai follows Ai-1
06-06-2018 CS8351 - DATA STRUCTURES 8
Lists are a basic example of containers, as they contain other values. If the same
value occurs multiple times, each occurrence is considered a distinct item.
Operations
• A list contains elements of same type arranged in sequential order and
following operations can be performed on the list.
get() – Return an element from the list at any given position.
insert() – Insert an element at any position of the list.
remove() – Remove the first occurrence of any element from a non-empty
list.
removeAt() – Remove the element at a specified location from a non-
empty list.
replace() – Replace an element at any position by another element.
size() – Return the number of elements in the list.
isEmpty() – Return true if the list is empty, otherwise return false.
isFull() – Return true if the list is full, otherwise return false.
06-06-2018 CS8351 - DATA STRUCTURES 9
Implementation of LIST ADT
• Each operation associated with the ADT is implemented by one or more
subroutines
• Two standard implementations for the list ADT
• Array-based
• Linked list
• An array is a random access data structure, where each element can be accessed
directly and in constant time.
• A typical illustration of random access is a book - each page of the book can be open
independently of others.
• A linked list is a sequential access data structure, where each element can be
accessed only in particular order.
• A typical illustration of sequential access is a roll of paper or tape - all prior material must be
unrolled in order to get to data you want.
06-06-2018 CS8351 - DATA STRUCTURES 10
An Array-based Implementation (LIST)
• An Array is a data structure which can store a fixed-size sequential collection
of elements of the same type.
(An array is used to store a collection of data, but it is often more useful to think of an array as a
collection of variables of the same type.)
• Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
• A specific element in an array is accessed by an index.
• All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
06-06-2018 CS8351 - DATA STRUCTURES 11
06-06-2018 CS8351 - DATA STRUCTURES 12
Operations:
Is Empty(LIST)
If (Current Size==0) "LIST is Empty"
else "LIST is not Empty"
Is Full(LIST)
If (Current Size=Max Size) "LIST is FULL"
else "LIST is not FULL“
Insert Element to End of the LIST
1. Check that weather the List is full or not
1. If List is full return error message ”List is full. Can’t Insert”.
2. If List is not full.
1. Get the position to insert the new element by Position=Current Size+1
2. Insert the element to the Position
3. Increase the Current Size by 1 i.e. Current Size=Current Size+1
06-06-2018 CS8351 - DATA STRUCTURES 13
Delete Element from End of the LIST
1. Check that weather the List is empty or not
1. If List is empty return error message ”List is Empty. Can't Delete”.
2. If List is not Empty.
1. Get the position of the element to delete by Position=Current Size
2. Delete the element from the Position
3. Decrease the Current Size by 1 i.e. Current Size=Current Size-1
Insert Element to front of the LIST
1. Check that weather the List is full or not
1. If List is full return error message ”List is full. Can't Insert”.
2. If List is not full.
1. Free the 1st Position of the list by moving all the Element to one position forward i.e New
Position=Current Position + 1.
2. Insert the element to the 1st Position
3. Increase the Current Size by 1 i.e. Current Size=Current Size+1
06-06-2018 CS8351 - DATA STRUCTURES 14
Delete Element from front of the LIST
1. Check that weather the List is empty or not
1. If List is empty return error message ”List is Empty. Can't Delete”.
2. If List is not Empty.
1. Move all the elements except one in the 1st position to one position backward i.e New Position=
Current Position -1
2. After the 1st step, element in the 1st position will be automatically deleted.
3. Decrease the Current Size by 1 i.e. Current Size=Current Size-1
Insert Element to nth Position of the LIST
1. Check that weather the List is full or not
1. If List is full return error message ”List is full. Can't Insert”.
2. If List is not full.
1. If List is Empty, Insert element at Position 1.
2. If (nth Position > Current Size)
1. Return message “nth Position Not available in List”
3. else
1. Free the nth Position of the list by moving all Elements to one position
forward except n-1,n-2,... 1 Position i.e move only from n to current
size position Elements. i.e New Position=Current Position + 1.
2. Insert the element to the nth Position
3. Increase the Current Size by 1 i.e. Current Size=Current Size+1
06-06-2018 CS8351 - DATA STRUCTURES 15
Delete Element from nth Position of the LIST
1. Check that weather the List is Empty or not
1. If List is Empty return error message ”List is Empty.”
2. If List is not Empty.
1. If (nth Position > Current Size)
1. Return message “nth Position Not available in List”
2. If (nth Position == Current Size)
1. Delete the element from nth Position
2. Decrease the Current Size by 1 i.e. Current Size=Current Size-1
3. If (nth Position < Current Size)
1. Move all the Elements to one position backward except n,n-1,n-2,... 1 Position i.e move only
from n+1 to current size position Elements. i.e New Position=Current Position - 1.
2. After the previous step, nth element will be deleted automatically.
3. Decrease the Current Size by 1 i.e. Current Size=Current Size-1
06-06-2018 CS8351 - DATA STRUCTURES 16
Search Element in the LIST
1. Check that weather the list is empty or not.
1. If List is empty, return error message “List is Empty”.
2. If List is not Empty
1. Find the Position where the last element available in the List by Last Position = Current Size
2. For( Position 1 to Last Position)
1. If(Element @ Position== Search Element)//If Element matches the search element
2. return the Position by message “Search Element available in Position”
3. Else return message “Search Element not available in the List”
Print the Elements in the LIST
1. Check that weather the list is empty or not.
1. If List is empty, return error message “List is Empty”.
2. If List is not Empty
1. Find the Position where the last element available in the List by Last Position = Current Size
2. For( Position 1 to Last Position)
3. Print the Position and Element available at the position of List.
06-06-2018 CS8351 - DATA STRUCTURES 17
Array Implementation of LIST ADT
LIST ADT
06-06-2018 CS8351 - DATA STRUCTURES 18
Linked List Implementation - LIST
• Like arrays, Linked List is a linear data structure.
• Unlike arrays, linked list elements are not stored at contiguous
location; the elements are linked using pointers.
06-06-2018 CS8351 - DATA STRUCTURES 19
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have
following limitations.
1) The size of the arrays is fixed: So we must know the upper
limit on the number of elements in advance. Also, generally, the
allocated memory is equal to the upper limit irrespective of the usage.
2) Inserting a new element in an array of elements is expensive,
because room has to be created for the new elements and to create room
existing elements have to shifted.
06-06-2018 CS8351 - DATA STRUCTURES 20
For example, in a system if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
And if we want to insert a new ID 1005, then to maintain the sorted order, we
have to move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques
are used. For example, to delete 1010 in id[], everything after 1010 has to be
moved.
• Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
• Drawbacks:
1) Random access is not allowed. We have to access elements sequentially
starting from the first node. So we cannot do binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the
list.
06-06-2018 CS8351 - DATA STRUCTURES 21
Representation in C:
A linked list is represented by a pointer to the first node of the linked
list. The first node is called head. If the linked list is empty, then value
of head is NULL.
Each node in a list consists of at least two parts:
1) data
2) pointer to the next node
In C, we can represent a node using structures. Below is an example of a
linked list node with an integer data.
06-06-2018 CS8351 - DATA STRUCTURES 22
06-06-2018 CS8351 - DATA STRUCTURES 23
data nextListStart
node
null
pointer
Singly Linked List
06-06-2018 CS8351 - DATA STRUCTURES 24
CODING
Doubly Linked List
06-06-2018 CS8351 - DATA STRUCTURES 25
CODING
Singly Circular Linked List
06-06-2018 CS8351 - DATA STRUCTURES 26
CODING
Doubly Circular Linked List
06-06-2018 CS8351 - DATA STRUCTURES 27
CODING
Comparison of array-based lists and linked lists
Array-based lists Linked lists
Pro
no wasted space for an
individual element
only need space for the objects
actually on the list
Con
size must be predetermined;
wasted space for lists with
empty slots
overhead for links (an extra
pointer added to every node)
Space complexity Ω(n) (or greater) Θ(n)
06-06-2018 CS8351 - DATA STRUCTURES 28
Applications of List
• Lists can be used to store a list of elements. However, unlike in
traditional arrays, lists can expand and shrink, and are stored
dynamically in memory.
• In computing, lists are easier to implement than sets.
• Lists also form the basis for other abstract data types including the
queue, the stack, and their variations.
06-06-2018 CS8351 - DATA STRUCTURES 29
Polynomial Manipulation
• A polynomial is a mathematical expression consisting of a sum of terms,
each term including a variable or variables raised to a power and multiplied
by a coefficient. The simplest polynomials have one variable.
• Representation of a Polynomial: A polynomial is an expression that
contains more than two terms. A term is made up of coefficient and
exponent. An example of polynomial is
• P(x) = 4x3+6x2+7x+9
06-06-2018 CS8351 - DATA STRUCTURES 30
A polynomial thus may be represented using arrays or linked lists.
• Array representation assumes that the exponents of the given expression are
arranged from 0 to the highest value (degree), which is represented by the
subscript of the array beginning with 0.
• The coefficients of the respective exponent are placed at an appropriate
index in the array.
• The array representation for the above polynomial expression is given
below:
06-06-2018 CS8351 - DATA STRUCTURES 31
• A polynomial may also be represented using a linked list. A structure may
be defined such that it contains two parts- one is the coefficient and second
is the corresponding exponent. The structure definition may be given as
shown below:
struct polynomial
{
int coefficient;
int exponent;
struct polynomial *next;
};
• Thus the above polynomial may be represented using linked list as shown
below:
06-06-2018 CS8351 - DATA STRUCTURES 32
Here are the most common operations on a polynomial:
• Add & Subtract
• Multiply
• Differentiate
• Integrate
• etc…
There are different ways of implementing the polynomial ADT:
• Array (not recommended)
• Linked List (preferred and recommended)
Array Implementation:
• p1(x) = 8x3 + 3x2 + 2x + 6
• p2(x) = 23x4 + 18x - 3
• This is why arrays aren’t good to represent polynomials:
• p3(x) = 16x21 - 3x5 + 2x + 6
06-06-2018 CS8351 - DATA STRUCTURES 33
• Advantages of using an Array:
• only good for non-sparse polynomials.
• ease of storage and retrieval.
• Disadvantages of using an Array:
• have to allocate array size ahead of time.
• huge array size required for sparse polynomials. Waste of space and runtime.
06-06-2018 CS8351 - DATA STRUCTURES 34
Linked list Implementation:
• p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3
• p2(x) = 4x6 + 10x4 + 12x + 8
Representation
struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;
06-06-2018 CS8351 - DATA STRUCTURES 35
coef exp next
• Advantages of using a Linked list:
• save space (don’t have to worry about sparse polynomials) and easy to
maintain
• don’t need to allocate list size and can declare nodes (terms) only as needed
• Disadvantages of using a Linked list :
• can’t go backwards through the list
• can’t jump to the beginning of the list from the end.
06-06-2018 CS8351 - DATA STRUCTURES 36
Addition of two Polynomials:
• For adding two polynomials using arrays is straightforward method,
since both the arrays may be added up element wise beginning from 0
to n-1, resulting in addition of two polynomials.
• Addition of two polynomials using linked list requires comparing the
exponents, and wherever the exponents are found to be same, the
coefficients are added up.
• For terms with different exponents, the complete term is simply added
to the result thereby making it a part of addition result.
06-06-2018 CS8351 - DATA STRUCTURES 37
Adding polynomials using a Linked list representation: (storing the result in p3)
To do this, we have to break the process down to cases:
Case 1: exponent of p1 > exponent of p2
• Copy node of p1 to end of p3.
• [go to next node]
Case 2: exponent of p1 < exponent of p2
• Copy node of p2 to end of p3.
• [go to next node]
Case 3: exponent of p1 = exponent of p2
• Create a new node in p3 with the same exponent and with the sum of the coefficients
of p1 and p2.
CODING
06-06-2018 CS8351 - DATA STRUCTURES 38
06-06-2018 CS8351 - DATA STRUCTURES 39
Multiplication of two Polynomials:
Multiplication of two polynomials however requires manipulation of each node such
that the exponents are added up and the coefficients are multiplied.
After each term of first polynomial is operated upon with each term of the second
polynomial, then the result has to be added up by comparing the exponents and
adding the coefficients for similar exponents and including terms as such with
dissimilar exponents in the result.
All Operations (Insertion, Deletion, Merge, Traversal)
• Insertion
• Deletion
• Merge two sorted linked lists
• Traversal
• Assume, that we have a list with some nodes. Traversal is the very basic
operation, which presents as a part in almost every operation on a singly-linked
list.
• For instance, algorithm may traverse a singly-linked list to find a value, find a
position for insertion, etc. For a singly-linked list, only forward direction
traversal is possible.
Traversal algorithm
• Beginning from the head,
• check, if the end of a list hasn't been reached yet;
• do some actions with the current node, which is specific for particular algorithm;
• current node becomes previous and next node becomes current. Go to the step 1.06-06-2018 CS8351 - DATA STRUCTURES 40
Example
• As for example, let us see an
example of summing up values in
a singly-linked list.
06-06-2018 CS8351 - DATA STRUCTURES 41

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Hashing
HashingHashing
Hashing
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
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]
 
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
 
Stack
StackStack
Stack
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Linked List
Linked ListLinked List
Linked List
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
stack presentation
stack presentationstack presentation
stack presentation
 
Queues
QueuesQueues
Queues
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 

Similar a Data Structures (CS8391)

1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptxBlueSwede
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyMalikireddy Bramhananda Reddy
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
Data structures - unit 1
Data structures - unit 1Data structures - unit 1
Data structures - unit 1SaranyaP45
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxsarala9
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptxSaralaT3
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxUsriDevi1
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introductionSugandh Wafai
 
unit 1_Linked list.pptx
unit 1_Linked list.pptxunit 1_Linked list.pptx
unit 1_Linked list.pptxssuser7922b8
 
Data Structures and algoithms Unit - 1.pptx
Data Structures and algoithms Unit - 1.pptxData Structures and algoithms Unit - 1.pptx
Data Structures and algoithms Unit - 1.pptxmexiuro901
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTSwapnil Mishra
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructuresNguync91368
 

Similar a Data Structures (CS8391) (20)

1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Data structures - unit 1
Data structures - unit 1Data structures - unit 1
Data structures - unit 1
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
UNITIII LDS.pdf
UNITIII LDS.pdfUNITIII LDS.pdf
UNITIII LDS.pdf
 
DS Module 1.pptx
DS Module 1.pptxDS Module 1.pptx
DS Module 1.pptx
 
Data Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptxData Structures & Algorithms Unit 1.pptx
Data Structures & Algorithms Unit 1.pptx
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introduction
 
unit 1_Linked list.pptx
unit 1_Linked list.pptxunit 1_Linked list.pptx
unit 1_Linked list.pptx
 
DSA - Copy.pptx
DSA - Copy.pptxDSA - Copy.pptx
DSA - Copy.pptx
 
Data Structure
Data StructureData Structure
Data Structure
 
General Data structures
General Data structuresGeneral Data structures
General Data structures
 
Data Structures and algoithms Unit - 1.pptx
Data Structures and algoithms Unit - 1.pptxData Structures and algoithms Unit - 1.pptx
Data Structures and algoithms Unit - 1.pptx
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 

Último

دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratoryدليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide LaboratoryBahzad5
 
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...Sean Meyn
 
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
Engineering Mechanics  Chapter 5  Equilibrium of a Rigid BodyEngineering Mechanics  Chapter 5  Equilibrium of a Rigid Body
Engineering Mechanics Chapter 5 Equilibrium of a Rigid BodyAhmadHajasad2
 
Landsman converter for power factor improvement
Landsman converter for power factor improvementLandsman converter for power factor improvement
Landsman converter for power factor improvementVijayMuni2
 
Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Bahzad5
 
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS Bahzad5
 
Test of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptxTest of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptxHome
 
Basic Principle of Electrochemical Sensor
Basic Principle of  Electrochemical SensorBasic Principle of  Electrochemical Sensor
Basic Principle of Electrochemical SensorTanvir Moin
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabusViolet Violet
 
Design of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptxDesign of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptxYogeshKumarKJMIT
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxrealme6igamerr
 
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfsdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfJulia Kaye
 
How to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdfHow to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdfRedhwan Qasem Shaddad
 
Renewable Energy & Entrepreneurship Workshop_21Feb2024.pdf
Renewable Energy & Entrepreneurship Workshop_21Feb2024.pdfRenewable Energy & Entrepreneurship Workshop_21Feb2024.pdf
Renewable Energy & Entrepreneurship Workshop_21Feb2024.pdfodunowoeminence2019
 
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Apollo Techno Industries Pvt Ltd
 
me3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Ame3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Akarthi keyan
 

Último (20)

دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratoryدليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
دليل تجارب الاسفلت المختبرية - Asphalt Experiments Guide Laboratory
 
Présentation IIRB 2024 Marine Cordonnier.pdf
Présentation IIRB 2024 Marine Cordonnier.pdfPrésentation IIRB 2024 Marine Cordonnier.pdf
Présentation IIRB 2024 Marine Cordonnier.pdf
 
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
 
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
Engineering Mechanics  Chapter 5  Equilibrium of a Rigid BodyEngineering Mechanics  Chapter 5  Equilibrium of a Rigid Body
Engineering Mechanics Chapter 5 Equilibrium of a Rigid Body
 
計劃趕得上變化
計劃趕得上變化計劃趕得上變化
計劃趕得上變化
 
Landsman converter for power factor improvement
Landsman converter for power factor improvementLandsman converter for power factor improvement
Landsman converter for power factor improvement
 
Litature Review: Research Paper work for Engineering
Litature Review: Research Paper work for EngineeringLitature Review: Research Paper work for Engineering
Litature Review: Research Paper work for Engineering
 
Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)Lecture 1: Basics of trigonometry (surveying)
Lecture 1: Basics of trigonometry (surveying)
 
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
 
Lecture 2 .pptx
Lecture 2                            .pptxLecture 2                            .pptx
Lecture 2 .pptx
 
Test of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptxTest of Significance of Large Samples for Mean = µ.pptx
Test of Significance of Large Samples for Mean = µ.pptx
 
Basic Principle of Electrochemical Sensor
Basic Principle of  Electrochemical SensorBasic Principle of  Electrochemical Sensor
Basic Principle of Electrochemical Sensor
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabus
 
Design of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptxDesign of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptx
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
 
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdfsdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
sdfsadopkjpiosufoiasdoifjasldkjfl a asldkjflaskdjflkjsdsdf
 
How to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdfHow to Write a Good Scientific Paper.pdf
How to Write a Good Scientific Paper.pdf
 
Renewable Energy & Entrepreneurship Workshop_21Feb2024.pdf
Renewable Energy & Entrepreneurship Workshop_21Feb2024.pdfRenewable Energy & Entrepreneurship Workshop_21Feb2024.pdf
Renewable Energy & Entrepreneurship Workshop_21Feb2024.pdf
 
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...Technology Features of Apollo HDD Machine, Its Technical Specification with C...
Technology Features of Apollo HDD Machine, Its Technical Specification with C...
 
me3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Ame3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part A
 

Data Structures (CS8391)

  • 1. 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). Elavarasi.K AP/CSE S.A Engineering College 06-06-2018 CS8351 - DATA STRUCTURES 1
  • 2. Introduction to DS • Data structures are generally based on the ability of a computer to fetch and store data at any place in its memory, specified by a pointer • Thus, the array and record data structures are based on computing the addresses of data items with arithmetic operations • What is Program? • A Set of Instructions Data Structures + Algorithms Data Structure = A Container stores Data Algorithm = Logic + Control CS8351 - DATA STRUCTURES06-06-2018 2
  • 3. • Data Structure • Data: are simply a value are set of values of different type which is called data types like string, integer, char etc. • Structure: Way of organizing information, so that it is easier to use In simple words we can define data structures as • Its a way organizing data in such a way so that data can be easier to use. • Data Structure .. • A data structure is a particular way of organizing data in a computer so that it can be used efficiently. • A scheme for organizing related pieces of information. 06-06-2018 CS8351 - DATA STRUCTURES 3
  • 4. Classification of Data Structure 06-06-2018 CS8351 - DATA STRUCTURES 4
  • 5. • Primitive Data Structure: • A Primitive Data Structure used to represent the standard data types of any one of the computer languages (integer, Character, float etc.). • Non - Primitive Data Structure : • A Primitive Data Structure can be constructed with the help of any one of the primitive data structure. • It can be structure and it is having a specific functionality. • It can be designed by user. • It can be classified as Linear and Non-Linear Data Structure. 06-06-2018 CS8351 - DATA STRUCTURES 5 Linear Data Structures: A linear data structure traverses the data elements sequentially, in which only one data element can directly be reached. Ex: Arrays, Linked Lists ,Stacks, Queues Non-Linear Data Structures: Every data item is attached to several other data items in a way that is specific for reflecting relationships. The data items are not arranged in a sequential structure. Ex: Trees, Graphs ,Heaps
  • 6. Operations on Data Structures • The basic operations that are performed on data structures are as follows: • Traversal: Traversal of a data structure means processing all the data elements present in it exactly once. • Insertion: Insertion means addition of a new data element in a data structure. • Deletion: Deletion means removal of a data element from a data structure if it is found. • Searching: Searching involves searching for the specified data element in a data structure. • Sorting: Arranging data elements of a data structure in a specified order is called sorting. • Merging: Combining elements of two similar data structures to form a new data structure of the same type, is called merging. 06-06-2018 CS8351 - DATA STRUCTURES 6
  • 7. Abstract Data Types (ADTs) • An Abstract Data type refers to set of data values and associated operations that are specified accurately, independent of any particular implementation. (Or) • ADT is a user defined data type which encapsulates a range of data values and their functions. (Or) • An Abstract Data Type is a mathematical model of a data structure. It describes a container which holds a finite number of objects where the objects may be associated through a given binary relationship. Ex: List, Stack ,Queue 06-06-2018 CS8351 - DATA STRUCTURES 7
  • 8. The LIST ADT • A list or sequence is an abstract data type that represents a countable number of ordered values, where the same value may occur more than once. A sequence of zero or more elements A1, A2, A3, … AN N: length of the list A1: first element AN: last element Ai: position i If N=0, then empty list Linearly ordered Ai precedes Ai+1 Ai follows Ai-1 06-06-2018 CS8351 - DATA STRUCTURES 8 Lists are a basic example of containers, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.
  • 9. Operations • A list contains elements of same type arranged in sequential order and following operations can be performed on the list. get() – Return an element from the list at any given position. insert() – Insert an element at any position of the list. remove() – Remove the first occurrence of any element from a non-empty list. removeAt() – Remove the element at a specified location from a non- empty list. replace() – Replace an element at any position by another element. size() – Return the number of elements in the list. isEmpty() – Return true if the list is empty, otherwise return false. isFull() – Return true if the list is full, otherwise return false. 06-06-2018 CS8351 - DATA STRUCTURES 9
  • 10. Implementation of LIST ADT • Each operation associated with the ADT is implemented by one or more subroutines • Two standard implementations for the list ADT • Array-based • Linked list • An array is a random access data structure, where each element can be accessed directly and in constant time. • A typical illustration of random access is a book - each page of the book can be open independently of others. • A linked list is a sequential access data structure, where each element can be accessed only in particular order. • A typical illustration of sequential access is a roll of paper or tape - all prior material must be unrolled in order to get to data you want. 06-06-2018 CS8351 - DATA STRUCTURES 10
  • 11. An Array-based Implementation (LIST) • An Array is a data structure which can store a fixed-size sequential collection of elements of the same type. (An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.) • Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. • A specific element in an array is accessed by an index. • All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. 06-06-2018 CS8351 - DATA STRUCTURES 11
  • 12. 06-06-2018 CS8351 - DATA STRUCTURES 12
  • 13. Operations: Is Empty(LIST) If (Current Size==0) "LIST is Empty" else "LIST is not Empty" Is Full(LIST) If (Current Size=Max Size) "LIST is FULL" else "LIST is not FULL“ Insert Element to End of the LIST 1. Check that weather the List is full or not 1. If List is full return error message ”List is full. Can’t Insert”. 2. If List is not full. 1. Get the position to insert the new element by Position=Current Size+1 2. Insert the element to the Position 3. Increase the Current Size by 1 i.e. Current Size=Current Size+1 06-06-2018 CS8351 - DATA STRUCTURES 13
  • 14. Delete Element from End of the LIST 1. Check that weather the List is empty or not 1. If List is empty return error message ”List is Empty. Can't Delete”. 2. If List is not Empty. 1. Get the position of the element to delete by Position=Current Size 2. Delete the element from the Position 3. Decrease the Current Size by 1 i.e. Current Size=Current Size-1 Insert Element to front of the LIST 1. Check that weather the List is full or not 1. If List is full return error message ”List is full. Can't Insert”. 2. If List is not full. 1. Free the 1st Position of the list by moving all the Element to one position forward i.e New Position=Current Position + 1. 2. Insert the element to the 1st Position 3. Increase the Current Size by 1 i.e. Current Size=Current Size+1 06-06-2018 CS8351 - DATA STRUCTURES 14
  • 15. Delete Element from front of the LIST 1. Check that weather the List is empty or not 1. If List is empty return error message ”List is Empty. Can't Delete”. 2. If List is not Empty. 1. Move all the elements except one in the 1st position to one position backward i.e New Position= Current Position -1 2. After the 1st step, element in the 1st position will be automatically deleted. 3. Decrease the Current Size by 1 i.e. Current Size=Current Size-1 Insert Element to nth Position of the LIST 1. Check that weather the List is full or not 1. If List is full return error message ”List is full. Can't Insert”. 2. If List is not full. 1. If List is Empty, Insert element at Position 1. 2. If (nth Position > Current Size) 1. Return message “nth Position Not available in List” 3. else 1. Free the nth Position of the list by moving all Elements to one position forward except n-1,n-2,... 1 Position i.e move only from n to current size position Elements. i.e New Position=Current Position + 1. 2. Insert the element to the nth Position 3. Increase the Current Size by 1 i.e. Current Size=Current Size+1 06-06-2018 CS8351 - DATA STRUCTURES 15
  • 16. Delete Element from nth Position of the LIST 1. Check that weather the List is Empty or not 1. If List is Empty return error message ”List is Empty.” 2. If List is not Empty. 1. If (nth Position > Current Size) 1. Return message “nth Position Not available in List” 2. If (nth Position == Current Size) 1. Delete the element from nth Position 2. Decrease the Current Size by 1 i.e. Current Size=Current Size-1 3. If (nth Position < Current Size) 1. Move all the Elements to one position backward except n,n-1,n-2,... 1 Position i.e move only from n+1 to current size position Elements. i.e New Position=Current Position - 1. 2. After the previous step, nth element will be deleted automatically. 3. Decrease the Current Size by 1 i.e. Current Size=Current Size-1 06-06-2018 CS8351 - DATA STRUCTURES 16
  • 17. Search Element in the LIST 1. Check that weather the list is empty or not. 1. If List is empty, return error message “List is Empty”. 2. If List is not Empty 1. Find the Position where the last element available in the List by Last Position = Current Size 2. For( Position 1 to Last Position) 1. If(Element @ Position== Search Element)//If Element matches the search element 2. return the Position by message “Search Element available in Position” 3. Else return message “Search Element not available in the List” Print the Elements in the LIST 1. Check that weather the list is empty or not. 1. If List is empty, return error message “List is Empty”. 2. If List is not Empty 1. Find the Position where the last element available in the List by Last Position = Current Size 2. For( Position 1 to Last Position) 3. Print the Position and Element available at the position of List. 06-06-2018 CS8351 - DATA STRUCTURES 17
  • 18. Array Implementation of LIST ADT LIST ADT 06-06-2018 CS8351 - DATA STRUCTURES 18
  • 19. Linked List Implementation - LIST • Like arrays, Linked List is a linear data structure. • Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers. 06-06-2018 CS8351 - DATA STRUCTURES 19
  • 20. Why Linked List? Arrays can be used to store linear data of similar types, but arrays have following limitations. 1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage. 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. 06-06-2018 CS8351 - DATA STRUCTURES 20
  • 21. For example, in a system if we maintain a sorted list of IDs in an array id[]. id[] = [1000, 1010, 1050, 2000, 2040]. And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved. • Advantages over arrays 1) Dynamic size 2) Ease of insertion/deletion • Drawbacks: 1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists. 2) Extra memory space for a pointer is required with each element of the list. 06-06-2018 CS8351 - DATA STRUCTURES 21
  • 22. Representation in C: A linked list is represented by a pointer to the first node of the linked list. The first node is called head. If the linked list is empty, then value of head is NULL. Each node in a list consists of at least two parts: 1) data 2) pointer to the next node In C, we can represent a node using structures. Below is an example of a linked list node with an integer data. 06-06-2018 CS8351 - DATA STRUCTURES 22
  • 23. 06-06-2018 CS8351 - DATA STRUCTURES 23 data nextListStart node null pointer
  • 24. Singly Linked List 06-06-2018 CS8351 - DATA STRUCTURES 24 CODING
  • 25. Doubly Linked List 06-06-2018 CS8351 - DATA STRUCTURES 25 CODING
  • 26. Singly Circular Linked List 06-06-2018 CS8351 - DATA STRUCTURES 26 CODING
  • 27. Doubly Circular Linked List 06-06-2018 CS8351 - DATA STRUCTURES 27 CODING
  • 28. Comparison of array-based lists and linked lists Array-based lists Linked lists Pro no wasted space for an individual element only need space for the objects actually on the list Con size must be predetermined; wasted space for lists with empty slots overhead for links (an extra pointer added to every node) Space complexity Ω(n) (or greater) Θ(n) 06-06-2018 CS8351 - DATA STRUCTURES 28
  • 29. Applications of List • Lists can be used to store a list of elements. However, unlike in traditional arrays, lists can expand and shrink, and are stored dynamically in memory. • In computing, lists are easier to implement than sets. • Lists also form the basis for other abstract data types including the queue, the stack, and their variations. 06-06-2018 CS8351 - DATA STRUCTURES 29
  • 30. Polynomial Manipulation • A polynomial is a mathematical expression consisting of a sum of terms, each term including a variable or variables raised to a power and multiplied by a coefficient. The simplest polynomials have one variable. • Representation of a Polynomial: A polynomial is an expression that contains more than two terms. A term is made up of coefficient and exponent. An example of polynomial is • P(x) = 4x3+6x2+7x+9 06-06-2018 CS8351 - DATA STRUCTURES 30
  • 31. A polynomial thus may be represented using arrays or linked lists. • Array representation assumes that the exponents of the given expression are arranged from 0 to the highest value (degree), which is represented by the subscript of the array beginning with 0. • The coefficients of the respective exponent are placed at an appropriate index in the array. • The array representation for the above polynomial expression is given below: 06-06-2018 CS8351 - DATA STRUCTURES 31
  • 32. • A polynomial may also be represented using a linked list. A structure may be defined such that it contains two parts- one is the coefficient and second is the corresponding exponent. The structure definition may be given as shown below: struct polynomial { int coefficient; int exponent; struct polynomial *next; }; • Thus the above polynomial may be represented using linked list as shown below: 06-06-2018 CS8351 - DATA STRUCTURES 32
  • 33. Here are the most common operations on a polynomial: • Add & Subtract • Multiply • Differentiate • Integrate • etc… There are different ways of implementing the polynomial ADT: • Array (not recommended) • Linked List (preferred and recommended) Array Implementation: • p1(x) = 8x3 + 3x2 + 2x + 6 • p2(x) = 23x4 + 18x - 3 • This is why arrays aren’t good to represent polynomials: • p3(x) = 16x21 - 3x5 + 2x + 6 06-06-2018 CS8351 - DATA STRUCTURES 33
  • 34. • Advantages of using an Array: • only good for non-sparse polynomials. • ease of storage and retrieval. • Disadvantages of using an Array: • have to allocate array size ahead of time. • huge array size required for sparse polynomials. Waste of space and runtime. 06-06-2018 CS8351 - DATA STRUCTURES 34
  • 35. Linked list Implementation: • p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3 • p2(x) = 4x6 + 10x4 + 12x + 8 Representation struct polynode { int coef; int exp; struct polynode * next; }; typedef struct polynode *polyptr; 06-06-2018 CS8351 - DATA STRUCTURES 35 coef exp next
  • 36. • Advantages of using a Linked list: • save space (don’t have to worry about sparse polynomials) and easy to maintain • don’t need to allocate list size and can declare nodes (terms) only as needed • Disadvantages of using a Linked list : • can’t go backwards through the list • can’t jump to the beginning of the list from the end. 06-06-2018 CS8351 - DATA STRUCTURES 36
  • 37. Addition of two Polynomials: • For adding two polynomials using arrays is straightforward method, since both the arrays may be added up element wise beginning from 0 to n-1, resulting in addition of two polynomials. • Addition of two polynomials using linked list requires comparing the exponents, and wherever the exponents are found to be same, the coefficients are added up. • For terms with different exponents, the complete term is simply added to the result thereby making it a part of addition result. 06-06-2018 CS8351 - DATA STRUCTURES 37
  • 38. Adding polynomials using a Linked list representation: (storing the result in p3) To do this, we have to break the process down to cases: Case 1: exponent of p1 > exponent of p2 • Copy node of p1 to end of p3. • [go to next node] Case 2: exponent of p1 < exponent of p2 • Copy node of p2 to end of p3. • [go to next node] Case 3: exponent of p1 = exponent of p2 • Create a new node in p3 with the same exponent and with the sum of the coefficients of p1 and p2. CODING 06-06-2018 CS8351 - DATA STRUCTURES 38
  • 39. 06-06-2018 CS8351 - DATA STRUCTURES 39 Multiplication of two Polynomials: Multiplication of two polynomials however requires manipulation of each node such that the exponents are added up and the coefficients are multiplied. After each term of first polynomial is operated upon with each term of the second polynomial, then the result has to be added up by comparing the exponents and adding the coefficients for similar exponents and including terms as such with dissimilar exponents in the result.
  • 40. All Operations (Insertion, Deletion, Merge, Traversal) • Insertion • Deletion • Merge two sorted linked lists • Traversal • Assume, that we have a list with some nodes. Traversal is the very basic operation, which presents as a part in almost every operation on a singly-linked list. • For instance, algorithm may traverse a singly-linked list to find a value, find a position for insertion, etc. For a singly-linked list, only forward direction traversal is possible. Traversal algorithm • Beginning from the head, • check, if the end of a list hasn't been reached yet; • do some actions with the current node, which is specific for particular algorithm; • current node becomes previous and next node becomes current. Go to the step 1.06-06-2018 CS8351 - DATA STRUCTURES 40
  • 41. Example • As for example, let us see an example of summing up values in a singly-linked list. 06-06-2018 CS8351 - DATA STRUCTURES 41