1. DATA STRUCTURES
Dr. P. Subathra
Professor
Dept. of Information Technology
KAMARAJ College of Engineering & Technology
Madurai
Tamil Nadu
India
subathrakishore@yahoo.com
2. CS8391 – DATA STRUCTURES
ONLINE CLASSES – CLASS NO. 2
13.08.2020
(10:00 AM – 11:00 AM
&
11:30 AM – 12:30 PM)
26. Singly Linked List
• Linked list or list is an ordered collection of
elements.
• Each element in the list is referred as a node.
• Each node contains two fields namely.
– Data field.
– Link field.
(or) Address field
27. Operations on a Singly Linked List
• Creating a new list
• Insertion
- at the beginning
- at the end
- after and element
- before an element
• Deletion
- at the beginning
- at the end
- a given element
- at a position
• Display
• Search Element
• Reverse List
• Sort List
• Find Duplicates
• ………………
28. CREATING A LIST
• Template for a node
struct node
{
int element ;
struct node * next ;
};
• Creating an Empty List
struct node * head = NULL;
Operations on a Singly Linked List
NULL
head
Data field Link field
2 B 4 B
4 B
head
29. CREATING A LIST
• Creating a New Node and Attaching it to the
List
struct node * temp = (node *) malloc (sizeof (struct node));
head = temp;
tempnext = NULL
Operations on a Singly Linked List
NULL
head
Data field Link field
temp1005
30. CREATING A LIST
• Creating a New Node and Attaching it to the
List
struct node * temp = (node *) malloc (sizeof (struct node));
tempnext = NULL
tempdata =222;
head = temp;
Operations on a Singly Linked List
1005
head
Data field Link field
NULL
temp
1005
222
31. INSERTION – FIRST
• Algorithm
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the
element field of the new node.
STEP 3: Copy the address value
available in the Head pointer (also
called as Header) into the next field of
the new node.
STEP 4: Make the Header to pointer to
this new node.
• Case 1
– Insertion into an Empty
List
• Case 2
– Insertion into a Non
Empty List
Operations on a Singly Linked List
32. INSERTION – FIRST
• Algorithm
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the
element field of the new node.
STEP 3: Copy the address value
available in the Head pointer (also
called as Header) into the next field of
the new node.
STEP 4: Make the Header to pointer to
this new node.
Case 1 – Empty List
Operations on a Singly Linked List
33. INSERTION – FIRST
• Algorithm
Case 2 : Non Empty List
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the
element field of the new node.
STEP 3: Copy the address value
available in the Head pointer (also called
as Header) into the next field of the new
node.
STEP 4: Make the Header to pointer to
this new node.
Operations on a Singly Linked List
34. INSERTION – FIRST
Pseudo code
void InsertFirst (int x )
{
creat new node temp,
temp element = x;
tempnext = header; // copy header value into T next
header = temp; // make header to point to new node T
}
Operations on a Singly Linked List
35. INSERTION – FIRST
Code Snippet in C
void InsertFirst (int x )
{
struct node * temp = (node*) malloc(sizeof(struct node*));
temp data = x;
tempnext = head; // copy header value into T next
head = temp; // make header to point to new node T
}
Operations on a Singly Linked List
36. INSERTION – LAST
• Algorithm
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the element
field of the new node and make the next field
to be NULL
STEP 3: If the list is empty insert this new
node as we did for insert first module
ie, make the header to point to this node.
STEP 4: If a non empty list already exist,
traverse the list till the last node is reached.
STEP 5: Make the last node to point to this
new node.
• Case 1
– Insertion into an Empty List
• Case 2
– Insertion into a Non Empty
List
Operations on a Singly Linked List
37. INSERTION – LAST
Algorithm : Case 1 - Empty
List
STEP 1: Dynamically create a new
node.
STEP 2: Read the element to the
element field of the new node and
make the next field to be NULL
STEP 3: If the list is empty insert this
new node as we did for insert first
module
ie, make the header to point to this
node.
Operations on a Singly Linked List
38. INSERTION – LAST
Algorithm :
Case 1 – Non Empty List
STEP 1: Dynamically create a new node.
STEP 2: Read the element to the element
field of the new node and make the next field
to be NULL
STEP 3: If the list is empty insert this new
node as we did for insert first module
ie, make the header to point to this node.
STEP 4: If a non empty list already exist,
traverse the list till the last node is reached.
STEP 5: Make the last node to point to this
new node.
Operations on a Singly Linked List
40. ALGORITHM Insert Last
void InsertLast (int x )
{
create a new node temp
temp data = x;
tempnext = NULL;
if (head== NULL) // List is empty
{
head = temp;
}
else
{ / * List Traversal * /
node * current = head;
while (current next! = NULL) // Traverse to the last node.
current = current next;
current next = temp ; // make last node to point to temp
}
}
Operations on a Singly Linked List