C Program For Doubly Linked List - Coding Expert 10

Doubly Linked List : 


#include<stdio.h>
//DOUBLY LINKED LIST
typedef struct node
{
int info;
struct node *prev, *next;
}NODE;
void createlist(NODE *head)
{
int n,i;
NODE *last, *newnode;
printf("How many nodes :");
scanf("%d",&n);

last =head;
for(i=1; i<=n; i++)
{
//create a newnode and fill it
newnode=(NODE *)malloc(sizeof(NODE));
printf("Enter the node value :");
scanf("%d", &newnode->info);
newnode->prev=newnode->next=NULL;
//attach newnode to last
newnode->prev=last;
last->next=newnode;
last=newnode;
}
}
void displaylist(NODE *head)
{
NODE *temp;
printf("\nThe list is :");
for(temp=head->next; temp!=NULL; temp=temp->next)
printf("%d\t", temp->info);
}
void searchlist(NODE *head)
{
NODE *temp;
int num, pos;
printf("\nEnter the element to be searched :");
scanf("%d",&num);

for(pos=1,temp=head->next; temp!=NULL; temp=temp->next, pos++)
if(temp->info==num)
{
printf("Found at position %d", pos);
break;
}
if(temp==NULL)
printf("\nElement Not found");
}
void main()
{
NODE *head;
head = (NODE *)malloc(sizeof(NODE));
head->prev=head->next=NULL;

createlist(head);
displaylist(head);
searchlist(head);
}

Output :


How many nodes :4
Enter the node value :1
Enter the node value :2
Enter the node value :3
Enter the node value :4

The list is :1  2       3       4
Enter the element to be searched :4
Found at position 4
Process returned 19 (0x13)   execution time : 15.515 s
Press any key to continue.

Explaination:
//coming soon

Post a Comment

0 Comments