Doubly Linklist Implemention program in c by Indranil Bhattacharjee.
Welcome Study Global
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
struct node*prev;
};struct node*next,*ptr,*temp,*newnode;
struct node *head=NULL;
struct node*creatDoublylinklist(){
int ch;
while (1)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the value:");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL){
head=temp=newnode;
}
else{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
printf("If you want to continue enter value press (1)then enter key & doesn't enter value press(0) then enter key");
scanf("%d",&ch);
if(ch==0)
{
break;
}
}
return head;
}
void traverseLinkedList(struct node *head)
{
struct node *ptr = head;
printf("Linkedlist Elements are:");
while(ptr!=NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
void ReversetraverseLinkedList(struct node *head){
struct node *ptr=head;
printf("\nReverse Linklist Elements are:");
ptr->next=newnode;
while(ptr->next!=NULL){
ptr=ptr->next;
}
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->prev;
}
}
int main()
{
struct node *head;
head=creatDoublylinklist();
traverseLinkedList(head);
ReversetraverseLinkedList(head);
return 0;
}
Output Is:-
Comments
Post a Comment