Linked List Traverse (Two Way)






#include <stdio.h>
struct node
{
    int data;
    struct node *next;
    struct node *prev;
};
int addnode(int n)
{
    struct node *temp,*current,*head;
    current=(struct node*)malloc(sizeof(struct node));
    head=current;
    while(1)
    {
        if(n!=0)
        {
            current->data=n;
            temp=current;
            current=(struct node*)malloc(sizeof(struct node));
            temp->next=current;
            current->prev=temp;
            printf("Enter Number [0 to terminate]=");
            scanf("%d",&n);
        }
        else
        {

            temp->next=NULL;
            break;
        }
    }
    return head;
}
int print(struct node *head)
{
    struct node *temp;
    temp=head;
    while(temp!=NULL)
    {
        printf("%10d%10d%10d%10d\n",temp->prev,temp,temp->data,temp->next);
        temp=temp->next;
    }

}
int main()
{
    int n;
    struct node *head;
    printf("Enter Number=");
    scanf("%d",&n);
    head=addnode(n);
    print(head);
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment