0% found this document useful (0 votes)
52 views27 pages

UNIT 2 Supriya (Single Link List)

The document discusses link lists and their advantages over arrays, including being dynamically sized and easier for insertion and deletion compared to shifting elements in arrays. It covers creating and traversing single linked lists through nodes containing data and a pointer to the next node. Various operations on single linked lists like insertion, deletion, and searching are described through examples of modifying pointers between nodes.

Uploaded by

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

UNIT 2 Supriya (Single Link List)

The document discusses link lists and their advantages over arrays, including being dynamically sized and easier for insertion and deletion compared to shifting elements in arrays. It covers creating and traversing single linked lists through nodes containing data and a pointer to the next node. Various operations on single linked lists like insertion, deletion, and searching are described through examples of modifying pointers between nodes.

Uploaded by

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

UNIT 2: LINK LIST

By,
Supriya Panigrahy
Deptt. of CSE
C. V. Raman Global University
Bhubaneswar
 Array vs Link list
CONTENT
 Introduction to Link list

 Single linked lists

 Operations and implementation

2
Advantages of Array Data Structure
• Array is a data structure where elements are
stored in consecutive memory locations.

• Time to access any element from an array is


constant.

• We can easily compute the address of the array


through index and we can also access the array
elements through index.
3
Disadvantages of Array Data
Structure
• The size of an array has to be defined when the
program is being written and its space is
reserved during compilation of the program.
• During operation in an
insertion nodes array, the inserted
after onethe
shifted positionnode have
to the to
right. be
• Similarly, during deletion, all nodes to the right
of the deleted node have to be shifted one
position to the left.
4
Advantages of Link list
• Dynamic data structure - The size of the linked list may vary
according to the requirements. Linked list does not have a
fixed size.
• Insertion and deletion - Unlike arrays, insertion, and deletion
in linked list is easier. Array elements are stored in the
consecutive location, whereas the elements in the linked list
are stored at a random location. To insert or delete an element
in an array, we have to shift the elements for creating the
space. Whereas, in linked list, instead of shifting, we just have
to update the address of the pointer of the node.
• Memory efficient - The size of a linked list can grow or shrink
according to the requirements, so memory consumption in
linked list is efficient.
• Implementation - We can implement both stacks and queues
using linked list.
Disadvantages of Link list

• Memory usage - In linked list, node occupies more memory than array.
Each node of the linked list occupies two types of variables, i.e., one is a
simple variable, and another one is the pointer variable.

• Traversal - Traversal is not easy in the linked list. If we have to access an


element in the linked list, we cannot access it randomly, while in case of
array we can randomly access it by index. For example, if we want to
access the 3rd node, then we need to traverse all the nodes before it.
So, the time required to access a particular node is large.

• Reverse traversing - Backtracking or reverse traversing is difficult in a


linked list. In a doubly-linked list, it is easier but requires more memory
to store the back pointer.
Array versus Linked Lists
• 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.
– Easy and fast insertions and deletions : With a linked list, no
need to
move other nodes. Only need to reset some pointers.

Array

Linked List

NULL

10 27 -36 16

7
LINKED LIST

12 45 17 NL

Start
• 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
• Start: pointer to the first node
node
• The last node points to NULL
12

data pointer

8
9
• A singly linked list is a
concrete data structure
consisting of a sequence of
next
nodes
• Also called as one-way list
• Each node stores
– element node
elem
– link to the next node

10
SINGLY LINK LIST:

⮚ Linked List can be defined as collection of objects called nodes that are
randomly stored in the memory.

⮚ A node contains two fields i.e., data stored at that particular address and the
pointer which contains the address of the next node in the memory.

⮚ The last node of the list contains pointer to the null.


Self Referential Structure(Node Representation)

• To implement a list in memory,


self referential structure is
used.

• In self referential structure, one


member of this structure is a
pointer that points to the structure
itself.

In the above representation, we have defined a user-defined


structure named a node containing two members, the first one
is data of integer type, and the other one is the pointer (next) of
the node type.
12
⮚ Creation- create a link list.
⮚ Traversal- Visit each node of a link list.
⮚ Insertion - add a new node to the linked list.
⮚ Deletion - removes the existing node in a link list.
⮚ Search - finds a node in the linked list
⮚ Sort - sort the nodes of the linked list

13
Creation of Single Linked List

Step1: Initially the list is empty, No value;


X3 X4 NL
Set Start=Null
Step2: Allocate space to the newly created node,
Set ptr = create node
Step3: Assign address of 1st node to the start variable
Set start=ptr
Step4: Assign the value to information part of the node
Set Info[node]=Value
Step5: Assign null to the address part for indicating end of the list.
Set next[node]=Null
Step6: Exit
Traversal of Single Linked List

Start is holding the address of 1stX3node. X4 NL

Step1: set node=start //Initialize the pointer variable node.


Step2: repeat step 3 & 4
While node!=null / Next[node]!=null
Step 3: process info[node] // each element
Step 4: set node=next[node]
[end of step 2 loop]
Step5: exit
Inserting a new node at the Beginning
• Step 1: If PTR=NULL, then

• Print, overflow
• exit
• else
• PTR=(NODE*) malloc (size of(node))//create new
node from memory and assign its address to PTR.
• End if
• Step2: PTR->info=item

• Step3: Set PTR->next=START

• Step4: Set START = PTR


INSERT AT BEGINNING
Insertion At End
Step 1: If PTR=NULL, then
Print, overflow
exit
else
PTR=(NODE*) malloc (size of(node))//create new
node from memory and assign its address to PTR.
End if
Step2: PTR->info=item
Step3. Set PTR->Next= NULL
Step4.if START=NULL set START=PTR
Step5.Set loc=START
Step6. Repeat step 7 until loc->next!=NULL
Step7. Set loc=loc->next
Step8: set loc->next=PTR
INSERT AT END
Inserting a new node at Any Specified Location
Step 1: If PTR=NULL, then
Print, overflow, exit
else
PTR=(NODE*) malloc (size of(node))//create new node from memory and assign its address to PTR.
End if
Step2: PTR->info=item
Step3: if START=NULL then
set START=PTR
PTR->next=NULL
end if
Step4: Initialize the counter I and pointers (Node* temp)
set I=0
set temp=START
Step5: Repeat step 6 and 7 until I<loc
Step6: Set temp=temp->next
Step7: Set I=I+1
Step8: Set PTR->next=temp->next
Step9: Set temp->next=PTR
20
Inserting a new node at Any Specified Location
OPERATIONS ON SINGLY LINK LIST

Deleting a Node from a Linked List:


How to delete a node from an already existing linked list.
There are three cases:
Case 1: The first node is deleted.
Case 2: The last node is deleted.
Case 3: The node after a given node is deleted.
OPERATIONS ON SINGLY LINK LIST
Case 1: Deleting a First Node from a Linked List
OPERATIONS ON SINGLY LINK LIST
Case 2: Deleting the Last Node from a Linked List
OPERATIONS ON SINGLY LINK LIST
Case 2: Deleting the Last Node from a Linked List
Case 3: Deleting After a Given Node in a Linked List

Suppose we want to delete the node that succeeds the node which contains data value 4 .
OPERATIONS ON SINGLY LINK LIST
Case 3: Deleting After a Given Node in a Linked List

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