google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: C program in data structure to make circular linked list

Monday, April 6, 2015

C program in data structure to make circular linked list

#include<stdio.h>
#include<conio.h>
#include<process.h>
void create();
void display();
void addbeg();
void addlast();
struct node
{
 int info;
 struct node *link;
}*start=NULL;
void main()
{
 int n;
 clrscr();
 while(1)
 {
  printf("\n **menu**\n");
  printf("\n 1. to create list \n 2. to display list");
  printf("\n 3. to add node at beginning of linked list");
  printf("\n 4. to add node at the last of linked list");
  printf("\n 5. exit");
  printf("\n Enter your choice:-");
  scanf("%d",&n);
  switch(n)
  {
   case 1 : create();
   break;
   case 2 : display();
   break;
   case 3 : addbeg();
   break;
   case 4 : addlast();
   break;
   case 5 : exit(0);

  }
 }
 getch();
}

void create()
{
 struct node *temp,*q;
 int x;
 temp=(struct node *)malloc(sizeof(struct node));
 if(temp==NULL)
 {
  printf("\n Insufficient memory");
 }
 else
 {
  printf("\n Enter number:-");
  scanf("%d",&x);
  temp->info=x;
  temp->link=NULL;
  if(start==NULL)
  {
   start=temp;
  }
  else
  {
   q=start;
   while(q->link!=start)
   {
    q=q->link;
   }
   q->link=temp;
  }
  temp->link=start;
 }
}

void display()
{
  struct node *q;
  printf("\n Circular linked list is:-\n\n");
  q=start;
  while(q->link!=start)
  {
   printf("   %d",q->info);
   q=q->link;
  }
  printf("   %d",q->info);
}

void addbeg()
{
 int x;
 struct node *temp,*q;
 temp=(struct node*)malloc(sizeof(struct node));
 if(temp==NULL)
 {
  printf("\n Insufficient memory");
 }
 else
 {
  printf("\n Enter number:-");
  scanf("%d",&x);
  temp->info=x;
  temp->link=NULL;

  q=start;
  while(q->link!=start)
  {
   q=q->link;
  }
  temp->link=start;
  start=temp;
  q->link=start;
 }
}
void addlast()
{
 struct node *temp,*q;
 int x;
 temp=(struct node*)malloc(sizeof(struct node));
 if(temp==NULL)
 {
  printf("\n Insufficient memory");
 }
 else
 {
  printf("\n Enter number:-");
  scanf("%d",&x);
  temp->info=x;
  temp->link=NULL;

  q=start;
  while(q->link!=start)
  {
   q=q->link;
  }
  q->link=temp;
  temp->link=start;
 }
}

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...