Algorithm
INPUT: HEADER IS A POINTER TO THE HEADER NODE OF THE LINKEDLIST AND X IS THE ELEMENT TO BE INSERTED
OUTPUT: LINKEDLIST WITH THE ADDED ELEMENT
DATA STRUCTURE: SINGLY LINKEDLIST
STEP 1: START
STEP 2: NEW=GETNODE(NODE)
STEP 3: IF(NEW=NULL)THEN
STEP 4: PRINT("MEMORY INSUFFICIENT")
STEP 5: EXIT
STEP 6: ELSE
STEP 7: PTR=HEADER->LINK
STEP 8: FOR I=1 TO POS-1 DO
STEP 9: PTR=PTR->LINK
STEP 10:NEW->DATA=X
STEP 11: NEW->LINK=PTR->LINK
STEP 12: PTR->LINK=NEW
STEP 13: ENDIF
STEP 14: STOP
Code
#include <stdio.h>
#include <stdlib.h>
int getnode(int);void display();
void newnode();int x,pos,n;struct node{int data;
struct node *link;};
struct node *prev,*head,*news,*p,*ptr;
struct node *header;
int main(){
printf("ENTER THE LIMIT");
scanf("%d",&n);
getnode(n);
return 0;}
void display(struct node *head){
struct node *ptr;
ptr=header->link;
while(ptr!=NULL){
printf("%d ",ptr->data);
ptr=ptr->link;}}
void newnode(){
news=NULL;
news=malloc(sizeof(struct node));
news->data=x;
if(news==NULL) {
printf("memory Insufficient"); }
else {
ptr=head;
for(int i=1;i<pos-1;i++) {
ptr=ptr->link; }
news->link=ptr->link;
ptr->link=news; }}
int getnode(int n){
header=malloc(sizeof(struct node));
head=NULL;
for(int i=0;i<n;i++){
p=malloc(sizeof(struct node));
scanf("%d",&p->data);
p->link=NULL;
if(head==NULL)
head=p;
else
prev->link=p;
prev=p;
}header->link=head;
display(header);
printf("Enter the number to be inserted at the position and the postion were you want to insert");
scanf("%d",&x);
scanf("%d",&pos);
newnode();
display(header);
return 0;}
Output
user@computer$ : ENTER THE LIMIT5
1
2
3
4
5
1 2 3 4 5 Enter the number to be inserted at the position and the postion were you want to insert7
3
1 2 7 3 4 5