//Program to delete alternate nodes of linked list
#include<stdio.h>
#include<conio.h>
typedef struct node node;
struct node{
int data;
node *link;
};
node *start=NULL;
void insert()
{
node *ptr=(node*)malloc(sizeof(node));
int item;
printf("Enter data : ");
scanf("%d",&ptr->data);
ptr->link=start;
start=ptr;
}
void display()
{
node *ptr=start;
printf("\nList is : ");
while(ptr!=NULL)
{
printf("%d
",ptr->data);
ptr=ptr->link;
}
}
void alternate()
{
node *temp,*ptr,*a,*p;
temp=start;
ptr=start->link;
while(temp!=NULL)
{
temp->link=ptr->link;
a=ptr;
ptr=ptr->link->link;
temp=temp->link;
free(a);
}
p=start;
printf("\nAlternate nodes are : ");
while(p!=NULL)
{
printf("%d
",p->data);
p=p->link;
}
}
void main()
{
int n,i;
clrscr();
printf("\nEnter number of nodes in list : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
insert();
}
display();
alternate();
getch();
}
Output:
No comments:
Post a Comment