Professional Documents
Culture Documents
Singly Linked List
Singly Linked List
Presented By,
M.Sangeetha
Linked list
linear data structure
elements are not stored at contiguous memory
locations
elements in a linked list are linked using pointers
TYPES
DEF:linked list consists of nodes where each node
contains a data field and a reference(link) to the next
node in the list.
Linked list comprise of group or list of nodes in which
each node have link to next node to form a chain
Types of linked list
Singly linked list
Doubly linked list
Circular linked list
Linked List vs Array
Array Linked List
data structure that contains a collection non-primitive data structure contains a
of similar type of data elements collection of unordered linked elements
known as nodes
Accessing an element in an array is fast Accessing an element in an array is bit
slower.
Operations in arrays consume a lot of operations in Linked lists is fast
time
Arrays are of fixed size. Linked lists are dynamic and flexible
and can expand and contract its size.
In array, memory is assigned during Linked list it is allocated during
compile time execution or runtime.
Elements are stored consecutively in Elements are stored randomly in Linked
arrays lists.
requirement of memory is less due to more memory in Linked Lists due to
actual data being stored within the storage of additional next and previous
index in the array referencing elements.
memory utilization is inefficient in the memory utilization is efficient in the
array linked list.
Why Linked List
arrays have the following limitations:
The size of the arrays is fixed
To inserting a new element in an array, existing
elements have to be shifted.
Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks of Array
Random access is not allowed.
Representation of linked list
represented by a pointer to the first node of the linked
list
The first node is called the head.
If the linked list is empty, then the value of the head is
NULL.
Each node in a list consists of at least two parts:
1) data
2) Pointer (Or Reference) to the next node
Linked List Representation
Basic Operations
Insertion − Adds an element at the beginning of the
list.
Deletion − Deletes an element at the beginning of the
list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
CREATE NODE
data link
name
struct node
{
int data;
struct node *next;
}*head,*newNode;
ALGORITHM TO CREATE LIST
Being:
struct node *head;.
alloc (head)
If (head == NULL) then
write ('Unable to allocate memory')
End if
Else then
Head->data =data
Head->next=NULL
End
void createList()
{
struct node *newNode, *temp;
int data, i;
int n;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data; // Link data field with data
head->next = NULL; // Link address field to NULL
}
INSERT NODE AT THE BEGINNING OF SINGLY LINKED LIST