Linked list 2- insertion at the end of the list

insertion at beginning of linked list

C program to insert the elements at the end of a linked list and to print them from starting node to end node.

Introduction: When you don't know the size of input data, prior to the compilation, using a large size array in the program is not efficient method. So take the advantage of linked list that it takes the memory at the execution time with the help of 'malloc' function according to the size of the input.

Program:

#include<stdio.h>
#include<stdlib.h>
int main()
{
                  typedef struct node_type{                       /* structure for a node */
                  int data;
                  struct node_type *next;
                  }node;
                  node *head;                                           /* pointer to start node */
                  node *tail;                                          /* pointer to tail of the node */
                  node *temp;                                      /* temporary pointer to a node*/
                  head=tail=NULL;    /* initializing empty list */
                  char ch;
                  int n;
                  printf("enter data?\n");
                  printf("say y(yes) or n(no): ");
                  scanf("%c",&ch);
                  fflush(stdin);
                  if((ch=='y')||(ch=='Y'))
                 {
                           scanf("%d",&n);
                           head=(node *)malloc(sizeof(node)); /* only for first element */
                           head->data=n;
                           head->next=NULL;
                           tail=head;
                           fflush(stdin);
                           printf("say y(yes) or n(no): ");
                           scanf("%c",&ch);
                           fflush(stdin);
                          while((ch=='y')||(ch=='Y'))
                          {
                                    scanf("%d",&n);
                                    fflush(stdin);
                                    tail->next=(node *)malloc(sizeof(node));        /* for the rest of elements */
                                    tail->next->data=n;  
                                    tail->next->next=NULL;
                                    tail=tail->next;
                                    printf("say y(yes) or n(no): ");
                                    scanf("%c",&ch);
                                    fflush(stdin);
                          }
                          temp=head;
                          while((temp)!=NULL){                            /*  while list is not empty, print elements  */
                                      printf("%d ",temp->data);
                                      temp=temp->next;
                          }
               }
               else printf("list is empty");
               return 0;
}

Sample input1:
enter data?say y(yes) or n(no): y
9
say y(yes) or n(no): y
8
say y(yes) or n(no): y
7
say y(yes) or n(no): y
6
say y(yes) or n(no): y
5
say y(yes) or n(no): n


Output1:
9 8 7 6 5

Sample input2:
enter data?
say y(yes) or n(no): n

Output2:
list is empty
                                                    insertion at the beginning of linked list

 

No comments: