silentblogg
Learn & Teach
6 posts
Exchanging information is the way to grow  and developed the world that's why First Learn & then Teach 
Don't wanna be here? Send us removal request.
silentblogg · 3 years ago
Text
Insertion in linked list
Insertion at First node:
To insert at first node we have to make new node , that node points towards head node and it become head note .
Algorithm:
1.Declare a PTR node .
2.Point the PTR node towards head node.
3.Head node become second node.
4.Traverse all nodes.
5.EXIT.
Source code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void trv(struct node *ptr)
{
while (ptr != NULL)
{
printf(" Element is : %d\n", ptr->data);
ptr = ptr->next;
}
}
struct node *insertionatfirst(struct node *head, int data)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
ptr->next = head;
ptr->data = data;
return ptr;
}
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = NULL;
trv(head);
head = insertionatfirst(head, 0);
trv(head);
return 0;
}
2.To insert an index of node
To insert after an index of a node ,we have to declare or decide the node . Create a 'p' pointer which points toward head node and then run the loop until we reach the just before the index.
Algorithm is :
1. Declare a index , p pointer and ptr pointer
2.Run a loop of p pointer until we reach previous index of index.
3.Set ptr->next = p->next and p->next = ptr
4.Traverse all the nodes .
5.EXIT
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void trv(struct node *ptr)
{
while (ptr != NULL)
{
printf(" Element is : %d\n", ptr->data);
ptr = ptr->next;
}
}
struct node * insertionatindex(struct node *head, int data,int index)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
struct node * p = head;
int i=0;
while (i != index-1)
{
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
return head;
}
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = NULL;
trv(head);
printf("\n\n\n");
head = insertionatindex(head, 56,3);
trv(head);
return 0;
}
3.Insertion at end of a node:
Algorithm:
1.Create a node with given value
2.Define a node pointer and initialize with head
3.keep moving until it reaches to the last node in the list.(*p!=NULL)
4.Set p->next = ptr and ptr->next = NULL.
Traverse the list.
EXIT.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
};
void traversal(struct node * ptr){
while (ptr != NULL)
{
printf("The element is : %d \n",ptr->data);
ptr = ptr->next;
}
}
struct node * insertatend(struct node * head, int data){
struct node*ptr=(struct node *)malloc(sizeof(struct node *));
ptr->data=data;
struct node * p = head;
// int i = 0;
while (p->next!=NULL)
{
p = p->next;
}
p->next = ptr;
ptr->next = NULL;
return head;
}
int main()
{
struct node * head;
struct node * second;
struct node * third;
struct node * fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next =second;
second ->data = 2;
second ->next = third;
third ->data=3;
third ->next = fourth;
fourth ->data=4;
fourth ->next=NULL;
printf("Before insertion\n");
traversal(head);
head = insertatend(head,56);
printf("After insertion\n");
traversal(head);
return 0;
}
4.Insertion after a node:
Algorithm:
1.Create a new node 'previousnode' that is value of node.
2.Set ptr->next = previousnode->next & previousnode->next=ptr.
3.traverse all the nodes and print.
4.EXIT.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void trv(struct node *ptr)
{
while (ptr != NULL)
{
printf(" Element is : %d\n", ptr->data);
ptr = ptr->next;
}
}
struct node *insertionafteranode(struct node *head,struct node * previousnode ,int data)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
ptr->data = data;
ptr->next = previousnode->next ;
previousnode->next = ptr;
return head;
}
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = NULL;
printf("Before Insertion :\n");
trv(head);
head = insertionafteranode(head, second ,60);
printf("After insertion :\n");
trv(head);
return 0;
}
0 notes
silentblogg · 3 years ago
Text
There is nothing impossible to they who will try.
1 note · View note
silentblogg · 3 years ago
Text
Traversal of linked list
First we have to declare head after that we have to find the next pointer which points to the next node. We will print the data of each node until next pointer shows NULL.
Tumblr media
Node declaration:
struct node {
Int data;
struct node * next;
};
Node declaration:
Struct node * head;
Struct node * second;
Struct node * third;
Memory allocation using malloc :
head= (struct node*)malloc(sizeof(struct node *));
Second = (struct node*)malloc(sizeof(struct node *));
Third = (struct node*)malloc(sizeof(struct node *));
data & pointer declaration: head->data=1;
Head->next = second;
second->data=2;
second->next = third;
third->data=3;
fourth->next = NULL;
Traversal Function:
Void(struct node * ptr){
While(ptr!=NULL){
Printf("Element is :%d",ptr->data);
ptr=ptr->next;
}
1 note · View note
silentblogg · 3 years ago
Text
Linked List
A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link called First.
4 notes · View notes
silentblogg · 3 years ago
Text
How to Code C++ in Smartphone
Today our smartphones are so powerful that we can even code programs in smartphone. There are many apps available on play store to code in C++ .
1. cxxdroid
2. Dcoder
3. MobileC
You should download compatible keyboard for coding it will make
your coding easy. There are bunch of app for coding keyboard
Coding keyboard for programmers
if you want a pc experience bring a keyboard , phone-stand , otg , usb hub and a mouse. It will enhance your experience coding in phone
1 note · View note
silentblogg · 3 years ago
Text
If you can not conquer yourself ;
you can not conquer the world
1 note · View note