Saturday, November 5, 2011

Link List Creation, Insertion, Deletion, Searching


This is the part 1 one of Link list operation


#include<conio.h>
#include<stdio.h>
#include<alloc.h>
//Creation of node
struct node
{
int value;
struct node *next;
};
//inserting new nodes
void insert(struct node **start,int v)
{
struct node *ptr;

ptr=(struct node *)malloc(sizeof(struct node *));
ptr->next=NULL;
ptr->value=v;

if(*start==NULL)
printf("\nList is empty. Please create the list first....");
else
{
ptr->next=*start;
*start=ptr;
}
}
//creation of the first node in the list
void create_list(struct node **start,int v)
{
struct node *ptr;

ptr=(struct node *)malloc(sizeof(struct node *));
ptr->next=NULL;
ptr->value=v;

if(*start==NULL)
*start=ptr;
else
printf("\nList has been already created.....");
}
//delete the whole list
void del_whole(struct node **start)
{
if(*start!=NULL)
*start=NULL;
else
printf("\nUnder flow.....");
}
//disply the list
void display(struct node *start)
{
struct node *ptr;
ptr=start;
printf("\nStart Pointer is at %u\n",start);
while(ptr!=NULL)
{
printf("\nAddress %u Value %d\tNext %u",ptr,ptr->value,ptr->next);
ptr=ptr->next;
}
}
//find value from the list
void find(struct node *start,int val)
{
struct node *ptr;
int flag=0;
ptr=start;
while(ptr!=NULL)
{
if(ptr->value==val)
{
flag=1;
break;
}
else
ptr=ptr->next;
}
if(flag==1)
printf("Value is present at position %u",ptr);
else
printf("Sorry your value is not present");
}
//find minimum number from the link list
void min(struct node *start)
{
struct node *ptr;
int m;
ptr=start;
m=ptr->value;
while(ptr!=NULL)
{
if(ptr->value<m)
m=ptr->value;
ptr=ptr->next;
}
printf("Minimum %u",m);
}

void main()
{
char ch;
int v;
struct node *start=NULL;
clrscr();
//run the loop until user wants to to exit by option 7
do
{
//option list
printf("\n1.Create List\n2.Insert\n3.Delete Whole list\n4.Search value\n5.Display\n6.Find Minimum\n7.Exit");
printf("\nEnter your choice:>>");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value:");
scanf("%d",&v);
create_list(&start,v);
break;
case 2:
printf("Enter value:");
scanf("%d",&v);
insert(&start,v);
break;
case 3:
del_whole(&start);
break;
case 4:
printf("Enter value:");
scanf("%d",&v);
find(start,v);
getch();
break;
case 5:
display(start);
getch();
break;
case 6:
min(start);
getch();
break;
default:
printf("Invalid option has been selected...");
}
}while(ch!=7);//end of the loop
}//end of the prog

2 comments:

thanku sir...ds programs are currently in need....its vry mch hlpful....

You are most welcome. I will update more DS programs soon. Visit soon here.

Post a Comment

You are most welcome for making comments here. I will consider your view and will reply you soon.