Sunday, 4 October 2015

Write A Program to Implement functionalities of a linklist in detail?


#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct node {
    int data;
    struct node *link;
};
int search(struct node *head , int data);
struct node *insertion(struct node *head,int data);
struct node *insertatbegin(struct node *head,int data);
struct node* deletenode(struct node *head,int data);
struct node *insertatend(struct node *head,int data);
struct node *insertinmiddle(struct node *head , int data1,int data2);
void Print(struct node *head);

int length(struct node *head);
int getlengthR(struct node *head);
struct node *swapnodes(struct node *head,int x,int y);
int nthnode(struct node *head,int n);
int middle(struct node *head);
int nthnodefromlast(struct node * head,int n);

int main(){

//implement main function in your way. main function is written only for testing purpose
//let me know about any discrepency
    struct node *head=NULL;
    struct node *curr=NULL;
    int i=0;
    int data=0,data2=0;
    for(i=0;i<5;i++){
        scanf("%d",&data);
        head=insertion(head,data);
    }
    for(i=0;i<2;i++){
        printf("Enter a number to search\n");
        scanf("%d",&data);
        if(search(head,data)==1)
            printf("Element Found\n");
        else
            printf("Element Not Found\n");   
    }
   
    Print(head);
    scanf("%d",&data);
    head=deletenode(head,data);
    scanf("%d",&data);
    head=insertatbegin(head,data);
    Print(head);
    scanf("%d",&data);
    scanf("%d",&data2);
    head=insertinmiddle(head,data,data2);
    Print(head);
    scanf("%d",&data);
    head=insertatend(head,data);
    Print(head);

    printf("\n\n");
    int len = getlengthR(head);
    printf("Linklist length = %d",len);
    printf("\n\n");
    printf("Enter two numbers in the linklist to swap the nodesn\n");
    scanf("%d %d",&data,&data2);
    swapnodes(head,data,data2);
    Print(head);
    printf("\n\n");
    printf("\nEnter n to get the required number less than %d\n",length(head));
    scanf("%d",&data);
    data=nthnode(head,data);
    printf("Nth node is %d",data);
    printf("\n\n");
    int mid=middle(head);
    printf("middle element in the linklist = %d\n",mid);
    printf("\n\n");
    printf("Enter number to get nth node from last\n");
    scanf("%d",&data);
    int nthnodelast=nthnodefromlast(head,data);
    printf("nth node from last in the linklist = %d\n\n",nthnodelast);

   
    return 0;
}

void Print(struct node *head){
    struct node *curr=head;
    while(curr!=NULL){
        printf("%d\t",curr->data);
        curr=curr->link;
    }
}

struct node *insertion(struct node *head,int data){
    struct node *curr=head;
    struct node *temp=NULL;
    temp=(struct node*)malloc(sizeof(struct node));
    temp->data = data;
    temp->link =NULL;
    if(head==NULL){
        head=temp;
    }
    else{
        while(curr->link!=NULL){
            curr=curr->link;
        }
        curr->link=temp;
    }
    //free(temp);
    return head;
}

int search(struct node *head , int data){
    struct node *curr=head;
    while(curr!=NULL){
        if(curr->data==data){
            return 1;
        }
        curr=curr->link;
    }
    return 0;
}

struct node *insertatbegin(struct node *head,int data){
    struct node *curr=NULL;
    curr=(struct node *)malloc(sizeof(struct node));
    curr->data=data;
    curr->link=head;
    head=curr;
    return head;
}

struct node *insertinmiddle(struct node *head , int data1,int data2){
    struct node *temp=NULL;
    struct node *curr=head;
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data=data2;
    temp->link=NULL;
    while(curr->data!=data1){
        curr=curr->link;
    }
    temp->link=curr->link;
    curr->link=temp;
    return head;
}

struct node *insertatend(struct node *head,int data){
    struct node *temp=NULL;
    struct node *curr=head;
    temp = (struct node *)malloc(sizeof(struct node));
    temp->data=data;
    temp->link=NULL;
    while(curr->link!=NULL){
        curr=curr->link;
    }
    temp->link=curr->link;
    curr->link=temp;
    return head;
}

struct node* deletenode(struct node *head,int data){
    struct node *curr=head;
    struct node *temp=NULL;
    if(head->data=data){
        temp=head;
        head=head->link;
        free(temp);
    }
    else{
        while(curr->data!=data){
            curr=curr->link;
        }
    }
    return head;
}



int length(struct node *head){
        int len=0;
        struct node *curr=head;
        while(curr!=NULL){
                curr=curr->link;
                ++len;
        }
        return len;
}

int getlengthR(struct node *head){
        int len=0;
        if(head==NULL){
                return len;
        }
        else{
                return 1+getlengthR(head->link);
        }
}

struct node *swapnodes(struct node *head,int x ,int y){
        struct node *prevX = NULL ,*currX=NULL, *prevY=NULL, *currY=NULL;
        if(x==y)
                return head;
        while(currX && currX->data!=x){
                prevX=currX;
                currX=currX->link;
        }

        while(currY && currY->data != y){
                prevY=currY;
                currY=currY->link;
        }

        if(currX==NULL || currY==NULL){
                return head;
        }
        if(prevX!=NULL)
                prevX->link=currY;
        else
                head = currY;

        if(prevY!=NULL)
                prevY->link=currX;
        else
                head = currX;
        return head;
}

int nthnode(struct node *head ,int n){
        struct node *curr=head;
        if(n>length(head)){
                printf("Enter a valid number less than %d\n",length(head));
                scanf("%d",&n);
        }
        int count=0;
        while(count!=n-1){
                count++;
                curr = curr->link;
        }
        return curr->data;
}



int middle(struct node *head){
        struct node *curr=head;
        struct node *next=head;

        while(!(next==NULL || next->link==NULL)){
                curr=curr->link;
                next = next->link->link;
        }
        return curr->data;
}

int nthnodefromlast(struct node * head,int n){
        int nfromstart = length(head)-n+1;
        int data = nthnode(head,nfromstart);
        return data;
}

No comments:

Post a Comment