EmbLogic's Blog

how to insert and delete elements in linked list at various position

head 1.1;
access;
symbols;
locks
root:1.1; strict;
comment @ * @;

1.1
date 2014.03.27.10.47.56; author root; state Exp;
branches;
next ;

desc
@@

1.1
log
@Initial revision
@
text
@#include
#include
struct node
{
int data;
struct node * next;
};
struct node * create_ll( );
struct node * insert_ll(struct node * );
struct node * insert_beg(struct node * );
struct node * insert_pos(struct node * );
struct node * insert_end(struct node * );
struct node * delete_ll(struct node * );
struct node * delete_beg(struct node * );
struct node * delete_pos(struct node * );
struct node * delete_end(struct node * );
struct node * display_ll(struct node * );
int main()
{
struct node * start;
int ch;
do
{
printf(“\n1 create linklist”);
printf(“\n2 insert linklist”);
printf(“\n3 delete linklist”);
printf(“\n4 display linklist”);
printf(“\n5 quit”);
printf(“\n enter the choice”);
scanf(“%d”,&ch);

switch(ch)
{
case 1:
start=create_ll();
break;
case 2:
start=insert_ll(start);
break;
case 3:
start=delete_ll(start);
break;
case 4:
start=display_ll(start);
break;
}
}while(ch != 5);
return 0;
}
struct node * create_ll()
{
struct node * temp;
temp=malloc(sizeof(struct node));
printf(“enter the data”);
scanf(“%d”,&temp->data);
temp->next=NULL;
return temp;
}
struct node * insert_ll(struct node * start )
{
int ch;
printf(“\n1 insert at the beg”);
printf(“\n2 insert at the pos”);
printf(“\n3 insert at the end”);
printf(“\n enter the choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
start=insert_beg(start);
break;
case 2:
start=insert_pos(start);
break;
case 3:
start=insert_end(start);
break;

}

return start;
}
struct node * delete_ll(struct node * start )
{
int ch;
printf(“\n1 delete at the beg”);
printf(“\n2 delete at the pos”);
printf(“\n3 delete at the end”);
printf(“\n enter the choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
start=delete_beg(start);
break;
case 2:
start=delete_pos(start);
break;
case 3:
start=delete_end(start);
break;
}
return start;
}

struct node * display_ll(struct node * start)
{
struct node * temp;
temp=start;
while(temp!=NULL)
{
printf(“entered data is %d”,temp->data);
temp=temp->next;

}
return start;
}
struct node * insert_beg(struct node * start)
{
struct node * temp;
temp=create_ll();
temp->next=start;
start=temp;
return start;

}
struct node * insert_pos(struct node * start)
{
int i,n;
struct node * temp,* new;
temp=start;
new=create_ll();
printf(“enter the position to be inserted”);
scanf(“%d”,&n);
for(i=0;inext;
}
new->next=temp->next;
temp->next=new;
return start;

}
struct node * insert_end(struct node * start)
{
struct node * temp,* new;
temp=start;
new=create_ll();
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
new->next=NULL;
return start;
}
struct node * delete_beg(struct node * start)
{
struct node * temp;
temp=start;
start=start->next;;
free(temp);
return start;
}
struct node * delete_pos(struct node * start)
{
struct node * temp,* pre;
int i,m;
temp=start;
printf(“\nenter the position”);
scanf(“%d”,&m);
for(i=0;inext;
}
pre->next=temp->next;
free(temp);
return start;
}
struct node * delete_end(struct node * start)
{
struct node * temp,*pre;
temp=start;
while(temp->next!=NULL)
{
pre=temp;
temp=temp->next;
}
free(temp);
pre->next=NULL;
return start;

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>