You are on page 1of 28

Singly Linked List

Dr. Beulah Christudas,


Karunya Institute of Technology and Sciences
Linked List
✔Linked list is a very flexible, dynamic data structure in which the
elements called nodes form a sequential list.
✔Unlike arrays, linked lists do not have a fixed size. They can grow,
whenever they need to accommodate more elements. Each node contains
the following types of data
1. A value that is stored in that node
2. A pointer or link to the next node in the list. The pointer actually contains the
address of the next element.

5 2 7
Common Operations

➔ All operations start with the ‘start’ node or head node. We need the start
node for any operation.
◆ start node is a parameter in all operations.
◆ Except for display operation, the start node will be returned back from the
function. [return type : node *]
➔ To check whether the list is empty or not.
◆ if (start == NULL)
➔ To create a new node (node1)
◆ Used in all insertions operation and createNew operation.
● node *node1 = new node();
➔ To assign ‘n’ to node1
◆ Used in all insertion operations and createNew operation.
● node1 -> data = n;
➔ Creating a pointer to the node
◆ node *ptr, *preptr;
Common Operations
➔ Pointing the pointer to the start node
◆ ptr=start;
◆ preptr=start;
◆ node *ptr=start; //Initialization
➔ Moving the pointer to the next node
◆ ptr = ptr->next;
➔ Checking whether the pointer has reached the last node.
◆ while (ptr->next != NULL)
◆ while (ptr!=NULL)
● //display operation, sort operation, delete any, delete all
➔ preptr following ptr
◆ preptr = ptr;
➔ Linking ptr to another node
◆ Ex. ptr->next = node1;
➔ Making a node as the last node
◆ Ex. node1->next = NULL;
Node
✔A node is defined using a structure.
struct node
{
int data;
node* next;
};
Create New List
✔Case 1: start == NULL
node1
n NULL

start

✔Case 2: Adding the last node


node1
10 12 15 n NULL
start
ptr ………………………………………..ptr………………………………………....ptr
Create New List : Algorithm

Algorithm Pseudocode
node *node1 = new node();
➔ Create a new node. node1->data = n;
➔ Assign ‘n’ to the data part. node1->next=NULL;
➔ Assign NULL to the next part. //Case 1 (if start == NULL)
➔ Case 1: creating node1 as the first node. start = node1;
◆ Make node1 as the start node //Case 2 (else)
➔ Case 2: Adding node1 as the last node. node *ptr=start;
➔ Point ptr to the start node. while (ptr->next != NULL)
➔ Move ptr till it reaches the last node. ptr=ptr->next;
➔ Link node1 to ptr ptr->next = node1;
➔ Return the start node return start;
Source Code: createNew
node* createNew(node *start,int n) else
{ {
node *node1, *ptr; ptr=start;
node1=new node(); while (ptr->next!=NULL)
node1->data=n; ptr=ptr->next;
if (start==NULL) ptr->next=node1;
{ }
start=node1; node1->next=NULL;
} return start;
}
Display : Algorithm
Algorithm Pseudocode
node *ptr;
➔ Create a pointer ptr.
➔ Point ptr to the start node. ptr=start;
➔ Move ptr till it reaches the last while (ptr!=NULL)
node. {
cout<<ptr->data<<'\t';
➔ Display the data of the current node.
➔ Move ptr to the next node. ptr=ptr->next;
}
Display
void display(node *start)
{ node *ptr;
ptr=start;
while (ptr!=NULL)
{ cout<<ptr->data<<'\t';
ptr=ptr->next;
} }
Insert at the Beginning
node1
n 10 12 15

start start

node* insertBeg(node *start, int n) Algorithm


➔ Create a new node. (node1)
{ ➔ Assign n to the data.
➔ Attach node1 to the start node.
node *node1=new node();
➔ Make node1 as the start node.
node1->data=n;

node1->next=start;

start=node1;

return start; }
Insert at the End
node1
10 12 15 n NULL
start
ptr ……………………………………...ptr………………………………………...ptr

Algorithm
➔ Create a new node. (node1)
➔ Assign n to the data.
➔ Assign NULL to the next pointer.
➔ Point a pointer (ptr) to the start node.
➔ Move ptr to the last node.
➔ Attach ptr to node1.
Insert at the End [code]
node1
10 12 15 n NULL
start
ptr …………………………………...ptr………………………………………...ptr
node* insertEnd(node *start, int n) while(ptr->next != NULL)
{ ptr=ptr->next;
node *node1=new node();
ptr->next=node1;
node *ptr;
return start;
node1->data=n;
}
node1->next=NULL;
ptr=start;
Insert Before

node1
n
start
10 12 15 25 NULL

ptr
preptr

Assume val = 15
i.e we should insert before 15
ptr (blue pointer) moves till val
preptr (pink pointer) follows ptr
*Dotted line (for pink and blue) indicates the pointer is moved from that node
Insert Before (Algorithm)

➔ Create a new node (node1)


➔ Assign n to the data.
➔ Create 2 pointers ptr, preptr and point them to the start node.
➔ Move ptr till ptr reaches the value (val) before which the new node is to
be inserted.
➔ Make preptr follow ptr.
➔ Link preptr to node1
➔ Link node1 to ptr.
node* insertBefore(node *start, int val, int n) while(ptr->data != val)

{ {
preptr=ptr;
node *node1=new node();
ptr=ptr->next;
node *ptr, *preptr;
}
node1->data=n;
preptr->next=node1;
ptr=start; node1->next=ptr;
return start;
}
Insert After
node1
n

start
10 12 15 25 NULL

ptr
preptr

Assume val = 15
i.e we should insert after 15
preptr (pink pointer) moves till val
preptr (pink pointer) follows ptr (blue pointer)
*Dotted line (for pink and blue) indicates the pointer is moved from that node
Insert After (Algorithm)

➔ Create a new node (node1)


➔ Assign n to the data.
➔ Create 2 pointers ptr, preptr and point them to the start node.
➔ Move preptr till preptr reaches the value (val) after which the new
node is to be inserted.
➔ Make preptr follow ptr.
➔ Link preptr to node1
➔ Link node1 to ptr.
node* insertAfter(node *start, int val, while(preptr->data != val)
int n)
{
{
preptr=ptr;
node *node1=new node(); ptr=ptr->next;
node *ptr, *preptr; }
node1->data=n; preptr->next=node1;
ptr=start; node1->next=ptr;
preptr=ptr; return start;
}
Delete First Node

start …………………………. start


10 12 15 25 NULL

ptr
node* delBeg(node* start)

{ Algorithm
node *ptr = start; ➔ Point a pointer ptr to the start
start=start->next; node.
delete ptr; ➔ Make the next node as the start
return start;
node.
➔ Delete ptr.
}
Delete Last Node
start
10 12 15 NULL 25 NULL

ptr
preptr

node* delEnd(node* start) preptr->next=NULL;


{ delete ptr;
node *ptr, *preptr; return start;
ptr=start; }
while (ptr->next!=NULL)
{ preptr=ptr;
ptr=ptr->next; }
Delete Any Node (val)
✔Deleting first node
start......................................start
10 12 15 25 NULL

✔Deleting last node


start
10 12 15 NULL 25 NULL

ptr
preptr

✔Deleting in Between (val=15)


start
10 12 15 25
ptr
preptr
node* delNode(node *start, int val) if (ptr->next==NULL)
{ node *ptr, *preptr; preptr->next=NULL;
ptr=start; else
if (start->data==val) preptr->next=ptr->next;
{ start=start->next; delete ptr;
delete ptr; }
else
}
{ while (ptr->data!=val && ptr!=NULL)
{
return start;
preptr=ptr;
}
ptr=ptr->next;
}
Delete All Nodes
start
10 12 15 25 NULL

node* delList(node *start) start=start->next;


{ delete ptr;
node *ptr; ptr=start;
ptr=start; }
while (ptr!=NULL) return start;
{ }
cout<<"\nDeleting "<<ptr->data<<endl;
Ordered and Unordered Linked Lists
✔A singly linked list in which values are inserted in either ascending or
descending order is called an ordered singly linked list.
✔In an unordered singly linked list the elements can be in any order.
Sorting : Comparison with array
//Sorting an array //Sorting a SLL
for (i=0;i<n-1;i++) for (ptri=start; ptri->next!=NULL; ptri=ptri->next)
for for (ptrj=ptri->next; ptrj!=NULL; ptrj=ptrj->next)
(j=i+1;j<n;j++)
if (ptri->data >ptrj->data)
if (a[i]>a[j])
{
{
t=ptri->data;
t = a[i];
ptri->data = ptrj->data;
a[i] =
a[j]; ptrj->data = t;

a[j] = t; }

}
Sorting a Singly Linked List
node *sort(node *start) if (i->data>j->data)
{ {
node *i, *j; t=i->data;
int t; i->data=j->data;
for (i=start;i->next != NULL; i=i->next) j->data=t;
for (j=i->next;j!=NULL;j=j->next) }
return start;
}
Thank you!

You might also like