Singly Linked List
A single linked list is one in which all nodes are linked together in sequential manner . It is also called as linear linked list.
The beginning of the linked list is stored in a "START" pointer which points to the first
node. The first node contains a pointer to the second node. The second node contains a
pointer to the third node, ... and so on. The last node in the list has its next field set to
NULL to mark the end of the list. You can access any node in the list by starting at the
start and following the next pointers.
Next part of the last node must be NULL
Operation
In a singly linked list we perform the following basic operations
- Creation
- Insertion
- Deletion
- Traversing
Creating a node for Single Linked List:
Creating a singly linked list starts with creating a node. Depending on node declaration sufficient memory is allocated in the heap section using malloc() function. After
allocating memory for the structure of type node, read data from the user and assign it to newnode data part and assign newnode next part to NULL.
Structure of a Singly linked list
struct LinkedList
{
int data;
struct LinkedList *next;
};
typedef struct LinkedList node;
node *start = NULL;
Creating a node for Singly Linked List
node* createnode()
{
node* newnode;
newnode = (node *) malloc(sizeof(node));
printf("\n Enter the data: ");
scanf("%d", &newnode -> data);
newnode -> next = NULL;
return newnode;
}
Creating a Singly Linked List with 'N' number of nodes
Step 1: Create THE NEW_NODE using createnode()
NEW_NODE = createnode();
Step 2: If the linked list is empthy (START == NULL)
START = NEW_NODE
Step 3: If the linked list is not empthy
PTR = START
while(PTR -> NEXT!=NULL)
{ PTR = PTR -> NEXT }
PTR -> NEXT = NEW_NODE
Step 4 : Repeat Steps 1,2,3 'N' times.
Step 5 : EXIT
0 Comment to "Singly Linked List - Data Structures"
Post a Comment