Singly linked list

Q. Write a C program that create a singly linked list.


Ans.


/*c program for creating singly linked list*/
#include<stdio.h>
#include<conio.h>
struct single_link_list
{
  int age;
  struct single_link_list *next;
};
typedef struct single_link_list node;
node *makenode(int );
int main()
{
  int ag;
  node *start,*last,*nn;   //nn=new node
  start=NULL;
  while(1)
  {
     printf("Enter your age : ");
     scanf("%d",&ag);
     if(ag==0)
        break;
     nn=makenode(ag);
     if(start==NULL)
     {
        start = nn;
        last = nn;
     }   
     else
     {
        last->next = nn;
        last = nn;
     }
  }
  printf("\n\t****Single linked list****\n\n");
  for(; start!=NULL; start=start->next)
     printf("%d\t",start->age);
  getch();
  return 0;
}


/*creation of node*/


node *makenode(int tmp)
{
  node *nn;
  nn = (node *)malloc(sizeof(node));
  nn->age = tmp;
  nn->next = NULL;
  return nn;
}

/**************** OUTPUT *****************/
Singly linked list


Related post:
  1. What is linked list in C?
  2. Traversing the list
  3. Search item in linked list
  4. Insert an item
  5. Deleting an item
  6. All linked list operation in program

Comments