Data structure:
A data structure is a logical representation of data and
operation that can be performed on the data.
Types:
1. Linear data structure.
2. Non linear data structure.
Linear data structure is an order of
data elements.
They are arrays, stacks, queues, and linked lists.
Why we need Linked List?
What are the problems with Arrays?
Size is fixed
Array Items are stored contiguously
Insertions and deletion at particular position is
complex
Why Linked list ?
 Size is not fixed
 Data can be stored at any place
 Insertions and deletions are simple and faster
What is Linked list?
Linked list is a linear data structure where
elements are not stored sequentially inside
the computer memory but they are linked with
each other by the help of address.
Linked list:
Linked list is a collection of elements called nodes.
Each node contains two parts. they are
data part and link part.
Dat
a
Lin
k
Node:
Linked List Types
 Singly Linked list
 Doubly linked lists
 Circular singly linked list
 Circular doubly linked lists
Singly Linked List
 Each node has only one link part
 Each link part contains the address of the next
node in the list
 Link part of the last node contains NULL value
which signifies the end of the node
10
1000
1000
2000
2000
15 NULL
20
4000
Singly Linked List
Each node points the next node . It is a
one way list.
First
Double Linked List
Contains the address of previous node
and next node
NULL
2000 3000
1000
10 15 20
2000 1000 2000 NULL
3000
First
10 15 20
Circular Singly Linked List
Last node contains the address of the first node
First
Circular Doubly Linked list
Contains the address of first node and
last node
3000
2000 3000
1000
10 15 20
2000 1000 2000 1000
3000
Operations on Linked List
Creation of the linked list
Traversal
Searching a specific node in the linked list
Insertion
Deletion of a node in the linked list
struct node
{
int data;
node*link;
};
1. Traversal of Singly Linked List
2. Searching in Singly Linked List
 There are three ways to insert a node into the
list.
Insertion at the beginning
Insertion at the end
Insertion after a particular node
3. Insertion
Insertion at the Beginning of Singly
Linked List:
Deleting a node in SLL
Here also we have three cases:-
 Deleting the first node
Deleting the last node
Deleting the intermediate node
Deleting the first node
Here we apply 2 steps:-
 Making the start pointer point towards the 2nd
node
 Deleting the first node using delete keyword
three
two
one
start
Deleting the last node
Here we apply 2 steps:-
 Making the second last node’s next pointer
point to NULL
 Deleting the last node via delete keyword
node3
node2
node1
start
Deleting a particular node
Here we make the next pointer of the node
previous to the node being deleted ,point to the
successor node of the node to be deleted and then
delete the node using delete keyword
node1 node2 node3
To be deleted
Operation ID-Array Complexity Singly-linked list Complexity
Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference
Insert at middle O(n) O(n)
Delete at beginning O(n) O(1)
Delete at end O(1) O(n)
Delete at middle O(n):
O(1) access followed by O(n)
shift
O(n):
O(n) search, followed by O(1) delete
Search O(n) linear search
O(log n) Binary search
O(n)
Indexing: What is
the element at a
given position k?
O(1) O(n)
COMPLEXITY OF VARIOUS
OPERATIONS IN ARRAYS AND SLL
Doubly Linked List
1. Doubly linked list is a linked data structure that consists of a
set of sequentially linked records called nodes.
2 . Each node contains three fields ::
-: one is data part which contain data only.
-:two other field is links part that are point
or references to the previous or to the next
node in the sequence of nodes.
3. The beginning and ending nodes' previous and next
links, respectively, point to some kind of terminator,
typically a sentinel node or null to facilitate traversal
of the list.
NODE
A B C
A doubly linked list contain three fields: an integer
value, the link to the next node, and the link to the
previous node.
previous data next
NULL 11 786
786
200 400
200 656 400 786 777 NULL
DLL’s compared to SLL’s
 Advantages:
 Can be traversed in
either direction (may
be essential for some
programs)
 Some operations, such
as deletion and
inserting before a
node, become easier
 Disadvantages:
 Requires more space
 List manipulations are
slower (because more
links must be changed)
 Greater chance of
having bugs (because
more links must be
manipulated)
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};
.Data .next
previou
s.inf
Inserting at beginning
Inserting at the end
Making next and previous pointer of the node to
be inserted point accordingly
Adjusting the next and previous pointers of the nodes b/w
which the new node accordingly
Inserting after a node
Deleting a node
• Node deletion from a DLL involves changing two
links
• In this example,we will delete node b
myDLL
a b c
• We don’t have to do anything about the links in
node b
• Garbage collection will take care of deleted nodes
• Deletion of the first node or the last node is a
special case
APPLICATIONS OF LINKED LIST
1. Applications that have an MRU list (a linked list of
file names)
2. The cache in your browser that allows you to hit the
BACK button (a linked list of URLs)
3. Undo functionality in Photoshop or Word (a linked
list of state)
4. A stack, hash table, and binary tree can be
implemented using a doubly linked list.
• Polynomial Representation
• Linked list Implementation:
• p1(x) = 23x9
+ 18x7
+ 41x6
+ 163x4
+ 3
• p2(x) = 4x6
+ 10x4
+ 12x + 8
23 9 18 7 41 6 18 7 3 0
4 6 10 4 12 1 8 0
P1
P2
NODE (contains coefficient & exponent)
TAIL (contains pointer)
Polynomials
A x a x a x a x
m
e
m
e e
m m
( ) ...
   
 
 
1 2 0
1 2 0
Representation
struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;
coef exp next
• 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.
Example
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
b x x x
  
8 3 10
14 10 6
null
null
a x x
  
3 2 1
14 8
Adding Polynomials
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14
d
a->expon == b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 a->expon < b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14
a->expon > b->expon
-3 10
d
2 8
THANK YOU

linked list_MODULE 3.pptx ppt on the linked list

  • 1.
    Data structure: A datastructure is a logical representation of data and operation that can be performed on the data. Types: 1. Linear data structure. 2. Non linear data structure. Linear data structure is an order of data elements. They are arrays, stacks, queues, and linked lists.
  • 2.
    Why we needLinked List? What are the problems with Arrays? Size is fixed Array Items are stored contiguously Insertions and deletion at particular position is complex Why Linked list ?  Size is not fixed  Data can be stored at any place  Insertions and deletions are simple and faster
  • 3.
    What is Linkedlist? Linked list is a linear data structure where elements are not stored sequentially inside the computer memory but they are linked with each other by the help of address.
  • 4.
    Linked list: Linked listis a collection of elements called nodes. Each node contains two parts. they are data part and link part. Dat a Lin k Node:
  • 6.
    Linked List Types Singly Linked list  Doubly linked lists  Circular singly linked list  Circular doubly linked lists
  • 7.
    Singly Linked List Each node has only one link part  Each link part contains the address of the next node in the list  Link part of the last node contains NULL value which signifies the end of the node
  • 8.
    10 1000 1000 2000 2000 15 NULL 20 4000 Singly LinkedList Each node points the next node . It is a one way list. First
  • 9.
  • 10.
    Contains the addressof previous node and next node NULL 2000 3000 1000 10 15 20 2000 1000 2000 NULL 3000 First
  • 11.
    10 15 20 CircularSingly Linked List Last node contains the address of the first node First
  • 12.
    Circular Doubly Linkedlist Contains the address of first node and last node 3000 2000 3000 1000 10 15 20 2000 1000 2000 1000 3000
  • 13.
    Operations on LinkedList Creation of the linked list Traversal Searching a specific node in the linked list Insertion Deletion of a node in the linked list
  • 14.
  • 15.
    1. Traversal ofSingly Linked List
  • 16.
    2. Searching inSingly Linked List
  • 17.
     There arethree ways to insert a node into the list. Insertion at the beginning Insertion at the end Insertion after a particular node 3. Insertion
  • 18.
    Insertion at theBeginning of Singly Linked List:
  • 21.
    Deleting a nodein SLL Here also we have three cases:-  Deleting the first node Deleting the last node Deleting the intermediate node
  • 22.
    Deleting the firstnode Here we apply 2 steps:-  Making the start pointer point towards the 2nd node  Deleting the first node using delete keyword three two one start
  • 23.
    Deleting the lastnode Here we apply 2 steps:-  Making the second last node’s next pointer point to NULL  Deleting the last node via delete keyword node3 node2 node1 start
  • 24.
    Deleting a particularnode Here we make the next pointer of the node previous to the node being deleted ,point to the successor node of the node to be deleted and then delete the node using delete keyword node1 node2 node3 To be deleted
  • 25.
    Operation ID-Array ComplexitySingly-linked list Complexity Insert at beginning O(n) O(1) Insert at end O(1) O(1) if the list has tail reference O(n) if the list has no tail reference Insert at middle O(n) O(n) Delete at beginning O(n) O(1) Delete at end O(1) O(n) Delete at middle O(n): O(1) access followed by O(n) shift O(n): O(n) search, followed by O(1) delete Search O(n) linear search O(log n) Binary search O(n) Indexing: What is the element at a given position k? O(1) O(n) COMPLEXITY OF VARIOUS OPERATIONS IN ARRAYS AND SLL
  • 26.
    Doubly Linked List 1.Doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. 2 . Each node contains three fields :: -: one is data part which contain data only. -:two other field is links part that are point or references to the previous or to the next node in the sequence of nodes. 3. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null to facilitate traversal of the list.
  • 27.
    NODE A B C Adoubly linked list contain three fields: an integer value, the link to the next node, and the link to the previous node. previous data next NULL 11 786 786 200 400 200 656 400 786 777 NULL
  • 28.
    DLL’s compared toSLL’s  Advantages:  Can be traversed in either direction (may be essential for some programs)  Some operations, such as deletion and inserting before a node, become easier  Disadvantages:  Requires more space  List manipulations are slower (because more links must be changed)  Greater chance of having bugs (because more links must be manipulated)
  • 29.
    Structure of DLL structnode { int data; node*next; node*previous; //holds the address of previous node }; .Data .next previou s.inf
  • 30.
  • 31.
  • 32.
    Making next andprevious pointer of the node to be inserted point accordingly Adjusting the next and previous pointers of the nodes b/w which the new node accordingly Inserting after a node
  • 33.
    Deleting a node •Node deletion from a DLL involves changing two links • In this example,we will delete node b myDLL a b c • We don’t have to do anything about the links in node b • Garbage collection will take care of deleted nodes • Deletion of the first node or the last node is a special case
  • 34.
    APPLICATIONS OF LINKEDLIST 1. Applications that have an MRU list (a linked list of file names) 2. The cache in your browser that allows you to hit the BACK button (a linked list of URLs) 3. Undo functionality in Photoshop or Word (a linked list of state) 4. A stack, hash table, and binary tree can be implemented using a doubly linked list.
  • 35.
    • Polynomial Representation •Linked list Implementation: • p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3 • p2(x) = 4x6 + 10x4 + 12x + 8 23 9 18 7 41 6 18 7 3 0 4 6 10 4 12 1 8 0 P1 P2 NODE (contains coefficient & exponent) TAIL (contains pointer)
  • 36.
    Polynomials A x ax a x a x m e m e e m m ( ) ...         1 2 0 1 2 0 Representation struct polynode { int coef; int exp; struct polynode * next; }; typedef struct polynode *polyptr; coef exp next
  • 37.
    • Adding polynomialsusing 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]
  • 38.
    • 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.
  • 39.
    Example 3 14 28 1 0 a 8 14 -3 10 10 6 b b x x x    8 3 10 14 10 6 null null a x x    3 2 1 14 8
  • 40.
    Adding Polynomials 3 142 8 1 0 a 8 14 -3 10 10 6 b 11 14 d a->expon == b->expon 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 a->expon < b->expon
  • 41.
    3 14 28 1 0 a 8 14 -3 10 10 6 b 11 14 a->expon > b->expon -3 10 d 2 8
  • 42.