Skip to main content
Write a program to insert new record at the beginning in the singly link list.
CODING:
#include <stdio.h>
#include<conio.h>
#include <alloc.h>
void main()
{
 struct node{
  int no;
  struct node *next;
 }*node,*start,*temp;
 char ch='y';
 clrscr();

 start=(struct node *)malloc(sizeof(struct node));
 node=start;

 while(ch=='y')
 {
clrscr();
printf("\nInput no. : ");
flushall();
scanf("%d",&node->no);
printf("\nEnter y to enter another no. : ");
flushall();
scanf("%c",&ch);

if(ch=='y')
{
node->next=(struct node *)malloc(sizeof(struct node));
node=node->next;
}
else
{
node->next=NULL;
}
 }
 node=start;
 clrscr();
 printf("\nValues :");

 while(node->next!=NULL)
 {
printf("\n%d",node->no);
node=node->next;
 }
 printf("\n%d\n",node->no);
 node=start;
 printf("\nEnter value to insert as starting node : ");
 temp=(struct node *)malloc(sizeof(struct node));
 flushall();
 scanf("%d",&temp->no);
 temp->next=start;
 start=temp;
 node=start;

 printf("\nValues after insertion :");

 while(node->next!=NULL)
 {
printf("\n%d",node->no);
node=node->next;
 }
 printf("\n%d\n",node->no);

 getch();

}

Comments