/*write a menu driven program using c for signly linked list following
to perform operation
-to create list
-to display list
-to search a node in linked list
-to count total number of node in linked list
*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start,*q,*temp;
int cnt=0;
void create();
void display();
void search();
void count();
void main()
{
char ch='y';
int choice;
clrscr();
while(ch=='y'||ch=='Y')
{
printf("\n1 :to create list");
printf("\n2 :to display list");
printf("\n3 :search mode in linkd list");
printf("\n4 :to count number of node linked list");
printf("\n\nenter your choice:-");
scanf("%d",&choice);
switch(choice)
{
case 1:while(ch=='y'||ch=='Y')
{
create ();
printf("\ndo you want to insert element(Y|N):-");
scanf("\n%s",&ch);
}
break;
case 2:
display();
break;
case 3:search();
break;
case 4:count();
break;
}
printf("\n\ndo you want to enter another choice(Y|N):-");
scanf("\n%s",&ch);
}
getch();
}
void create()
{
temp=malloc(sizeof(struct node));
printf("\nenter the number:-");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void display()
{
if(start==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in singly linked list are:-\n");
q=start;
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->next;
}
}
}
void search()
{
int flag=0,num;
if(start==NULL)
printf("\nlinked list is empty");
else
{
printf("\nenter the number which you want to search:-");
scanf("%d",&num);
q=start;
while(q!=NULL)
{
if(q->data==num)
{
printf("\nnumber is found");
flag=1;
break;
}
q=q->next;
}
if(flag==0)
printf("\nnumber not found");
}
}
void count()
{
struct node *q=start;
cnt=0;
if(start==NULL)
printf("\nlinked list is empty");
else
{
while(q!=NULL)
{
q=q->next;
cnt++;
}
printf("\ntotal node:-%d",cnt);
}
}
No comments:
Post a Comment