#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define n 10
struct Node
{
int data;
struct Node* next;
};
struct Node* createNode(int item)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=item;
newNode->next=NULL;
return newNode;
}
void search(struct Node* arr[])
{
int item,pos;
struct Node* temp;
printf("Enter item to be searched :");
scanf("%d",&item);
pos=item%10;
temp=arr[pos];
while(temp!=NULL)
{
if(temp->data==item)
{
printf("Found");
return;
}
temp=temp->next;
}
printf("Not Found");
}
void insert(struct Node* arr[])
{
struct Node* temp;
int pos,item;
printf("Enter Item : ");
scanf("%d",&item);
pos=item%10;
temp=arr[pos];
if(temp==NULL)
{
arr[pos]=createNode(item);
return;
}
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=createNode(item);
}
void display(struct Node* arr[])
{
int i;
struct Node* temp;
for(i=0;i<n;i++)
{
printf("\n");
temp=arr[i];
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
}
void delete(struct Node* arr[])
{
int i,item,pos,present;
struct Node* temp;
struct Node* prev;
printf("Enter item to be deleted : ");
scanf("%d",&item);
pos=item%10;
temp=arr[pos];
prev=arr[pos];
if(temp==NULL)
{
printf("Not Found");
return ;
}
if(temp->data==item)
{
arr[pos]=NULL;
return;
}
while(temp!=NULL)
{
if(temp->data==item)
{
present=1;
break;
}
prev=temp;
temp=temp->next;
}
if(present) prev->next=temp->next;
else printf("Not Found");
}
void main()
{
int i,ch;
struct Node* arr[n];
for(i=0;i<n;i++)
{
arr[i]=NULL;
}
while(1)
{
printf("\n1. Insert");
printf("\n2. Search");
printf("\n3. Delete");
printf("\n4. Display");
printf("\n5. Exit");
printf("\n Enter Your Choice : ");
scanf("%d",&ch);
if(ch==1) insert(arr);
else if(ch==2) search(arr);
else if(ch==3) delete(arr);
else if(ch==4) display(arr);
else if(ch==5) break;
else printf("Invalid Choice ");
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter