SlideShare a Scribd company logo
1 of 56
What are Linked Lists
 A linked list is a linear data
structure.
 Nodes make up linked lists.
 Nodes are structures made up
of data and a pointer to another
node.
 Usually the pointer is called
next.
Arrays Vs Linked Lists
Arrays Linked list
Fixed size: Resizing is expensive Dynamic size
Insertions and Deletions are inefficient:
Elements are usually shifted
Insertions and Deletions are efficient: No
shifting
Random access i.e., efficient indexing No random access
 Not suitable for operations requiring
accessing elements by index such as sorting
No memory waste if the array is full or almost
full; otherwise may result in much memory
waste.
Since memory is allocated dynamically(acc. to
our need) there is no waste of memory.
Sequential access is faster [Reason: Elements in
contiguous memory locations]
Sequential access is slow [Reason: Elements not
in contiguous memory locations]
Types of lists
 There are two basic types of linked list
Singly Linked list
Doubly linked list
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
Schematic representation
 Here is a singly-linked list (SLL):
a b c d
myList
• Each node contains a value(data) and a pointer
to the next node in the list
• myList is the header pointer which points at
the first node in the list
Basic Operations on a list
• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
Creating a node
struct node{
int data; // A simple node of a linked list
node*next;
}*start; //start points at the first node
start=NULL ; initialised to NULL at beginning
node* create( int num) //say num=1 is passed from main
{
node*ptr;
ptr= new node; //memory allocated dynamically
if(ptr==NULL)
‘OVERFLOW’ // no memory available
exit(1);
else
{
ptr->data=num;
ptr->next=NULL;
return ptr;
}
}
To be called from main() as:-
void main()
{
node* ptr;
int data;
cin>>data;
ptr=create(data);
}
Inserting the node in a SLL
There are 3 cases here:-
Insertion at the beginning
Insertion at the end
Insertion after a particular node
Insertion at the beginning
There are two steps to be followed:-
a) Make the next pointer of the node point towards the
first node of the list
b) Make the start pointer point towards this new node
 If the list is empty simply make the start pointer
point towards the new node;
void insert_beg(node* p)
{
node* temp;
if(start==NULL) //if the list is empty
{
start=p;
cout<<”nNode inserted successfully at the
beginning”;
}
else {
temp=start;
start=p;
p->next=temp; //making new node point at
} the first node of the list
}
Inserting at the end
Here we simply need to make the next pointer
of the last node point to the new node
void insert_end(node* p)
{
node *q=start;
if(start==NULL)
{
start=p;
cout<<”nNode inserted successfully at the end…!!!n”;
}
else{
while(q->link!=NULL)
q=q->link;
q->next=p;
}
}
Inserting after an element
Here we again need to do 2 steps :-
 Make the next pointer of the node to be inserted
point to the next node of the node after which you
want to insert the node
 Make the next pointer of the node after which the
node is to be inserted, point to the node to be
inserted
void insert_after(int c,node* p)
{
node* q;
q=start;
for(int i=1;i<c;i++)
{
q=q->link;
if(q==NULL)
cout<<”Less than “<<c<<” nodes in the list…!!!”;
}
p->link=q->link;
q->link=p;
cout<<”nNode inserted successfully”;
}
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
threetwoone
start
void del_first()
{
if(start==NULL)
cout<<”nError……List is emptyn”;
else
{
node* temp=start;
start=temp->link;
delete temp;
cout<<”nFirst node deleted successfully….!!!”;
}
}
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
node3node2node1
start
void del_last()
{
if(start==NULL)
cout<<”nError….List is empty”;
else
{
node* q=start;
while(q->link->link!=NULL)
q=q->link;
node* temp=q->link;
q->link=NULL;
delete temp;
cout<<”nDeleted successfully…”;
}
}
}
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
void del(int c)
{
node* q=start;
for(int i=2;i<c;i++)
{
q=q->link;
if(q==NULL)
cout<<”nNode not foundn”;
}
if(i==c)
{
node* p=q->link; //node to be deleted
q->link=p->link; //disconnecting the node p
delete p;
cout<<“Deleted Successfully”;
}
}
Searching a SLL
 Searching involves finding the required element in the
list
 We can use various techniques of searching like linear
search or binary search where binary search is more
efficient in case of Arrays
 But in case of linked list since random access is not
available it would become complex to do binary search
in it
 We can perform simple linear search traversal
In linear search each node is traversed till the data in
the node matches with the required value
void search(int x)
{
node*temp=start;
while(temp!=NULL)
{
if(temp->data==x)
{
cout<<“FOUND ”<<temp->data;
break;
}
temp=temp->next;
}
}
Reversing a linked list
• We can reverse a linked list by reversing the
direction of the links between 2 nodes
 We make use of 3 structure pointers say p,q,r
 At any instant q will point to the node next to p and r
will point to the node next to q
NULLHead P q r
NULL
• For next iteration p=q and q=r
• At the end we will change head to the last node
p q
Code
void reverse()
{
node*p,*q,*r;
if(start==NULL)
{
cout<<"nList is emptyn";
return;
}
p=start;
q=p->link;
p->link=NULL;
while(q!=NULL)
{
r=q->link;
q->link=p;
p=q;
q=r;
}
start=p;
cout<<"nReversed successfully";
}
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
786200 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 .nextprevious.
inf
Inserting at beginning
void insert_beg(node *p)
{
if(start==NULL)
{
start=p;
cout<<"nNode inserted successfully at the beginningm";
}
else
{
node* temp=start;
start=p;
temp->previous=p; //making 1st node’s previous point to the
new node
p->next=temp; //making next of the new node point to the
1st node
cout<<"nNode inserted successfully at the beginningn";
}
Inserting at the end
void insert_end(node* p)
{
if(start==NULL)
{
start=p;
cout<<"nNode inserted successfully at the end";
}
else
{
node* temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->previous=temp;
cout<<"nNode inserted successfully at the endn";
}
}
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
void insert_after(int c,node* p)
{
temp=start;
for(int i=1;i<c-1;i++)
{
temp=temp->next;
}
p->next=temp->next;
temp->next->previous=p;
temp->next=p;
p->previous=temp;
cout<<"nInserted successfully";
}
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
void del_at(int c)
{
node*s=start;
{
for(int i=1;i<c-1;i++)
{
s=s->next;
}
node* p=s->next;
s->next=p->next;
p->next->previous=s;
delete p;
cout<<"nNode number "<<c<<" deleted successfully";
}
}
}
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.
• Polynomials
•Array Implementation:
• p1(x) = 8x3 + 3x2 + 2x + 6
• p2(x) = 23x4 + 18x - 3
6 2 3 8
0 2
Index
represents
exponents
-3 18 0 0 23
0 42
p1(x) p2(x)
•This is why arrays aren’t good to represent
polynomials:
• p3(x) = 16x21 - 3x5 + 2x + 6
6 2 0 0 -3 0 0 16…………
WASTE OF SPACE!
• 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.
• 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)
• 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.
Polynomials
A x a x a x a xm
e
m
e em 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 x8 3 1014 10 6
null
null
a x x3 2 114 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

More Related Content

What's hot (20)

Linked lists
Linked listsLinked lists
Linked lists
 
Linked list
Linked listLinked list
Linked list
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Sorting
SortingSorting
Sorting
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Hashing
HashingHashing
Hashing
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Queues
QueuesQueues
Queues
 
Array in c++
Array in c++Array in c++
Array in c++
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Stacks
StacksStacks
Stacks
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Array in c
Array in cArray in c
Array in c
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 

Viewers also liked

Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)John C. Havens
 
Double linked list
Double linked listDouble linked list
Double linked listraviahuja11
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Lovelyn Rose
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Trees data structure
Trees data structureTrees data structure
Trees data structureSumit Gupta
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked listFahd Allebdi
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8sumitbardhan
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
Open Legal Data Workshop at Stanford
Open Legal Data Workshop at StanfordOpen Legal Data Workshop at Stanford
Open Legal Data Workshop at StanfordHarry Surden
 
Harry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law OverviewHarry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law OverviewHarry Surden
 
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017Carol Smith
 

Viewers also liked (17)

7 Myths of AI
7 Myths of AI7 Myths of AI
7 Myths of AI
 
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
Individual-In-The-Loop (for Ethically Aligned Artificial Intelligence)
 
Cyber Crime
Cyber CrimeCyber Crime
Cyber Crime
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
Insertion and Deletion in Binary Search Trees (using Arrays and Linked Lists)
 
linked list
linked list linked list
linked list
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Open Legal Data Workshop at Stanford
Open Legal Data Workshop at StanfordOpen Legal Data Workshop at Stanford
Open Legal Data Workshop at Stanford
 
Harry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law OverviewHarry Surden - Artificial Intelligence and Law Overview
Harry Surden - Artificial Intelligence and Law Overview
 
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
 

Similar to Linked list

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptxchin463670
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdfkitty811
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdfharihelectronicspune
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 

Similar to Linked list (20)

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Linked list
Linked list Linked list
Linked list
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 

Recently uploaded

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 

Recently uploaded (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 

Linked list

  • 1. What are Linked Lists  A linked list is a linear data structure.  Nodes make up linked lists.  Nodes are structures made up of data and a pointer to another node.  Usually the pointer is called next.
  • 2. Arrays Vs Linked Lists Arrays Linked list Fixed size: Resizing is expensive Dynamic size Insertions and Deletions are inefficient: Elements are usually shifted Insertions and Deletions are efficient: No shifting Random access i.e., efficient indexing No random access  Not suitable for operations requiring accessing elements by index such as sorting No memory waste if the array is full or almost full; otherwise may result in much memory waste. Since memory is allocated dynamically(acc. to our need) there is no waste of memory. Sequential access is faster [Reason: Elements in contiguous memory locations] Sequential access is slow [Reason: Elements not in contiguous memory locations]
  • 3. Types of lists  There are two basic types of linked list Singly Linked list Doubly linked list
  • 4. 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
  • 5. Schematic representation  Here is a singly-linked list (SLL): a b c d myList • Each node contains a value(data) and a pointer to the next node in the list • myList is the header pointer which points at the first node in the list
  • 6. Basic Operations on a list • Creating a List • Inserting an element in a list • Deleting an element from a list • Searching a list • Reversing a list
  • 7. Creating a node struct node{ int data; // A simple node of a linked list node*next; }*start; //start points at the first node start=NULL ; initialised to NULL at beginning
  • 8. node* create( int num) //say num=1 is passed from main { node*ptr; ptr= new node; //memory allocated dynamically if(ptr==NULL) ‘OVERFLOW’ // no memory available exit(1); else { ptr->data=num; ptr->next=NULL; return ptr; } }
  • 9. To be called from main() as:- void main() { node* ptr; int data; cin>>data; ptr=create(data); }
  • 10. Inserting the node in a SLL There are 3 cases here:- Insertion at the beginning Insertion at the end Insertion after a particular node
  • 11. Insertion at the beginning There are two steps to be followed:- a) Make the next pointer of the node point towards the first node of the list b) Make the start pointer point towards this new node  If the list is empty simply make the start pointer point towards the new node;
  • 12.
  • 13. void insert_beg(node* p) { node* temp; if(start==NULL) //if the list is empty { start=p; cout<<”nNode inserted successfully at the beginning”; } else { temp=start; start=p; p->next=temp; //making new node point at } the first node of the list }
  • 14. Inserting at the end Here we simply need to make the next pointer of the last node point to the new node
  • 15. void insert_end(node* p) { node *q=start; if(start==NULL) { start=p; cout<<”nNode inserted successfully at the end…!!!n”; } else{ while(q->link!=NULL) q=q->link; q->next=p; } }
  • 16. Inserting after an element Here we again need to do 2 steps :-  Make the next pointer of the node to be inserted point to the next node of the node after which you want to insert the node  Make the next pointer of the node after which the node is to be inserted, point to the node to be inserted
  • 17.
  • 18. void insert_after(int c,node* p) { node* q; q=start; for(int i=1;i<c;i++) { q=q->link; if(q==NULL) cout<<”Less than “<<c<<” nodes in the list…!!!”; } p->link=q->link; q->link=p; cout<<”nNode inserted successfully”; }
  • 19. Deleting a node in SLL Here also we have three cases:-  Deleting the first node Deleting the last node Deleting the intermediate node
  • 20. 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 threetwoone start
  • 21. void del_first() { if(start==NULL) cout<<”nError……List is emptyn”; else { node* temp=start; start=temp->link; delete temp; cout<<”nFirst node deleted successfully….!!!”; } }
  • 22. 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 node3node2node1 start
  • 23. void del_last() { if(start==NULL) cout<<”nError….List is empty”; else { node* q=start; while(q->link->link!=NULL) q=q->link; node* temp=q->link; q->link=NULL; delete temp; cout<<”nDeleted successfully…”; } } }
  • 24. 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
  • 25. void del(int c) { node* q=start; for(int i=2;i<c;i++) { q=q->link; if(q==NULL) cout<<”nNode not foundn”; } if(i==c) { node* p=q->link; //node to be deleted q->link=p->link; //disconnecting the node p delete p; cout<<“Deleted Successfully”; } }
  • 26. Searching a SLL  Searching involves finding the required element in the list  We can use various techniques of searching like linear search or binary search where binary search is more efficient in case of Arrays  But in case of linked list since random access is not available it would become complex to do binary search in it  We can perform simple linear search traversal
  • 27. In linear search each node is traversed till the data in the node matches with the required value void search(int x) { node*temp=start; while(temp!=NULL) { if(temp->data==x) { cout<<“FOUND ”<<temp->data; break; } temp=temp->next; } }
  • 28. Reversing a linked list • We can reverse a linked list by reversing the direction of the links between 2 nodes
  • 29.  We make use of 3 structure pointers say p,q,r  At any instant q will point to the node next to p and r will point to the node next to q NULLHead P q r NULL • For next iteration p=q and q=r • At the end we will change head to the last node p q
  • 30. Code void reverse() { node*p,*q,*r; if(start==NULL) { cout<<"nList is emptyn"; return; } p=start; q=p->link; p->link=NULL; while(q!=NULL) { r=q->link; q->link=p; p=q; q=r; } start=p; cout<<"nReversed successfully"; }
  • 31. 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
  • 32. 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.
  • 33. 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 786200 400 200 656 400 786 777 NULL
  • 34. 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)
  • 35. Structure of DLL struct node { int data; node*next; node*previous; //holds the address of previous node }; .Data .nextprevious. inf
  • 37. void insert_beg(node *p) { if(start==NULL) { start=p; cout<<"nNode inserted successfully at the beginningm"; } else { node* temp=start; start=p; temp->previous=p; //making 1st node’s previous point to the new node p->next=temp; //making next of the new node point to the 1st node cout<<"nNode inserted successfully at the beginningn"; }
  • 39. void insert_end(node* p) { if(start==NULL) { start=p; cout<<"nNode inserted successfully at the end"; } else { node* temp=start; while(temp->next!=NULL) { temp=temp->next; } temp->next=p; p->previous=temp; cout<<"nNode inserted successfully at the endn"; } }
  • 40. 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
  • 41. void insert_after(int c,node* p) { temp=start; for(int i=1;i<c-1;i++) { temp=temp->next; } p->next=temp->next; temp->next->previous=p; temp->next=p; p->previous=temp; cout<<"nInserted successfully"; }
  • 42. 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
  • 43. void del_at(int c) { node*s=start; { for(int i=1;i<c-1;i++) { s=s->next; } node* p=s->next; s->next=p->next; p->next->previous=s; delete p; cout<<"nNode number "<<c<<" deleted successfully"; } } }
  • 44. 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.
  • 45. • Polynomials •Array Implementation: • p1(x) = 8x3 + 3x2 + 2x + 6 • p2(x) = 23x4 + 18x - 3 6 2 3 8 0 2 Index represents exponents -3 18 0 0 23 0 42 p1(x) p2(x)
  • 46. •This is why arrays aren’t good to represent polynomials: • p3(x) = 16x21 - 3x5 + 2x + 6 6 2 0 0 -3 0 0 16………… WASTE OF SPACE!
  • 47. • 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.
  • 48. • 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)
  • 49. • 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.
  • 50. Polynomials A x a x a x a xm e m e em m ( ) ...1 2 0 1 2 0 Representation struct polynode { int coef; int exp; struct polynode * next; }; typedef struct polynode *polyptr; coef exp next
  • 51. • 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]
  • 52. • 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.
  • 53. Example 3 14 2 8 1 0 a 8 14 -3 10 10 6 b b x x x8 3 1014 10 6 null null a x x3 2 114 8
  • 54. 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
  • 55. 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