Linked list is collection of nodes which contain the two parts the data and the address of the next node to which it is connected.
Singular Linked List
In singular linked list the node contains two parts as we have discussed the first one is the data part and the other one is the address of the next node as depicted in figure.

Operation performed in Singular linked list
-
Insertion − add element at beginning .
-
Deletion − delete an element from the beginning of the list.
-
Display − displaying complete list.
-
Search − search an element using given key.
-
Delete − delete an element using given key.
Insertion Operation
In insertion we will create a node first then we will assign data into it then we will point the next location of the node to its second part

//insert link at the first location
void insertFirst(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
Deletion Operation
In deletion operation we will delete the node by removing its data and removing the address reference that it holds.

//delete first item
struct node* deleteFirst(){
//save reference to first link
struct node *tempLink = head;
//mark next to first link as first
head = head->next;
//return the deleted link
return tempLink;
}
0 Comment(s)