Single Linked List

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Single Linked List

Terminology: Head, Node, Data, Next, NULL


Operations: create(), insertion( ), deletion( ), Display(), Search( )
SLL
Content
• What is Single linked list
• What is node?
• Why are we using Single linked list instead of
stack and Queue.
• Node Structure.
• Connecting nodes by address
• Operations on SLL.
What is Single linked list

• List: collection of number of elements


• SLL: SLL is linear Data Structure.
• It is also a collection of elements(nodes) but
every element is linked with next
element(node) by address.
Example picture
What is node?

• Every single element in a List is called “Node”.


• Node contains two fields
1) Data filed-it holds data(element value)
2) Next field- it holds address of next node
• Every node has it’s own address value in the
memory
Data Next

Node
100
Why are we using Single linked list instead of stack and Queue.

• Stack and Queue are linear DS, those are


having limited elements (static size).
• But Linked Lists are having unlimited elements
(Dynamic).
• Insertion at middle is not possible in Queue.
• But it is possible in stack, it takes more
operations to perform
Node Structure
Structure Node
{ Data Next
Int data;
Structure Node *Next; Node
}*head=NULL;
100
Connecting nodes by address
1)Before creating first node :: Assign Head=NULL

Data Next

10 NULL
Node1=(*struct Node)malloc(sizeof(*struct Node);

Node1->data=10;
Node1->next=NULL;
100
Node 1 If(head==NULL)
i.e: {
100->data=10; Head=node1;
100->next=NULL; }

( first node of list is called “Head” in SLL.


After creating first node Head=first node address
i.e Head=100

Head=100

Data Next

10 NULL

100
Node 1
Head=100

Data Next Data Next


Node2=(*struct Node)malloc(sizeof(*struct Node);

10 NULL 20 NULL Node2->data=20;


Node2->next=NULL;

100 200 If(head==NULL)


{
Node 1 Node 2 Head=node1;
}
Head=100

Data Next Data Next

10 NULL 20 NULL
200 temp=head;

While(temp->next!=NULL)
{
100 200 temp=temp->next;
Node 1 Node 2 }
Temp->next=node2;
Head=100 Node3=(*struct Node)malloc(sizeof(*struct Node);

Data Next Node3->data=30;


Data Next Data Next Node3->next=NULL;

10 200 20 NULL 30 NULL


If(head==NULL)
{
100 200 300
Head=node1;
}
Node 1 Node 2 Node 3
Head=100

Data Next Data Next


Data Next

10 200 20 NULL 30 NULL


300 temp=head;

While(temp->next!=NULL)
100 200 300
{
temp=temp->next;
Node 1 Node 2 Node 3 }
Temp->next=node3;
Head=100

Data Next Data Next Data Next Data Next

10 200 20 300 30 NULL 40 NULL

100 200 300 400


Node 1 Node 2 Node 3 Node 4
Head=100

Data Next Data Next Data Next Data Next

10 200 20 300 30 400 40 NULL

100 200 300 400


Node 1 Node 2 Node 3 Node 4
Head=100

Data Next Data Next Data Next Data Next Data Next

10 200 20 300 30 400 40 NULL 50 NULL

100 200 300 400 500


Node 1 Node 2 Node 3 Node 4 Node 5
Head=100

Data Next Data Next Data Next Data Next Data Next

10 200 20 300 30 400 40 500 50 NULL

100 200 300 400 500


Node 1 Node 2 Node 3 Node 4 Node 5
Operations on SLL.

• Create(): It is used to create the node.


• Insertion(): it is used to insert the node at
1. start
2. Middle
3. End

• Deletion(): it is used to delete the node at


1. start
2. Middle
3. End
Operations on SLL
• Display(): it is used to display the nodes in the
SLL
• Search(): it used to search particular node.
Inserting node at start

100

200 300 400

100 200 300 400


Inserting node at start

500

200 300 400

100 200 300 400

100

500
newn
Inserting node at End

100

200 300 400

100 200 300 400


Inserting node at End

100

200 300 400 500

100 200 300 400

500
Inserting node at Middle

100

200 300 400

100 200 300 400


Inserting node at Middle

200 500 400

100 200 300 400


300

500
newn
Delete node at start
Delete node at middle
Delete node at End
Display
Search
• Search a particular node in the list.
Thank You

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