Wednesday, 27 August 2014

To insert a node at the end of singly linked list...

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>

struct node
{
    int data;
    struct node *next;
}*start=NULL;
void end_insert();

    int main()
    {
      int ch;
      printf("1.To insert at end\n");
      printf("0.To exit\n");
      printf("\nEnter your choice....\n");
      scanf("%d",&ch);
     
         switch(ch)
     {
       
        case 1: end_insert();
                 break;
        case 0: exit(0);
 
        default:printf("\nPlease enter a valid choice");         
     }
}
void end_insert()
{
    struct node *r;
    int d;
    r=(struct node*)malloc(sizeof(struct node));
    printf("\nEnter data to insert.");
    scanf("%d",&d);
    r->data=d;
    r->next=NULL;
    if(start==NULL)
    start=r;
 else
 {
    struct node*temp;
    for(temp=start;temp->next!=NULL;temp=temp->next);
    temp->next=r;   
 }
}

3 comments:

  1. //struct node *start=NULL;

    ReplyDelete
  2. hw wil we understand whether the value is inserted or not??you are not dislaying the result

    ReplyDelete
  3. if the link list isnt defined...how can a user insert at the end???

    ReplyDelete

Note: only a member of this blog may post a comment.