Friday, 22 June 2012

Write menu driven Program using C for implementation of Double Linked List. the menu include.


/*Write menu driven Program using C for implementation
of Double Linked List. the menu include.
-create
-display
-search specific element in the list and display appropiate massage
-exit.
*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
void create();
void display();
void search();
struct node
{
int data;
struct node *addr_prev;
struct node *addr_next;
}*start,*q,*tmp;

void main()
{
int choice;
char ch='y';
start=NULL;
clrscr();
while(ch=='y'||ch=='Y')
{
printf("1 : create double Linked List\n");
printf("2 :display doubly linked list\n");
printf("3 :search the element\n");
printf("4 :exit");
printf("\nenter your choice:-");
scanf("%d",&choice);
switch(choice)
{
case 1:create();
       break;
case 2:display();
       break;
case 3:search();
       break;
case 4:exit(0);
default:printf("\nyou entered wrong choice");
}
printf("\ndo you want to continue(Y|N):-");
scanf("%s",&ch);
}
getch();
}

void create()
{
char cha='y';
while(cha=='y'||cha=='Y')
{
tmp=malloc(sizeof(struct node));
printf("\nenter the element:-");
scanf("%d",&tmp->data);
tmp->addr_next=NULL;
if(start==NULL)
{
tmp->addr_prev=NULL;
start->addr_prev=tmp;
start=tmp;
}
else
{
q=start;
while(q->addr_next!=NULL)
{
q=q->addr_next;
}
q->addr_next=tmp;
tmp->addr_prev=q;
}
printf("\ndo you want to enter element(Y|N):");
scanf("%s",&cha);
}
}
void display()
{
if(start==NULL)
{
printf("List is empty\n");
}
else
{
q=start;
while(q!=NULL)
{
printf("\n%d" ,q->data);
q=q->addr_next;
}
}
}
void search()
{
int num,flag=0;
printf("\nenter number you want to search:-");
scanf("%d",&num);
if(start==NULL)
{
printf("List is empty\n");
}
else
{
q=start;
while(q!=NULL)
{
if(q->data==num)
flag=1;
q=q->addr_next;
}
if(flag==1)
printf("\nnumber is found in the linked list");
else
printf("\nnumber not found");
}
}
/*
1 : create double Linked List
2 :display doubly linked list
3 :search the element
4 :exit
enter your choice:-1

enter the element:-4

do you want to enter element(Y|N):Y

enter the element:-6

do you want to enter element(Y|N):Y

enter the element:-2

do you want to continue(Y|N):-N

do you want to continue(Y|N):-y
1 : create double Linked List
2 :display doubly linked list
3 :search the element
4 :exit
enter your choice:-2

4
6
2
do you want to continue(Y|N):-y
1 : create double Linked List
2 :display doubly linked list
3 :search the element                                                          
4 :exit                                                                        
enter your choice:-3                                                          

enter number you want to search:-4                                            
                                                                               
number is found in the linked list                                            
do you want to continue(Y|N):-y                                                
1 : create double Linked List                                                  
2 :display doubly linked list                                                  
3 :search the element                                                          
4 :exit                                                                        
enter your choice:-3                                                          
                                                                               
enter number you want to search:-1                                            
                                                                               
number not found                                                              
do you want to continue(Y|N):-y                                                
1 : create double Linked List                                                  
2 :display doubly linked list                                                  
3 :search the element                                                          
4 :exit                                                                        
enter your choice:-5                                                          

you entered wrong choice                                                      
do you want to continue(Y|N):-y                                                
1 : create double Linked List                                                  
2 :display doubly linked list                                                  
3 :search the element                                                          
4 :exit                                                                        
enter your choice:-6

you entered wrong choice
do you want to continue(Y|N):-n

*/
/*
1 : create double Linked List
2 :display doubly linked list                                                  
3 :search the element                                                          
4 :exit                                                                        
enter your choice:-3                                                          
                                                                               
enter number you want to search:-2
List is empty                                                                  
                                                                               
do you want to continue(Y|N):-y                                                
1 : create double Linked List                                                  
2 :display doubly linked list                                                  
3 :search the element                                                          
4 :exit                                                                        
enter your choice:-2                                                          
List is empty                                                                  
                                                                               
do you want to continue(Y|N):-n
*/

No comments: