0% found this document useful (0 votes)
17 views59 pages

Linked List - 1

Uploaded by

Rida Zainab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views59 pages

Linked List - 1

Uploaded by

Rida Zainab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 59

Linked Lists

Dr. Tahir Maqsood


node

Department of Computer Science


CUI, Lahore Campus data pointer
Outline

 Static vs Dynamic Data Structure


 Limitations of List ADT with Array Implementation.
 Dynamic List (Linked lists)
 Variations of linked lists
 Single linked lists
 Circular linked lists
 Doubly linked lists
 Basic operations of linked lists
 Insert
 Iinsert at start
 Insert at end
 Insert at specific position
 delete,
 print,
Department find
of Computer etc.
Science
Array Limitations

 What are the limitations of an array, as a data


structure?
 Fixed size(e.g. int L[10])
 What is the drawback of fixed size?
 Can not grow or shrink as needed
 Solution: Use dynamic Data Structure
 Physically stored in consecutive memory locations
 What is drawback of consecutive memory
 Suppose we need 10 memory location
 The 10 locations are available
but not consecutive
 Solution: store elements where space
available
Department of Computer Science
Linked Data Structures

 How we ensure these two solutions?


 Use Linked Data Structure
 A linked data structure consists of items that are linked to
other items(how?)
 each item points to another item
 Now what is linked list?
 A linked list is an ordered sequence of items called nodes
 Each node contains at least
 A piece of data (any type)
 Pointer to the next node in the list

node

Department of Computer Science data pointer


Linked Data Structures

 In linked list, adjacency between the elements


are maintained by means of link or pointers

Department of Computer Science


Conceptual Diagram of a Singly-Linked List

A 

Head

 The first item (node) in the linked list is accessed via a


front or head pointer
 The linked list is defined by its head (this is its starting
point)
 Head: pointer to the first node
 The last node points to NULL
Department of Computer Science
Conceptual Diagram of a Singly-Linked List

head pointer "defines" the linked list


head (note that it is not a node)

these are nodes

data data data .

Department of Computer Science


Linked List inside Computer Memory

Conceptual diagram

Linked list in memory


Department of Computer Science
Advantages of Linked Lists

 The items do not have to be stored in


consecutive memory locations: the successor
can be anywhere physically
 So, can insert and delete items without
shifting data
 Can increase the size of the data structure
easily
 Linked lists can grow dynamically (i.e. at run
time) – the amount of memory space allocated
can grow and shrink as needed

Department of Computer Science


Variations of Linked Lists

 Singly linked list: each item points to the next


item
 Circular or Non circular

A 

Head

A B C

Head

Department of Computer Science


Variations of Linked Lists

 Doubly linked list: each item points to the


next item and to the previous item

 

Head

Department of Computer Science


Implementation of Linked List Using C++

 Declare Node structure for the nodes


 data: int-type data e.g. example
 link: a pointer to the next node in the list

struct Node
{
int data; // data
Node* link; // pointer to next
};

node

data link
Department of Computer Science
;
Creating single Linked List
 We use two important pointers, i.e. head and
tail
 head points to first node
 tail points to last node.
 Demo

head NULL tail NULL

 Initially Linked list has no node, i.e. Linked list is


empty
 It means if the head is equal to NULL then we can
conclude that the linked list is empty.
Department of Computer Science 4-13
;
Creating single Linked List

head 100 node *temp=new node;


100 temp->data=value;
if(head==NULL) temp->link=NULL;
50 NULL
{ if there is just one node in
head=temp; linked lists, then it is called
tail=temp; tail both head and tail.
100
temp=NULL;
}
head 100
100 200
else 50 200 55 NULL
{
tail->link=temp;
tail=temp; tail 200
}

Department of Computer Science 4-14


;
Creating single Linked List
node *temp=new node;
head 100 temp->data=value;
100 temp->link=NULL; 200
50 200 55 NULL

head tail 200


100
100 200 300
50 200 50 300 40 NULL
if(head==NULL)
{
head=temp; tail 300
else
tail=temp;
{
temp=NULL;
tail->link=temp;
}
tail=temp;
}
Department of Computer Science 4-15
;
Creating single Linked List

 Now, we will write a function for creating


LinkedList( node will be inserted at end)
void createnode(int value)
{
node *temp=new node;
temp->data=value;
temp->link=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->link=temp;
tail=temp;
}
}
Department of Computer Science 4-16
;
Creating single Linked List

void createnode(int value)


{ temp 100
node *temp=new node;
temp->data=value; 50
temp->next=NULL; 50 NULL
if(head==NULL)
{ head 100
head=temp; 100
tail=temp; 50 NULL
temp=NULL;
}
else 100 tail 100
{ 100 200
tail->next=temp;
tail=temp; } 50 200 55 NULL
}

Department of Computer Science tail 200


Displaying single Linked List

 Now we have a working linked list which allows


creating nodes.
 If we want to see that what is placed in our
linked list then we will have to make a display
function.

Department of Computer Science 4-18


Displaying single Linked List

 The logic behind this function is that:


 Make a temporary node node *temp=new node;
 pass the address of the head node to it.
temp=head;
 Now we want to print all the nodes on the screen. So
we need a loop which runs as many times as nodes
exist.
 Every node contains the address of the next node so the
temporary node walks through the whole linked list.
 If the temporary node becomes equal to NULL then the loop

would be terminated. while(temp!=NULL)


 {
Print “temp->data";
temp=temp->link;
Department of Computer Science }
Displaying single Linked List
void display() 25
{
node *temp=new node; temp
temp=head; temp 0x0F032009
while(temp!=NULL)
{
cout<<temp->data<<"\t"; 25
temp=temp->link; temp 0x0F032010
}
}

Department of Computer Science


Displaying single Linked List
void display()
{ 25, 50
node *temp=new node;
temp=head;
while(temp!=NULL) temp 0x0F032010
{
cout<<temp->data<<"\t"; 50
temp=temp->link; temp 0x0F032011
}
}

Department of Computer Science


Displaying single Linked List
void display()
{ 25, 50,90
node *temp=new node;
temp=head;
while(temp!=NULL) temp 0x0F032011
{
cout<<temp->data<<"\t"; 90
temp=temp->link; temp 0x0F032012
}
}

Department of Computer Science


Displaying single Linked List
void display()
{ 25, 50,90,40
node *temp=new node;
temp=head;
while(temp!=NULL) temp 0x0F032012
{
cout<<temp->data<<"\t"; 40
temp=temp->link; temp 0x0F032013
}
}

Department of Computer Science


Displaying single Linked List
void display()
{ 25, 50,90,40,55
node *temp=new node;
temp=head;
while(temp!=NULL) temp 0x0F032013
{
cout<<temp->data<<"\t"; 55
temp=temp->link; temp NULL
}
}

Department of Computer Science


Displaying single Linked List
void display()
{
node *temp=new node;
temp=head;
while(temp!=NULL) temp NULL
{
cout<<temp->data<<"\t";
temp=temp->link;
}
}

25, 50,90,40,55
Department of Computer Science
Operations on single Linked List

We will now examine linked list operations:


 Insert an item to the linked list
 We have 3 situations to consider:
 insert a node at the front
 insert a node at the end(Already done)
 insert a node at particular position
 Delete an item from the linked list
 We have 3 situations to consider:
 delete the node at the front
 delete an interior node
 delete the last node

Department of Computer Science 4-26


Inserting a Node
 The following steps are involved in inserting an
element into linked list
 Creation of the node
 Before insertion, the node is created. Using new
operator memory space for the node is allocated
 Assignment of data
 Once the node is created, data values are assigned to
members
 Adjusting pointers
 The insertion operation changes the sequence.
 Hence, according to the sequence the address of the
next elements is assigned to the inserted node. The
address of the current node (inserted node) is
assigned to the previous node.
Department of Computer Science 4-27
Inserting a Node at the front

 It is just a 2-step algorithm which is performed


to insert a node at the start of a singly linked
list.
 Created a node called temp
 Connect the newly created node to the first node,
 This can be achieved by putting the address of the head in
the link field of the new node.
 New node should be considered as a head.
 It can be achieved by declaring head equals to a new node.

Department of Computer Science 4-28


Inserting a Node at the front

1. Create a node
node *temp=new node;
temp->data=value;

temp 2. Make the new node point to the first node


(i.e. the node that head points to)

temp->link=head;
3. Make headt point to the new node (i.e the node that node points to)

head=temp;
Department of Computer Science 4-29
Inserting a Node at the front

void insert_start(int value)


{
node *temp=new node; temp
temp->data=value; 85
temp->link=head; 85 100
head=temp;
} head
head 101 100
100 200 300

85 100 50 200 50 300 40

tail 300

Department of Computer Science 4-30


;
Inserting a Node at the end

 The insertion of a new node at the end of linked


list has 2 steps:
 Create a node called temp
 Linking the newly created node with tail node.
 This can be achieved by assigning the address of a
new node to the link filed of a tail node.
 The tail pointer should always point to the last
node.
 This can be achieved by assigning the address of a
new node to tail pointer.

Department of Computer Science 4-31


Inserting a Node at the end

node *temp=new node; 1. Create a node


temp->data=value;
2. Make the new node last node
temp->link=NULL;
temp
tail-> link = temp;

3. Make tail point to the new node (i.e the node that node points to)

Department of Computer Science tail= temp; 4-32


Inserting a Node at Specific

 In this case, the new node is inserted between


two consecutive nodes.
 We will access these nodes by asking the user at
what position (s)he wants to insert the new node.
 We call one node as current and the other as
previous, and the new node is placed between
them.

Department of Computer Science 4-33


Inserting a Node at Specific
head 500

500 600 700 800

85 600 65 700 50 800 40 NULL

node *pre=new node; tail


pre

node *cur=new node; cur

Department of Computer Science 4-34


;
Inserting a Node at Specific

 We initialized our current node by the head

650
head
500

500 600 700 800

85 600 65 700 50 800 40 NULL

cur tail

cur=head;
cur 500
Department of Computer Science 4-35
;
Inserting a Node at Specific

 Now, we will start a loop to reach those specific


nodes.
 We move current node through the linked list
head 650 temp
500

500 600 700 800

85 600 65 700 50 800 40 NULL

for(int i=1;i<pos;i++)
{ tail
pre pre=cur;
cur
} cur=cur->link;

Department of Computer Science 4-36


;
Inserting a Node at Specific

 Now, we will start a loop to reach those specific


nodes.
 We move current node through the linked list
head 500 650 temp

500 600 700 800

85 600 65 700 50 800 40 NULL

for(int i=1;i<pos;i++)
{ tail
pre=cur;
pre cur
} cur=cur->link;

Department of Computer Science 4-37


Inserting a Node at Specific

 Now, we will start a loop to reach those specific


nodes.
 We move current node through the linked list
head 500 650 45 temp

500 600 700 800

85 600 65 700 50 800 40 NULL

temp->data=value; tail
pre cur

Department of Computer Science 4-38


;
Inserting a Node at Specific

 Now the new node can be inserted between the


previous and current node by just performing
two steps:
 Pass the address of the new node in the link field of
the previous node. pre->link=temp;
 Pass the address of the current node in the link field
of the new node. temp->link=cur;
650
temp
head 500 45 700

500 600 700 800

85 600 65 650
700 50 800 40 NULL

Department of Computer Science pre cur 4-39


tail
;
Inserting a Node at Specific
head 500 45 700

500 600 700 800

85 600 65 650
700 50 800 40 NULL

tail

head 500

500 600 650 700 800

85 600 65 650 45 700 50 800 40 NULL

Department of Computer Science


tail
4-40
;
Inserting a Node at Specific

Department of Computer Science 4-41


;
Deleting a node from Linked List

 There are also three cases in which a node can


be deleted:
 Deletion at the start
 Deletion at the end
 Deletion at a particular position

Department of Computer Science 4-42


Deleting a node from start

 The process of deletion includes:


 Declare a temp pointer and pass the address of the
first node, i.e. head to this pointer.
 Declare the second node of the list as head as it will
be the first node of linked list after deletion.
 Delete the temp node.

Step -1 node *temp=new node;


temp=head;

Step -2 head=head->link;
Step -3 delete temp
Department of Computer Science 4-43
;
Deleting a node from end
 In the case find a node that comes before the
last node.
 This can be achieved by traversing the linked list.
 We would make two temporary pointers(previous and
current) and let them move through the whole linked
list.
 At the end, the previous node will point to the second
to the last node and the current node will point to the
last node, i.e. node to be deleted.
 We would delete current node and make the
previous node as the tail.

Department of Computer Science 4-44


;
Deleting a node from end

Department of Computer Science 4-45


;
Deleting a node from specific position

 We ask the user to input the position of the


node to be deleted.
 After that:
 Just move two temporary pointers(previous and
current) through the linked list until we reach our
specific node.
 Established the link between previous and next node.
pass the address of the node that is after current node to

the previous pointer.


 delete current node cur next
pre

Department of Computer Science


;
Deleting a node from specific position

cur next
pre

Department of Computer Science


Analysis of Link List

 As stated earlier, we will be going to analyze


each data structure.
 We will see whether it is useful or not.
 We will see its cost and benefit with respect to time
and memory.
 Let us analyze the link list which we have
created with the dynamic memory allocation in
a chain form.

Department of Computer Science


Insertion operation
Dynamic List(Linked List Static List(Array Based)

Insert at start: Insert at start:


we simply insert the new node after Suppose if we have to insert the
the current node. So ‘add’ is a one- element in the start of the array, all
step operation. the elements to the right one spot are
shifted.
Insert at end: Insert at end:
We insert a new node after the Suppose if we have to insert the
current node in the chain. element in the start of the array, all
the elements to the left one spot are
shifted.
Insert at specific position: Insert at specific position:
For this, we have to change two or if we have to add an element in the
three pointers while changing the centre of the array, the space for it is
values of some pointer variables. created at first. For this, all the
elements that are after the current
pointer in the array, should be shifted
one place to the right.

Department of Computer Science


Insertion operation

Department of Computer Science


Insertion operation

Department of Computer Science


Insertion operation

Department of Computer Science


Deletion operation
Dynamic List(Linked List Static List(Array Based)

Delete from start: Delete from start:


we simply insert the new node after Suppose if we have to insert the
the current node. So ‘add’ is a one- element in the start of the array, all
step operation. the elements to the right one spot are
shifted.
Delete from end: Delete from end:
We insert a new node after the Suppose if we have to insert the
current node in the chain. element in the start of the array, all
the elements to the left one spot are
shifted.
Delete from specific position: Delete from specific position:
For this, we have to change two or if we have to add an element in the
three pointers while changing the center of the array, the space for it is
values of some pointer variables. created at first. For this, all the
elements that are after the current
pointer in the array, should be shifted
one place to the right.

Department of Computer Science


Deletion operation

Department of Computer Science


Deletion operation

Department of Computer Science


Deletion operation

Department of Computer Science


Display Function

Department of Computer Science


CONCLUSION

Department of Computer Science


Array versus Linked List

 Linked lists are more complex to code and manage than


arrays, but they have some distinct advantages.
 Dynamic: a linked list can easily grow and shrink in size.
 We don’t need to know how many nodes will be in the list. They
are created in memory as needed.
 In contrast, the size of a C++ array is fixed at compilation time.
 Easy and fast insertions and deletions
 To insert or delete an element in an array, we need to copy to
temporary variables to make room for new elements or close the
gap caused by deleted elements.
 With a linked list, no need to move other nodes. Only need to reset
some pointers.

Department of Computer Science

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy