Linked List Program

Linked List Program:

#include<stdio.h>
#include<malloc.h>
struct node
{
  int info;
  struct node *next;
};
struct node *start;
create();
display();
void main()
{
  int ch,n;
  start=NULL;
  printf("1-Create List\n2-Display\nEnter your choice\n");
  scanf("%d",&ch);
  do
  {
    switch(ch)
    {
      case 1:
      printf("Enter number\n");
      scanf("%d",&n);
      create(n);
      break;

      case 2:
      display();
      break;
    }
    printf("Enter your choice\n");
    scanf("%d",&ch);
  }while(ch!=0);
}

create(int data)
{
  struct node *q;
  struct node *temp=malloc(sizeof(struct node));
  temp->info=data;
  temp->next=NULL;
  if(start==NULL)
  {
    start=temp;
  }
  else
  {
    q=start;
    while(q->next!=NULL)
    {
      q=q->next;
    }
    q->next=temp;
  }
}

display()
{
  struct node *ptr;
  ptr=start;
  while(ptr!=NULL)
  {
    printf("%d\n",ptr->info);
    ptr=ptr->next;
  }
}

Post a Comment

0 Comments