Linked List
Linked List
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to
full; otherwise may result in much memory our need) there is no waste of memory.
waste.
Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]
Types of lists
There are two basic typesof linked list
myList
a b c d
• Creating a List
• Inserting an element in alist
• Deleting anelement 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 firstnode
start=NULL ; initialised to NULL at beginning
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;
}
}
To be called from main() as:-
void main()
{
node* ptr;
int data;
cin>>data;
ptr=create(data);
}
Inserting the node in a SLL
Insertion at thebeginning
Insertion at theend
Insertion after a particular node
Insertion at the beginning
There are two steps to be followed:-
start
start
To be deleted
void del(intc)
{
node* q=start;
for(int i=2; i<c;i++)
{
q=q->link;
if(q==NULL)
cout<<”\nNode not found\n”;
}
if(i==c)
{
node* p=q->link; //node to bedeleted
q->link=p->link; //disconnecting the nodep
delete p;
cout<<“Deleted Successfully”;
}
}
Searching a SLL
Searching involves finding the required element inthe
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(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 2 nodes
We make use of 3 structure pointers say p,q,r
Head P p
q rq NULL
NULL
A B C
NULL 11 786 200 656 400 786 777 NULL
Advantages: Disadvantages:
Can be traversed in either Requires more space
direction (may be List manipulations are
essential for some slower (because more
programs) links must bechanged)
Some operations, suchas Greater chance of having
deletion and inserting bugs (because more links
before a node, become must be manipulated)
easier
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previousnode
};
Adjusting the next and previous pointers of the nodes b/w which
the new nodeaccordingly
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 twolinks
• In this example,wewill delete node b
myDLL
a b c
• With a circular list, a pointer to the last node gives easy access also
to the first node, by following one link. Thus, in applications that
require access to both ends of the list, a circular structure allows one
to handle the structure by a single pointer, instead of two.
• Insertion
• Deletion
• Display
Insertion :
• Insertion can be of three types.
▫ Insert at first
▫ Insert at last
▫ Insert after constant
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
temp = head;
while(temp->next != head)
temp = temp->next;
temp -> next = ptr;
ptr->next = head;
head = ptr;
Insertion into circular singly linked list
at the end
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
temp = head;
while(temp->next != head)
temp = temp->next;
temp -> next = ptr;
ptr -> next = head;
Deletion in circular singly linked list at
beginning
Scenario 1: (The list is Empty)
if(head == NULL)
{
printf("\nUNDERFLOW");
return;
}
if(head->next == head)
{
head = NULL;
}
Scenario 3: (The list contains more than one node)
ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
head = ptr->next;
Deletion in Circular singly linked list at the end
if(head == NULL)
{
printf("\nUNDERFLOW");
return;
}
if(head->next == head)
{
head = NULL;
}
Scenario 3(the list contains more than one element)
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
Insertion in circular doubly linked list at
beginning
if(head == NULL)
{
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> prev = temp;
head -> prev = ptr;
ptr -> next = head;
head = ptr;
Insertion in circular doubly linked list at
end
if(head == NULL)
{
head = ptr;
ptr -> next = head;
ptr -> prev = head;
}
temp = head;
while(temp->next != head)
temp = temp->next;
head -> prev = ptr;
ptr -> next = head;
temp->next = ptr;
ptr ->prev=temp;
Deletion in Circular doubly linked list at
beginning
Scenario 1 (the list is empty)
if(head == NULL)
{
printf("\nUNDERFLOW");
return;
}
if(head->next == head)
{
head = NULL;
}
Scenario 3(the list contains more than
one element)
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
if(head == NULL)
{
printf("\nUNDERFLOW");
return;
}
if(head->next == head)
{
head = NULL;
}
Scenario 3 (the list contains more than
one element)
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> prev -> next = head;
head -> prev = temp -> prev;
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)
6 2 3 8 -3 18 0 0 23
0 2 0 2 4
Index
represents
exponents
•This is why arrays aren’t good to represent
polynomials:
6 2 0 0 -3 0 ………… 0 16
WASTE OF SPACE!
• Advantages of using an Array:
P1 23 9 18 7 41 6 18 7 3 0
P2 4 6 10 4 12 1 8 0
struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;
14 10
b 8x 3x 10 x 6
b
8 14 -3 10 10 6 null
Adding Polynomials
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 a->expon == b->expon
d
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 -3 10 2 8
d
a->expon > b->expon
THANK YOU