what is Singly linked list ?
A singly linked list is data structure that is a head or tail and length property.
Linked list consist of something called node. A node is piece of data that has a values as well as a pointer to another node or null. The head of a linked list is a very first node and the tail is the very last node which will always points to null.
A Singly linked list is a data structure that consist of nodes. Each node has a value and a pointer that points to the next node or null once it gets to the end.
The Difference Between singly linked list and Doubly linked list it as follows:
In singly linked list each node only points to the next node in line.
In Doubly linked list each node points to previous node and next node both.
Linked List do not have built in Indexes.
WHY/WHEN COULD WE USE A SINGLY LINKED LIST ?
There are a few different times linked list shine above arrays and vice-versa. It depends on what exactly you will be doing with the data.
Inserting and Deleting of the data at the beginning of the list :
Remember that array come with built in indexes. This means that if an array has a thousand items in it and we need to place an item at very beginning of an array. Every single index in a entire array needs to be shifted to the right and incremented.
Function for inserting node at the beginning:
void insert_beg(node *head, int num )
{
node *newnode;
newnode = (node *)malloc(sizeof(node));
newnode->info = num;
newnode->next = head->next; //attach newnode to the node after head
head->next = newnode;
}
Function for inserting node at the last:
To insert the node at the end of the list we have to attach it to the last node A pointer temp should be moved from the first node to the last node using a loop The last node is the node having the its next pointer NULL.
void insert_end(node *head ,int num)
{
node * newnode, temp= head;
while(temp->next! = NULL)
temp = temp->next;
newnode = (node * )malloc(sizeof(node));
newnode->info = num;
newnode->next = NULL;
temp->next = newnode;
}
Delete the node at the beginning of the linked list :
To delete the first node we have to delete the node after the header node. Thus header node is linked to the node after the first node.
Function for to delete the first node :
Void deletefir(node * head)
{
node * temp;
temp = head ->next;
head->next = temp->next;
free(temp);
}
Delete the node at the last in the linked list :
To delete the last node temp must be moved to the end of the lost However we need a pointer to the second last node so that its pointer can be made NULL.
Function for to delete the last node in linked list :
Void deletelast(node *head)
{
node *temp, temp1;
for(temp = head; temp->next!= NULL; temp-temp->next)
temp1 = temp;
temp1->next = NULL;
free(temp);
}
Functions for to insert and delete the node at the fixed position in the linked list:
Insert node at the specific position in linked list :
To insert node at any specific position pos, the pointer temp be moved to the node at the position pos-1. The new node must be inserted between nodes temp and temp->next. If the position is beyond range the node will not be inserted.
The steps are :
1. Move temp pos-1 times from head .
2. check if position is out of range.
3. Insert newnode between temp and temp->next.
Function for inserting node at specific position :
Void insert(node *head ,int num, int pos)
{
node * newnode, temp;
int i;
for(temp = head, i=1; (temp!= NULL) && (i <= pos-1); i++)
temp = temp->next;
if(temp= NULL)
{
printf("position is out of range:");
return;
}
newnode= (node * )malloc(sizeof(node));
newnode-> info = num;
newnode- >next = temp- >next;
temp->next = newnode;
}
Delete Node at specific position:
To delete a node at a specific position pos, temp must points to nose at pos-1 .
Function for to delete the node at from specific position :
Void delete_pos(node * head, innt pos)
{
node * temp, temp1;
int i;
for(temp = head ,i= 1; (temp->next!=NULL) && (i<=pos-1);i++)
temp = temp->next;
if(temp->next = NULL)
{
printf("position is out of range :");
return;
}
temp1 = temp->next;
temp- >next = temp1 ->next;
free(temp1);
}
Create function :
To cerate a singly linked list first we create a first node (head) and add node one by one to end of the list.
The Header node as the pointer called head.
Algorithm for create function:
step 1. start
step2. create a first node point it by head.
step 3. Accept n i.e number of node to be created.
step 4. counter =1.
step 5 last is pointer pointing to the header node i .e. last = head .
step 6. Repeat step 7-10 while counter <=n
step 7. Create a new node and store address in new node
step 8. Attached last node to the first.
step 9 . Move last to the first node.
step 10. Increment counter.
step 11. stop.
Function for to Create a singly linked list.
Void create_list(Node *head )
{
int n, count;
Node *last, *newnode;
printf("How many nodes ");
scanf("%d",&n);
last = head';
for(count= 1; count<= n; count++)
{
newnode = (Node *)malloc(sizeof(node));
newnode->next = NULL;
printf("enter node data :");
scanf("%d",&newnode->info);
last->next = newnode;
last = newnode;
}
}
0 comments:
Post a Comment