Sunteți pe pagina 1din 46

LINKED LISTS

• A linked list is a non sequential collection


of data items called nodes
LINKED LISTS

• Each node in a linked list has basically two


fields
• Data field
• Link field
LINKED LISTS

• The data field contains an actual value to


be stored and processed
• The link field contains the address of the
next data item in the linked list

Data field Link field


LINKED LISTS
• The logical and physical ordering of data
items in a linked list need not be the same

• The link field of the last node contains


NULL rather than a valid address

• It is a null pointer and indicates the end of


the list
LINKED LISTS
EMPTY LIST

• If the nodes are not present in a linked list,


then it is called an empty linked list or
empty list

• It is also called the null list


LINKED LISTS
REPRESENTATION OF LINEAR LINKED LIST
The linear linked list can be represented in memory
with the following declarations
class node
{ int data;
node link;
node(int val)
{ data=val;
link=null;
}}
LINKED LISTS

A new node can be created using


node freshnode= new node(value);

It can be accessed as
freshnode.data
freshnode.link
LINKED LISTS
OPERATION ON LINKED LIST
The basic operations to be performed on the
linked lists are
• Creation
• Insertion
• Deletion
• Traversing
• Searching
• Display
LINKED LISTS

Creation

• This operation is used to create a linked list


LINKED LISTS
Insertion
This operation is used to insert a new node in
the linked list at the specified position. A
new node may be inserted
• At the beginning of a linked list
• At the end of a linked list
• At the specified position in linked list
• If the list itself is empty, then the new node
is inserted as a first node
LINKED LISTS

Deletion
This operation is used to delete a node from
the linked list. A node may be deleted from
the
• Beginning of a linked list
• End of a linked list
• Specified item in the list
LINKED LISTS

Traversing

• It is a process of going through all the


nodes of a linked list from one end to the
other end
LINKED LISTS

Searching

• This operation is used to search a


particular element in a linked list

• If the desired element is found, we signal


operation SUCCESSFUL, otherwise
UNSUCCESSFULL
LINKED LISTS

Display

• This operation is used to print each and


every node’s information
LINKED LISTS
TYPES OF LINKED LIST

We can put linked list into the following


types
• Singly linked list
• Doubly linked list
• Circular linked list
• Circular doubly linked list
LINKED LISTS
Singly Linked List (Linear Linked list)

• A Singly linked list is one in which all


nodes are linked together in some
sequential manner
start
10 20 30 40 null
LINKED LIST

Singly Linked list

• The problem with this list is that we cannot


access the predecessor of node from the
current node
LINKED LIST

Doubly Linked list

• A doubly linked list is one in which all


nodes are linked together by multiple links
which help in accessing both the successor
node (next node) and predecessor node
(previous node) for any arbitrary node
within the list
LINKED LIST
Doubly Linked list
start null 10 20 5 15 null
left data right

Each node in a doubly linked list has two


pointer fields
• Pointer to left node
• Pointer to right node
LINKED LISTS
Circular Linked List

• A Circular linked list is one which has no


beginning and no end

start
10 20 30 40
LINKED LIST
Circular Doubly Linked list
start 10 20 5 15
left data right

A circular doubly linked list is one which


has both the successor pointer and
predecessor pointer in circular manner
LINKED LIST
ADVANTAGES
• Linked lists are dynamic data structures
• can grow and shrink during the execution of a
program
• Efficient memory utilization
• Memory is not pre-allocated
• Insertion and deletions are easier and
efficient
LINKED LIST
DISADVANTAGES

• More memory
• If the number of fields are more, then more
memory space is needed

• Access to an arbitrary data item is little bit


cumbersome and also time consuming
LINKED LIST
Inserting a node at the beginning

start
20 40 25 48 null

32 null

newnode
LINKED LIST
Inserting a node at the beginning

start
20 40 25 48 null

32
newnode
LINKED LIST
Void insert_at_begin(int item)
{
node newnode=new node(item);
if (start!=null)
newnode.link=start;
start=newnode;
}
LINKED LIST
Inserting a node at the end

start
20 40 25 48 null

32 null

newnode
LINKED LIST
Inserting a node at the end

loc
start
20 40 25 48 null

32 null

newnode
LINKED LIST
Inserting a node at the end

loc
start
20 40 25 48

32 null

newnode
LINKED LIST
Inserting a node at the end
Void insert_at_end(int item)
{ node newnode=new node(item);
loc=start;
while (loc.link!=null)
loc=loc.link;
loc.link=newnode;
}
LINKED LIST
Inserting a new node at the specified
position
start
20 40 25 48 null

32 null
Insert a new node after
the second node newnode
LINKED LIST
Inserting a new node at the specified
position
start
20 40 25 48 null

loc

32 null

newnode
LINKED LIST
Inserting a new node at the specified
position
start
20 40 25 48 null

loc

32

newnode
LINKED LIST
Inserting a new node at the specified
position
Void insert_specific_position(int item, int pos)
{ node newnode= new node(item);
loc=start; temp=1;
while(temp<pos){
loc=loc.link;
temp=temp+1;}
newnode.link=loc.link;
loc.link=newnode;
}
LINKED LIST
Deleting the first node

start
20 40 25 48 null
LINKED LIST
Deleting the first node

start
20 40 25 48 null
LINKED LIST
Deleting the first node
Void delete_firstnode()
{ if (start!= null)
start=start.link;
}
LINKED LIST
Deleting the last node

start
20 40 25 48 null
LINKED LIST
Deleting the last node
prev last
start
20 40 25 48 null
LINKED LIST
Deleting the last node
prev last
start
20 40 25 null 48 null
LINKED LIST
Deleting the last node
Void delete_lastnode()
{ last=start;
while(last.link!=null)
{ prev=last;
last=last.link;
}
prev.link=null;
}
LINKED LIST
Deleting the node containing a particular
item
start
20 40 25 48 null

Delete the node containing value 25


LINKED LIST
Deleting the node containing a particular
item
start
20 40 25 48 null
prev loc

Delete the node containing value 25


LINKED LIST
Deleting the node containing a particular
item
start
20 40 25 48 null
prev loc

Delete the node containing value 25


LINKED LIST
Deleting the node containing a particular item
Void delete_particular_item(int item)
{ loc=start; prev=start;
while (loc!=null && loc.data!=item)
{ prev=loc;
loc=loc.link;
}
if (prev==start)
{ start=null;
return;
}
LINKED LIST
Deleting the node containing a particular
item
if (loc!=null)
prev.link=loc.link;
else return unsuccessful;
}

S-ar putea să vă placă și