Showing posts with label linkedlist. Show all posts
Showing posts with label linkedlist. Show all posts

Thursday, September 8, 2016

PROGRAM TO DELETE A NODE FROM A LINKED LIST USING LOCATION :)


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *next;

}*head;
void insert(int data,int loc)
{ struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *ptr=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
 if(loc==1)
 {
  temp->next=head;
  head=temp;
  return;
 }int i;
 ptr=head;
 for(i=0;i<loc-2;i++)
 { ptr=ptr->next;

 }
 temp->next=ptr->next;
 ptr->next=temp;

}
void deplete(int loc)
{
struct node *ptr=(struct node *)malloc(sizeof(struct node));
struct node *save=(struct node *)malloc(sizeof(struct node));
save=head;
ptr=head->next;
if (loc==1)
{
head=head->next;
return;
}
else
{
for(int i=0;i<loc-2;i++)
{ save=ptr;
ptr=ptr->next;
}
save->next=ptr->next;

}
}
void print()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}

}
main()
{int a,b;
 head=NULL;
 insert(1,1);
 insert(6,1);
 insert(7,1);
 insert(9,3);
 insert(10,3);
 insert(5,2);
printf("ELEMENTS ARE :");
     print();
     deplete(4);
     printf("AFTER DELETION\n");
     print();
}
Also see program to illustrate the concept of inline function

Wednesday, September 7, 2016

PROGRAM TO DELETE A NODE FROM A LINKED LIST:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *next;

}*head;
void insert(int data,int loc)
{ struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *ptr=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
 if(loc==1)
 {
  temp->next=head;
  head=temp;
  return;
 }int i;
 ptr=head;
 for(i=0;i<loc-2;i++)
 { ptr=ptr->next;

  }
 temp->next=ptr->next;
 ptr->next=temp;

}
void insert(int data)
{struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *temp=(struct node*)malloc(sizeof(struct node));
 ptr=head;
 while(ptr->next!=NULL)
 {
  ptr=ptr->next;
  
 }
 temp->data=data;
 temp->next=NULL;
 ptr->next=temp;
 return;
 }
 void deplete(int data)
 {
  struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *save=(struct node*)malloc(sizeof(struct node));
save=head;
ptr=head->next;
if(save->data==data)
{
head=head->next;
return;
}
else{
while(ptr!=NULL)
{
if(ptr->data==data)
{
save->next=ptr->next;
return;
}
else
{
save=ptr;
ptr=ptr->next;
}}
printf("ELEMENT NOT FOUND");
 
}
 }
void print()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}
}
main()
{
 head=NULL;
 insert(1,1);
 insert(6,1);
 insert(7,1);
 insert(9,3);
 insert(10,3);
 insert(5,2);
 insert(11);
 insert(12);
 printf("ELEMENTS ARE:\n");
     print();
     deplete(9);
     printf("AFTER DELETION");
     print();
}

Saturday, September 3, 2016

PROGRAM TO INSERT NODE AT ANY POSITION AND AT END IN A LINKED LIST:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;

}*head;
void insert(int data,int loc)
{ struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *ptr=(struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
if(loc==1)
{
temp->next=head;
head=temp;
return;
}int i;
ptr=head;
for(i=0;i<loc-2;i++)
{ ptr=ptr->next;

}
temp->next=ptr->next;
ptr->next=temp;

}
void insert(int data)
{struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *temp=(struct node*)malloc(sizeof(struct node));
ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;

}
temp->data=data;
temp->next=NULL;
ptr->next=temp;
return;




}
void print()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}
}
main()
{
head=NULL;
insert(1,1);
insert(6,1);
insert(7,1);
insert(9,3);
insert(10,3);
insert(5,2);
insert(11);
insert(12);
printf("ELEMENTS ARE:\n");
     print();
}