Search

Tuesday, November 3, 2015

Singly Linked List

#include<stdio.h>

typedef struct node
{
    int data;
    struct node *next;
}NODE;

NODE *new_node,*temp,*ptr;
NODE *start=NULL,*last=NULL;


void Insert_At_last();
void Insert_At_Position();
NODE *Create_Node(int valu);
void Delete_At_Position();
void Search();
void Display();
void Reverse();
void Update_Value();
void Node_Count();

void main()
{
    int choice;
    while(1)
    {
        printf("ENTER CHOISE \n 1.INSERT AT LAST \n 2.INSERT AT POSITION \n 3.DISPLAY \n 4.DELETE AT POSITION \n 5.SEARCH \n 6.REVERSE \n 7.UPDATE VALUE \n 8.NODE COUNT \n 9.EXIT ");
        printf("\nEnter Your Choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                Insert_At_Last();
                break;

            case 2:
                Insert_At_Position();
                break;

            case 3:
                Display();
                break;

            case 4:
                Delete_At_Position();
                break;

            case 5:
                Search();
                break;

            case 6:
                Reverse();
                break;

            case 7:
                Update_Value();
                break;

            case 8:
                Node_Count();
                break;

            case 9:
                exit(0);

            default:
                printf("\nWrong Choice\n");
        }
    }
}


NODE *Create_Node(int valu)
{
    new_node=(NODE *)malloc(sizeof(NODE));
    new_node->data=valu;
    new_node->next=NULL;
    return new_node;
}

void Insert_At_Last()
{
    int val;
    printf("\nEnter the data:");
    scanf("%d",&val);
    new_node=Create_Node(val);
    if(start==last && last==NULL)
    {
        start=last=new_node;
    }
    else
    {
        last->next=new_node;
        last=new_node;
    }
}

void Delete_At_Position()
{
    int cnt=0,pos,i;
    printf("Enter the Position:->");
    scanf("%d",&pos);
    if(pos==1)
    {
       if(start==NULL)
        {
           printf("\n LIST IS EMPTY \n");
        }
        else
        {
            start=start->next;
        }
    }
    else
    {
        temp=start;
        while(pos>2)
        {
            temp=temp->next;
            pos--;
        }
        temp->next=temp->next->next;
    }
}

void Insert_At_Position()
{
    int cnt=0,pos,val,i;
    printf("Enter the value:->");
    scanf("%d",&val);
    new_node=Create_Node(val);
    printf("Enter the Position:->");
    scanf("%d",&pos);
    if(pos==1)
    {
       temp=start;
       start=new_node;
       start->next=temp;
    }
    else
    {
        temp=start;
        while(pos>2)
        {
            temp=temp->next;
            pos--;
        }
        new_node->next=temp->next;
        temp->next=new_node;
    }
}

void Search()
{
    int ch,cnt=1,a=0;
    temp=start;
    printf("Enter the data:->");
    scanf("%d",&ch);
    while (temp != NULL)
    {
        if (temp->data == ch)
        {
            printf("Data at position:->%d\n",cnt);
            a++;
        }
        temp = temp->next;
        cnt++;
    }
    if(a==0)
    {
        printf("Data not found\n\n");
    }
    else
        return;
}

void Display()
{
    NODE *temp;
    temp=start;
    while(temp!=NULL)
    {
        printf("\n%d",temp->data);
        temp=temp->next;
    }
}

void Reverse()
{
    int cnt=0,i=0,pos;
    temp=start;
    while(temp!=NULL)
    {
        temp=temp->next;
        cnt++;
    }
    for(i=cnt;i>0;i--)
    {
        pos=0;
        temp=start;
        while(pos!=i-1)
        {
            temp=temp->next;
            pos++;
        }
        printf("%d \n",temp->data);
    }
    //return;
}

void Update_Value()
{
    int Old_val, New_val, flag = 0;
    printf("\n...Updating Node Value...\n");
    if (start == NULL)
    {
        printf("\nNo nodes in the list to update\n");
    }
    else
    {
        printf("\nEnter the value to be updated:->");
        scanf("%d", &Old_val);
        printf("\nEnter the newvalue:->");
        scanf("%d", &New_val);
        for (ptr = start;ptr != NULL;ptr = ptr->next)
        {
            if (ptr->data == Old_val)
            {
                ptr->data = New_val;
                flag = 1;
                break;
            }
        }
        if (flag == 1)
        {
            printf("\nUpdated Successfully");
        }
        else
        {
            printf("\nValue not found in List");
        }
    }
}

void Node_Count()
{
    int cnt=0;
    temp=start;
    while(temp!=NULL)
    {
        temp=temp->next;
        cnt++;
    }
    printf("\nNo.Of Nodes in the linked list:->%d\n\n",cnt);
}

No comments:

Post a Comment