Thursday 31 August 2017

Write a C program to add a node after a particular position in the Linked List.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node {
    int data;
    struct node * link;
};


void append ( struct node **, int ) ;
void display ( struct node * ) ;
void addafter ( struct node *, int, int ) ;


int main() {
    struct node *p ;
    p=NULL;
    int n,l;
    char ch[10];
    do {
        printf("Enter the value\n");
        scanf("%d",&n);
        append(&p,n);
        printf("Do you want to append another node? Type Yes/No\n");
        scanf("%s",ch);
       }while(!strcmp(ch,"Yes"));
    printf("The elements in the linked list are");
    display(p);
    printf("\n");
    do {
        printf("Enter the position after which you want to add another node\n");
        scanf("%d",&l);
        printf("Enter the value\n");
        scanf("%d",&n);
        addafter(p,l,n);
        printf("The elements in the linked list are");
        display(p);
        printf("\n");
        printf("Do you want to add another node after a certain position? Type Yes/No\n");
        scanf("%s",ch);
    }while(!strcmp(ch,"Yes"));
    return 0;
}

void append ( struct node **q, int num ) {
    struct node *temp;
    struct node *newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=num;
    newnode->link=NULL;
    if(*q==NULL)
    *q=newnode;
    else
    {
        temp=*q;
        while(temp->link!=NULL){
            temp=temp->link;}
            temp->link=newnode;
    }
}

void display ( struct node *q ) {
    struct node *temp;
    temp=q;
    while(temp!=NULL){
        printf(" %d",temp->data);
        temp=temp->link;
    }
}

void addafter ( struct node *q, int loc, int num ) {
    struct node *temp;
    int i;
    struct node *newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=num;
    newnode->link=NULL;
    temp=q;
    for(i=0;i<loc;i++){
        temp=temp->link;
        if(temp==NULL){
            printf("There are less than %d elements in the linked list\n",loc);
        return;}
    }
    newnode->link=temp->link;
    temp->link=newnode;
}

No comments:

Post a Comment