Show Mobile Navigation

Thursday, September 20, 2012

Perform Various Operations on Link List

Anonymous - 17:37
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*start=NULL,*ptr,*temp;

//---------------------------create list-----------------------------------//
void create(int item)

{
struct node *new1;
if(start==NULL)
{
new1=(struct node*)malloc(sizeof(struct node));
new1->info=item;
new1->next=NULL;
start=new1;

}

else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
new1=(struct node*)malloc(sizeof(struct node));
new1->info=item;
new1->next=NULL;
ptr->next=new1;

}
}

//------------------------------Display---------------------------------//

void display()
{
if(start==NULL)
{
printf("List is Empty");
}
else
{
int i=1;
ptr=start;
while(ptr!=NULL)
{
printf("Node%d: %d\t",i,ptr->info);
ptr=ptr->next;
i++;
}
}
printf("\n");
}

//--------------------add at beginning----------------------------------//

void addatbegin(int item)
{
struct node *new1;
new1=(struct node*)malloc(sizeof(struct node));
new1->info=item;
new1->next=start;
start=new1;

}
//-------------------add at specified location--------------------------//

void addatsp(int loc)
{
int count=0,i;
ptr=start;
while(ptr!=NULL)
{
count=count+1;
ptr=ptr->next;
}

if(loc<=count)
{
int item;
struct node *new1;
ptr=start;
printf("Enter item to be added :");
scanf("%d",&item);
for(i=1;i<loc;i++)
{
ptr=ptr->next;
}
new1=(struct node*)malloc(sizeof(struct node));
new1->info=item;
new1->next=ptr->next;
ptr->next=new1;

}
else
{
printf("Location Not Found!!\n");
}
}
//---------------------------delete from beginning------------------------//
void deletebegin()
{
ptr=start;
start=ptr->next;
free(ptr);
}
//-------------------------------delete from end--------------------------//
void deletelast()
{
ptr=start;
temp=ptr;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
free(ptr);
}
//-----------------------delete from specified loation-------------------//
void deletesp(int loc)
{
int count=0,i;
ptr=start;
while(ptr!=NULL)
{
count=count+1;
ptr=ptr->next;
}

if(loc<=count)
{
ptr=start;
temp=ptr;
for(i=1;i<loc;i++)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=ptr->next;
free(ptr);
}
else
{
printf("Location Not Found!!\n");
}
}
//-----------------------------delete specified item-----------------------//
void deleteitem(int item)
{
ptr=start;
temp=ptr;
while(ptr->info!=item)
{
temp=ptr;
ptr=ptr->next;
}

if(ptr->info=item)
{
temp->next=ptr->next;
free(ptr);
}

else
{
printf("Location Not Found!!\n");
}
}
//---------------------------main function------------------------------//

main()
{
int ch,loc,item;
clrscr();
printf("\n\n\t\tPERFORM VARIOUS OPERATION ON LINK LIST");
while(1)
{
printf("\n1:Create list                 2:Display list ");
printf("\n3:Add at beginning            4:Add after specified position");
printf("\n5:Add after last node         6:delete from beginning ");
printf("\n7:delete from end             8:delete from specified postion");
printf("\n9:delete specified item      10:Exit\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter Item to add in List");
scanf("%d",&item);
create(item);
break;
case 2:display();
break;
case 3:printf("Enter Item to add in List at beginning");
scanf("%d",&item);
addatbegin(item);
break;
case 4:printf("Enter position of item after you want to add in List");
scanf("%d",&loc);
addatsp(loc);
break;
case 5:printf("Enter Item to add in List");
scanf("%d",&item);
create(item);
break;
case 6:deletebegin();
break;
case 7:deletelast();
break;
case 8:printf("Enter position of item which you want to deletefrom list");
scanf("%d",&loc);
deletesp(loc);
break;
case 9:printf("Enter item which you want to delete");
scanf("%d",&item);
deleteitem(item);
break;
case 10:exit(0);
break;
default :printf("You Entered a Wrong Choice\n");
}
}
}
//---------------------end of program-----------------------------------//

0 comments:

Post a Comment

Comments : Read Them Or Add One to promote us