/*WAP of LINKED lIST insert front , print , delet front in single linked list*/
#include<stdio.h>
#include<conio.h>
void insert_front_sl();
void print_sl();
void insert_end_sl();
struct node
{
int data;
struct node *link;
};
struct node *header;
void main()
{
clrscr();
header = (struct node *) malloc(sizeof(struct node));
header->data = NULL;
header->link = NULL;
insert_front_sl();
insert_front_sl();
print_sl();
insert_end_sl();
insert_end_sl();
print_sl();
getch();
}
void insert_front_sl()
{
struct node *temp,*ptr;
int X;
temp = (struct node *) malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Memory Insufficient");
}
else
{
ptr = header->link;
printf("\nEnter the value to insert at front: ");
scanf("%d", &X);
temp->data = X;
temp->link = ptr;
header->link = temp;
}
}
void print_sl()
{
struct node *ptr;
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
printf("%d ",ptr->data);
}
}
void insert_end_sl()
{
struct node *temp,*ptr;
int X;
temp = (struct node *) malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Memory Insufficient");
}
else
{
ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
printf("\nEnter the value to insert at end: ");
scanf("%d", &X);
temp->data = X;
temp->link = NULL;
ptr->link = temp;
}
}
No comments:
Post a Comment