Search

Tuesday, November 3, 2015

Doubly Linked List

#include<stdio.h>
typedef struct node
{
    int data;
    struct node *next;
    struct node *prev;
}NODE;

NODE *start=NULL,*top,*temp;

void *Create_Node(int value);
void Insert_At_Last();
void Display_Start_To_End();
void Display_End_To_Start();
void Insert_At_Position();
void Delete_At_Last();
void Delete_At_Position();
void Search();
void Update_Value();
void Node_Count();


void main()
{
    int choice;
    while(1)
    {
        printf("\n ENTER CHOISE \n 1.INSERT AT LAST \n 2.Delete_At_Last \n                                         3.Insert_At_Position \n 4.Display_Start_To_End \n 5.Display_End_To_Start \n                                   6.Delete_At_Position \n 7.Search \n 8.Update_Value \n 9.Node Count \n 10.Exit \n");
        printf("\nEnter Your Choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                Insert_At_Last();
                break;

            case 2:
                Delete_At_Last();
                break;

            case 3:
                Insert_At_Position();
                break;

            case 4:
                Delete_At_Position();
                break;

            case 5:
                Display_Start_To_End();
                break;

            case 6:
                Display_End_To_Start();
                break;

            case 7:
                Search();
                break;

            case 8:
                Update_Value();
                break;

            case 9:
                Node_Count();
                break;

            case 10:
                exit(0);

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

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

void Insert_At_Last()
{
    int ch;
    NODE *new_node;
    printf("Enter The Data:-> ");
    scanf("%d",&ch);
    new_node=Create_Node(ch);
    if(start==NULL)
    {
        start=new_node;
        top=new_node;
    }
    else
    {
        top->next=new_node;
        new_node->prev=top;
        top=new_node;
    }
}

void Display_Start_To_End()
{
    NODE *temp;
    printf("Displaying List From Start to End:->\n");
    temp=start;
    while(temp!=NULL)
    {
        printf("%d \n",temp->data);
        temp=temp->next;
    }
}

void Display_End_To_Start()
{
    NODE *temp;
    printf("Displaying List From End to Start:->\n");
    temp=top;
    while(temp!=NULL)
    {
        printf("%d \n",temp->data);
        temp=temp->prev;
    }
}

void Insert_At_Position()
{
    NODE *new_node;
    int ch,pos,cnt=1;
    printf("\nEnter the Data:->");
    scanf("%d",&ch);
    new_node=Create_Node(ch);
    printf("\nEnter the position:->");
    scanf("%d",&pos);
    temp=start;
    if(pos==1)
    {
        new_node->next=start;
        start->prev=new_node;
        start=new_node;
    }
    else
    {
        temp=start;
        while(cnt<(pos-1))
        {
            temp=temp->next;
        }
        new_node->next=temp->next;
        temp->next->prev=new_node;
        new_node->prev=temp;
        temp->next=new_node;
    }
}

void Delete_At_Last()
{
    top=top->prev;
    top->next=NULL;
}

void Delete_At_Position()
{
    int pos,cnt=1;
    NODE *temp;
    printf("\nEnter the Position:->");
    scanf("%d",&pos);
    temp=start;
    while(pos<=(cnt-1))
    {
        temp=temp->next;
    }
    temp->next=temp->next->next;
    temp->next->prev=temp;
}

void Search()
{
    NODE *temp;
    int ch,cnt=1;
    printf("\nEnter the data to search:->");
    scanf("%d",&ch);
    temp=start;
    while(temp->next!=NULL)
    {
        if(temp->data==ch)
        {
            printf("\nData is at position:->%d \n",cnt);
        }
        temp=temp->next;
        cnt++;
    }
}

void Update_Value()
{
    NODE *ptr;
    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;
    temp=start;
    while(temp!=NULL)
    {
        temp=temp->next;
        cnt++;
    }
    printf("\nNO.of Nodes In the list is =%d",cnt);
}










No comments:

Post a Comment