SlideShare ist ein Scribd-Unternehmen logo
1 von 40
DATA STRUCTURE
Linked list
p
Linked Lists
• A linked list is a series of connected nodes
• Each node contains at least
– A piece of data (any type)
– Pointer to the next node in the list
• Head: pointer to the first node
• The last node points to NULL
A 
Head
B C
A
data pointer
node
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]
List Overview
• Linked lists
– Abstract data type (ADT)
• Basic operations of linked lists
– Insert, find, delete, print, etc.
• Variations of linked lists
– Circular linked lists
– Doubly linked lists
Basic Operations on a list
• Creating a List
• Inserting an element in alist
• Deleting anelement from a list
• Searching a list
• Reversing alist
Creating a node
// A simple nodeof a linked list
struct node{
int data;
node*next;
}*start;
start=NULL ;
//start points at the firstnode
initialised to NULL atbeginning
node* create( int num) //say num=1 is passed frommain
{
node*ptr;
ptr= new node; //memory allocateddynamically
if(ptr==NULL)
‘OVERFLOW’ // no memoryavailable
exit(1);
else
{
ptr->data=num;
ptr->next=NULL;
return ptr;
}
}
Inserting the node in a SLL
There are 3 cases here:-
Insertion at thebeginning
Insertion at theend
Insertion aftera particular node
//if the list isempty
void insert_beg(node* p)
{
node* temp;
if(start==NULL)
{
start=p;
cout<<”nNode inserted successfully atthe
beginning”;
}
else {
}
temp=start;
start=p;
p->next=temp; //making new node point at
the first node of thelist
}
Inserting at the end
Herewesimply 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 theend…!!!n”;
}
else{
while(q->link!=NULL)
q=q->link;
q->next=p;
}
}
p
q
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 thelist…!!!”;
}
p->link=q->link;
q->link=p;
cout<<”nNode inserted successfully”;
}
Deleting a node in SLL
Here also we have threecases:-
 Deleting the first node
Deleting the last node
Deleting the intermediatenode
Deleting the first node
threetwoone
Here we apply 2steps:-
 Making the start pointerpoint towards the 2nd
node
 Deleting the first node using deletekeyword
start
void del_first()
{
if(start==NULL)
cout<<”nError……List isemptyn”;
else
{
node* temp=start;
start=temp->link;
delete temp;
cout<<”nFirst node deleted successfully….!!!”;
}
}
Deleting the last node
node3node2node1
Here we apply 2steps:-
 Making the second last node’s next pointerpoint
to NULL
 Deleting the last node viadelete keyword
start
void del_last()
{
if(start==NULL)
cout<<”nError….List isempty”;
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(intc)
{
node* q=start;
for(int i=2;i<c;i++)
{
q=q->link;
if(q==NULL)
cout<<”nNode notfoundn”;
}
if(i==c)
{
//node to bedeleted
//disconnecting the nodep
node* p=q->link;
q->link=p->link;
deletep;
cout<<“Deleted Successfully”;
}
}
Searching a SLL
 Searching involves finding the required element inthe
list
 Wecan usevarious techniquesof searching like linear
search or binary search where binary search is more
efficient in case ofArrays
 But in case of linked list since random access is not
available itwould becomecomplex todo binary search
in it
 Wecan perform simple linearsearch traversal
In linear search each node is traversed till the data in
the node matches with the required value
void search(intx)
{
node*temp=start;
while(temp!=NULL)
{
if(temp->data==x)
{
cout<<“FOUND ”<<temp->data;
break;
}
temp=temp->next;
}
}
Reversing a linked list
• Wecan reverse a linked list by reversing the
direction of the links between 2nodes
 We make useof 3 structurepointers say p,q,r
 Atany instantq will point to the node next topand r
will point to the node next toq
NULLHead P
NULL
• For next iteration p=q andq=r
• At the end wewill change head to the last node
qp rq
AAA Code
void reverse()
{
node*p,*q,*r;
if(start==NULL)
{
cout<<"nList isemptyn";
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";
}
A B C D
P q r
A B C
NULL
P q
Doubly Linked List
1. Doubly linked list is a linked data structurethatconsistsof a set of
sequentially linked records callednodes.
2 . Each node contains three fields::
-: one is data part which contain dataonly.
-: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 listcontain three fields: an integervalue, 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 ineither
direction (may be
essential for some
programs)
 Some operations, suchas
deletion and inserting
before a node, become
easier
 Disadvantages:
 Requires morespace
 List manipulations are
slower (because more
links must bechanged)
 Greater chance of having
bugs (because more links
must be manipulated)
Structure of DLL
//holds the address of previousnode
struct node
{
int data;
node*next;
node*previous;
};
.Data .nextprevious.
inf
Inserting at beginning
void insert_beg(node *p)
{
if(start==NULL)
{
start=p;
cout<<"nNode inserted successfully at thebeginningm";
}
else
{
node* temp=start;
start=p;
//making 1st node’s previous point tothe
//making nextof the new node point to the
temp->previous=p;
new node
p->next=temp;
1st node
cout<<"nNode inserted successfully at thebeginningn";
}
Inserting at the end
void insert_end(node* p)
{
if(start==NULL)
{
start=p;
cout<<"nNode inserted successfullyat theend";
}
else
{
node* temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->previous=temp;
cout<<"nNode inserted successfullyat theendn";
}
}
Making nextand previous pointerof the node to be
inserted pointaccordingly
Adjusting the nextand previous pointersof the nodes b/wwhich
the new nodeaccordingly
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";
}
temp
p
prev next
X
X
1
2
3
4
Deleting a node
• Node deletion from a DLL involves changing twolinks
• In this example,wewill delete node b
myDLL
a
b
c
• Wedon’t have todo anything about the links in node b
• Garbage collection will take care of deletednodes
• Deletion of the first nodeor the last node isa special
case
void del_at(intc)
{
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";
}
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structuresDurgaDeviCbit
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data StructureMeghaj Mallick
 
Bubble sort
Bubble sortBubble sort
Bubble sortManek Ar
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
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
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 

Was ist angesagt? (20)

single linked list
single linked listsingle linked list
single linked list
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Linked List
Linked ListLinked List
Linked List
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Linked List
Linked ListLinked List
Linked List
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Linked list
Linked list Linked list
Linked list
 
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...
 
Linked lists
Linked listsLinked lists
Linked lists
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Linked list
Linked listLinked list
Linked list
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Queues
QueuesQueues
Queues
 

Ähnlich wie Linear data structure concepts

Ähnlich wie Linear data structure concepts (20)

linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Linked list
Linked listLinked list
Linked list
 
DS Module 03.pdf
DS Module 03.pdfDS Module 03.pdf
DS Module 03.pdf
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Data structure
Data structureData structure
Data structure
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.pptx
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Stacks and Queues with Linked List.pdf
Stacks and Queues with Linked List.pdfStacks and Queues with Linked List.pdf
Stacks and Queues with Linked List.pdf
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Data Structure
Data StructureData Structure
Data Structure
 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 

Mehr von Akila Krishnamoorthy

Mehr von Akila Krishnamoorthy (13)

Automata Theory - Turing machine
Automata Theory - Turing machineAutomata Theory - Turing machine
Automata Theory - Turing machine
 
Automata theory - RE to DFA Conversion
Automata theory - RE to DFA ConversionAutomata theory - RE to DFA Conversion
Automata theory - RE to DFA Conversion
 
Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)
 
Automata theory -RE to NFA-ε
Automata theory -RE to  NFA-εAutomata theory -RE to  NFA-ε
Automata theory -RE to NFA-ε
 
Automata theory - NFA ε to DFA Conversion
Automata theory - NFA ε to DFA ConversionAutomata theory - NFA ε to DFA Conversion
Automata theory - NFA ε to DFA Conversion
 
Automata theory - NFA to DFA Conversion
Automata theory - NFA to DFA ConversionAutomata theory - NFA to DFA Conversion
Automata theory - NFA to DFA Conversion
 
Automata theory -- NFA and DFA construction
Automata theory -- NFA and DFA  constructionAutomata theory -- NFA and DFA  construction
Automata theory -- NFA and DFA construction
 
Intro to automata theory
Intro to automata theoryIntro to automata theory
Intro to automata theory
 
Automata theory -Conversion of ε nfa to nfa
Automata theory -Conversion of ε nfa to nfaAutomata theory -Conversion of ε nfa to nfa
Automata theory -Conversion of ε nfa to nfa
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Slr parser
Slr parserSlr parser
Slr parser
 
CLR AND LALR PARSER
CLR AND LALR PARSERCLR AND LALR PARSER
CLR AND LALR PARSER
 
Keypoints c strings
Keypoints   c stringsKeypoints   c strings
Keypoints c strings
 

Kürzlich hochgeladen

Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Kürzlich hochgeladen (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 

Linear data structure concepts

  • 2.
  • 3. p
  • 4. Linked Lists • A linked list is a series of connected nodes • Each node contains at least – A piece of data (any type) – Pointer to the next node in the list • Head: pointer to the first node • The last node points to NULL A  Head B C A data pointer node
  • 5. 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]
  • 6. List Overview • Linked lists – Abstract data type (ADT) • Basic operations of linked lists – Insert, find, delete, print, etc. • Variations of linked lists – Circular linked lists – Doubly linked lists
  • 7. Basic Operations on a list • Creating a List • Inserting an element in alist • Deleting anelement from a list • Searching a list • Reversing alist
  • 8. Creating a node // A simple nodeof a linked list struct node{ int data; node*next; }*start; start=NULL ; //start points at the firstnode initialised to NULL atbeginning
  • 9. node* create( int num) //say num=1 is passed frommain { node*ptr; ptr= new node; //memory allocateddynamically if(ptr==NULL) ‘OVERFLOW’ // no memoryavailable exit(1); else { ptr->data=num; ptr->next=NULL; return ptr; } }
  • 10. Inserting the node in a SLL There are 3 cases here:- Insertion at thebeginning Insertion at theend Insertion aftera particular node
  • 11.
  • 12. //if the list isempty void insert_beg(node* p) { node* temp; if(start==NULL) { start=p; cout<<”nNode inserted successfully atthe beginning”; } else { } temp=start; start=p; p->next=temp; //making new node point at the first node of thelist }
  • 13. Inserting at the end Herewesimply need to make the next pointer of the last node point to the new node
  • 14. void insert_end(node* p) { node *q=start; if(start==NULL) { start=p; cout<<”nNode inserted successfully at theend…!!!n”; } else{ while(q->link!=NULL) q=q->link; q->next=p; } }
  • 15. p q
  • 16. 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 thelist…!!!”; } p->link=q->link; q->link=p; cout<<”nNode inserted successfully”; }
  • 17. Deleting a node in SLL Here also we have threecases:-  Deleting the first node Deleting the last node Deleting the intermediatenode
  • 18. Deleting the first node threetwoone Here we apply 2steps:-  Making the start pointerpoint towards the 2nd node  Deleting the first node using deletekeyword start
  • 19. void del_first() { if(start==NULL) cout<<”nError……List isemptyn”; else { node* temp=start; start=temp->link; delete temp; cout<<”nFirst node deleted successfully….!!!”; } }
  • 20. Deleting the last node node3node2node1 Here we apply 2steps:-  Making the second last node’s next pointerpoint to NULL  Deleting the last node viadelete keyword start
  • 21. void del_last() { if(start==NULL) cout<<”nError….List isempty”; else { node* q=start; while(q->link->link!=NULL) q=q->link; node* temp=q->link; q->link=NULL; delete temp; cout<<”nDeleted successfully…”; } } }
  • 22. 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
  • 23. void del(intc) { node* q=start; for(int i=2;i<c;i++) { q=q->link; if(q==NULL) cout<<”nNode notfoundn”; } if(i==c) { //node to bedeleted //disconnecting the nodep node* p=q->link; q->link=p->link; deletep; cout<<“Deleted Successfully”; } }
  • 24. Searching a SLL  Searching involves finding the required element inthe list  Wecan usevarious techniquesof searching like linear search or binary search where binary search is more efficient in case ofArrays  But in case of linked list since random access is not available itwould becomecomplex todo binary search in it  Wecan perform simple linearsearch traversal
  • 25. In linear search each node is traversed till the data in the node matches with the required value void search(intx) { node*temp=start; while(temp!=NULL) { if(temp->data==x) { cout<<“FOUND ”<<temp->data; break; } temp=temp->next; } }
  • 26. Reversing a linked list • Wecan reverse a linked list by reversing the direction of the links between 2nodes
  • 27.  We make useof 3 structurepointers say p,q,r  Atany instantq will point to the node next topand r will point to the node next toq NULLHead P NULL • For next iteration p=q andq=r • At the end wewill change head to the last node qp rq
  • 28. AAA Code void reverse() { node*p,*q,*r; if(start==NULL) { cout<<"nList isemptyn"; 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"; } A B C D P q r A B C NULL P q
  • 29. Doubly Linked List 1. Doubly linked list is a linked data structurethatconsistsof a set of sequentially linked records callednodes. 2 . Each node contains three fields:: -: one is data part which contain dataonly. -: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.
  • 30. NODE A B C A doubly linked listcontain three fields: an integervalue, 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
  • 31. DLL’s compared to SLL’s  Advantages:  Can be traversed ineither direction (may be essential for some programs)  Some operations, suchas deletion and inserting before a node, become easier  Disadvantages:  Requires morespace  List manipulations are slower (because more links must bechanged)  Greater chance of having bugs (because more links must be manipulated)
  • 32. Structure of DLL //holds the address of previousnode struct node { int data; node*next; node*previous; }; .Data .nextprevious. inf
  • 34. void insert_beg(node *p) { if(start==NULL) { start=p; cout<<"nNode inserted successfully at thebeginningm"; } else { node* temp=start; start=p; //making 1st node’s previous point tothe //making nextof the new node point to the temp->previous=p; new node p->next=temp; 1st node cout<<"nNode inserted successfully at thebeginningn"; }
  • 36. void insert_end(node* p) { if(start==NULL) { start=p; cout<<"nNode inserted successfullyat theend"; } else { node* temp=start; while(temp->next!=NULL) { temp=temp->next; } temp->next=p; p->previous=temp; cout<<"nNode inserted successfullyat theendn"; } }
  • 37. Making nextand previous pointerof the node to be inserted pointaccordingly Adjusting the nextand previous pointersof the nodes b/wwhich the new nodeaccordingly Inserting after a node
  • 38. 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"; } temp p prev next X X 1 2 3 4
  • 39. Deleting a node • Node deletion from a DLL involves changing twolinks • In this example,wewill delete node b myDLL a b c • Wedon’t have todo anything about the links in node b • Garbage collection will take care of deletednodes • Deletion of the first nodeor the last node isa special case
  • 40. void del_at(intc) { 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"; } } }