Search This Blog

Tuesday, May 11, 2010

Singly Link List : C code

#includestdio.h
#includeconio.h
#includealloc.h
#includestdlib.h
struct s_ll
{
 int x;
 struct s_ll *nxt;

} *ll;

void append(struct s_ll **, int);
void in_begin(struct s_ll **, int);
void del(struct s_ll **, int);
void in_middle(struct s_ll **, int, int);
int count(struct s_ll *);
void display(struct s_ll *);

void main()
{
 int i=2;
 int j=2;
 int ch,num,loc;

 ll=NULL;
 clrscr();
 printf("PRESS ENTER ");

 getch();
 clrscr();
 for(i=1;i<20;i++)
 {
 gotoxy(i,j);
  printf("*");
  j++;

 }
  for(;i<39;i++)
 {
 j--;
 gotoxy(i,j);
 printf("*");

 }
     getch();
 clrscr();

  gotoxy(0,0);
  printf("****** MAIN MENU ***********\n");
  printf("1. READ A FILE\n");
  printf("2. WRITE A FILE\n");
  printf("3. XXXXXXXXXXXXX\n");
  printf("4. YYYYYYYYYYYYY\n");
  printf("****************************\n");



 getch();


 do
 {
  clrscr();
  gotoxy(0,0);
  printf("******** PROGRAM TO IMPLEMENT SINGLY nxt LIST ***********\n");
  printf("-----------------------------------------------------------\n");
  printf("1. CREATE OR APPEND\n");
  printf("2. INSERT AT START\n");
  printf("3. INSERT AT MIDDLE\n");
  printf("4. DELETE A NODE\n");
  printf("5. COUNT NODE\n");
  printf("6. DISPLAY NODE\n");
  printf("7. EXIT\n");
  printf("***********************************************************\n");

  oper:
  printf("ENTER CHOICE (1-6)/7 FOR EXIT - ");
  scanf("%d",&ch);

 switch(ch)
 {
   case 1:
    char res;
    do {
      printf("ENTER ANY NUMBER : ");
      scanf("%d",&num);
      append(&ll,num);
      printf("ENTER AGAIN (y/n) : ");
      fflush(stdin);
      res=getchar();

    } while(res != 'n');
    break;
case 2:
printf("Enter The x : ");
scanf("%d",&num);
in_begin(&ll,num);
break;
case 3:
printf("\nEnter The Position :");
scanf("%d",&loc);
printf("\nEnter The x : ");
scanf("%d",&num);
in_middle(&ll,loc,num);
break;
case 4:
printf("\nEnter The x u Want To Delete : ");
scanf("%d",&num);
del(&ll,num);
break;
case 5:
printf("\nThe No Of s_lls Are %d",count(ll));
getch();
break;
case 6:
display(ll);
getch();
break;
case 7:
printf("\n\nQuiting.......");
getch();
exit(0);
break;
default:
printf("Invalid choice.Please Enter Correct Choice");
//goto oper;
 }

 }while(ch != 7);



}

void append(struct s_ll **q,int num)
{ struct s_ll *temp,*r;
temp = *q;
if(*q==NULL)
{ temp = (struct s_ll *)malloc(sizeof(struct s_ll));
temp->x=num;
temp->nxt=NULL;
*q=temp;
}
else
{ temp = *q;
while(temp->nxt !=NULL)
{ temp=temp->nxt;
}
r = (struct s_ll *)malloc(sizeof(struct s_ll));
r->x=num;
r->nxt=NULL;
temp->nxt=r;
}
}
void display(struct s_ll *q)
{ if(q==NULL)
{ printf("\n\nEmpty nxt List.Can't Display The x");
getch();
goto last;
}
while(q!=NULL)
{ printf("\n%d",q->x);
q=q->nxt;
}
last:
}

int count(struct s_ll *q)
{ int c=0;
if(q==NULL)
{ printf("Empty nxt List.\n");
getch();
goto last;
}
while(q!=NULL)
{ c++;
q=q->nxt;
}
last:
return c;
}

void in_begin(struct s_ll **q,int num)
{ struct s_ll *temp;
if(*q==NULL)
{ printf("nxt List Is Empty.Can't Insert.");
getch();
goto last;
}
else
{ temp=(struct s_ll *)malloc(sizeof(struct s_ll));
temp->x=num;
temp->nxt=*q;
*q=temp; /* pointing to the first s_ll */
}
last:
getch();
}

void in_middle(struct s_ll **q,int loc,int num)
{ struct s_ll *temp,*n;
int c=1,flag=0;
temp=*q;
if(*q==NULL)
{ printf("\n\nnxt List Is Empty.Can't Insert.");
getch();
goto last;
}
else
while(temp!=NULL)
{ if(c==loc)
{ n = (struct s_ll *)malloc(sizeof(struct s_ll));
n->x=num;
n->nxt=temp->nxt;
temp->nxt=n;
flag=1;
}
c++;
temp=temp->nxt;
}
if(flag==0)
{ printf("\n\ns_ll Specified Doesn't Exist.Cant Enter The x");
getch();
}
else
{ printf("x Inserted");
getch();
}
last:
getch();
}
void del(struct s_ll**q,int num)
{ if(*q==NULL)
{ printf("\n\nEmpty nxted List.Cant Delete The x.");
getch();
goto last;
}
else
{
struct s_ll *old,*temp;
int flag=0;
temp=*q;
while(temp!=NULL)
{ if(temp->x==num)
{ if(temp==*q) /* First s_ll case */
*q=temp->nxt; /* shifted the header s_ll */
else
old->nxt=temp->nxt;
free(temp);
flag=1;
}
else
{ old=temp;
temp=temp->nxt;
}
}
if(flag==0)
printf("\nx Not Found...");
else
printf("\nx Deleted...Tap a key to continue");
getch();
}
last:
getch();
}

No comments:

Post a Comment