Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Linked List Using C (http://www.go4expert.com/forums/linked-list-using-c-t24984/)

angad_aks 15Feb2011 23:57

Linked List Using C
 
Code:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
 int data;
 struct node *next;
};
struct node *head=NULL;
void create_list();
void display_list();
void ins_beg();
void del_beg();
void ins_aft_data(int x);
void ins_aft_node(int count);
void ins_end();
void del_after_data(int x);
void del_after_node(int count);
void del_end();
void main()
{
 int ch,x,count;
 clrscr();
 while(1)
 {
  printf("\nenter,1 for create,2 for display,3 for insert beginning,4 for ins after data,5 for ins aft node,6 for ins end,7 for del end,8 for del after data,9 for del after node,10 for del end,11 for exit");
  printf("\nenter choice");
  scanf("%d",&ch);
  switch(ch)
  {
  case 1:
                create_list();
                break;
  case 2:
                display_list();
                break;
  case 3:
                ins_beg();
                break;
  case 4:
                printf("\n enter data after which a node will be inserted");
                scanf("%d",&x);
                ins_after_data(x);
                break;
  case 5:
                printf("enter count \n");
                scanf("%d",&count);
                ins_after_node(count);
                break;
  case 6:
                ins_end();
                break;
  case 7:
                del_beg();
                break;
  case 8:
                printf("\n enter data to be deleted");
                scanf("%d",&x);
                del_after_data(x);
                break;
  case 9:
                printf("enter count \n");
                scanf("%d",&count);
                del_after_node(count);
                break;
  case 10:
                del_end();
                break;
  case 11:
                return;
  }
 }
}
void create_list()
{
 struct node *p,*t;
 if(head!=NULL)
 {
  printf("\n list already exist");
  return;
 }
 p=(struct node *)malloc(sizeof(struct node));
 printf("\n enter data:");
 scanf("%d",&(p->data));
 p->next=NULL;
 if(head==NULL)
  head=p;
 }


void ins_beg()
{
 struct node *p;
 if(head==NULL)
 {
  printf("\n no list exist");
  return;
 }
 p=(struct node *)malloc(sizeof(struct node));
 printf("\n enter data");
 scanf("%d",&(p->data));

 p->next=head;
 head=p;

}
void display_list()
{
 struct node *t;
 if(head==NULL)
 {
  printf("\n no list exist");
 return;
 }

  t=head;
  while(t!=NULL)
  {
        printf("%d",t->data);
        t=t->next;
  }
 }
void del_beg()
{
 struct node *t;
 if(head==NULL)
  printf("\n no list exist");
 else
 {
  t=head;
  head=t->next;
  free(t);
 }
 return;
}
void ins_aft_data(int x)
{
 struct node *t;
 if(head==NULL)
 {
  printf("\n no list exist");
  return;
 }
 t=head;
 while(t!=NULL)
 {
  if((t->data)==x)
  {
  struct node *p;
  p=(struct node *)malloc(sizeof(struct node));
  printf("\n enter data");
  scanf("%d",&(p->data));
  p->next=t->next;
  t->next=p;
  return;
  }
  else
  {
  t=t->next;
  }
  if(t==NULL)
  {
  printf("there is no such data,so insertion failed");
  }
 }
}
void ins_aft_node(int count)
{
 struct node *t;
 if(head==NULL)
 {
  printf("\n no list exist");
  return;
 }
 t=head;
 while(count>1 && t!=NULL)
 {
  t=t->next;
  count=count-1;
 }
 if(t!=NULL)
 {
  struct node *p;
  p=(struct node *)malloc(sizeof(struct node));
  printf("\n enter data");
  scanf("%d",&(p->data));
  p->next=t->next;
  t->next=p;
 }
 else
 {
  printf("\n insertion failed");
 }
}
void ins_end()
{
 struct node *p,*t;
 if(head==NULL)
 {
  printf("\n no list exist");
 }
 else
 {
  t=head;
  while((t->next)!=NULL)
  {
  t=t->next;
  }
  p=(struct node *)malloc(sizeof(struct node));
  printf("\n enter data");
  scanf("%d",&(p->data));
  t->next=p;
  p->next=NULL;
 }
}
void del_after_data(int x)
{
 struct node *p,*t;
 if(head==NULL)
 {
  printf("\n no list exist");
 }
 else if((head->data)==x && (head->next)==NULL)
 {
  free(head);
  head=NULL;
 }
 else if((head->data)==x && (head->next)!=NULL)
 {
  p=head;
  head=p->next;
  free(p);
 }
 else
 {
  t=head;
  while((t->next->data)!=x && (t->next)!=NULL)
  {
  t=t->next;
  }
  if((t->next)==NULL)
  {
  printf("\n deletion failed");
  }
  else
  {
  p=t->next;
  t->next=p->next;
  free(p);
  }
 }
}
void del_end()
{
 struct node *t,*p;
 if(head==NULL)
 {
  printf("\n no list exist");
 }
 else if((head->next)==NULL)
 {
  t=head;
  free(t);
  head=NULL;
 }
 else
 {
  t=head;
  while((t->next->next)!=NULL)
  {
  t=t->next;
  }
  p=t->next;
  t->next=NULL;
  free(p);
 }
}
void del_after_node(int count)
{
 struct node *t,*p;
 if(head==NULL)
 {
  printf("\n no list exist");
 }
 else if(count>1 && (head->next)==NULL)
 {
  t=head;
  while((t->next)!=NULL)
  {
  t=t->next;
  }
  count=count-1;
 }
 else if((head->next)==NULL)
 {
  free(head);
  head=NULL;
 }
 else if
 {
  free(head);
  head=NULL;
 }
 else
 {
  p=head;
  head=p->next;
  free(p);
  if((t->next)==NULL)
  printf("\n deletion failed");
  else
  {
  p=t->next;
  t->next=p->next;
  free(p);
  }
 }
}


xpi0t0s 16Feb2011 14:41

Re: Linked List Using C
 
clrscr(), getch(), single letter variable names, no comments or explanation, void main(). Please stop posting code until you can do a better job of it.

angad_aks 16Feb2011 21:26

Re: Linked List Using C
 
sir i am very new to this forum. but ya tryd . n it ws not shw off. n i gues dis is pretty discouraging 4m such a highly reputed ppl.

xpi0t0s 16Feb2011 22:54

Re: Linked List Using C
 
Well, I don't understand most of that as you seem to have half the letters missing on your keyboard. When new to a forum, it's always worth hanging around for a while to see what's going on. Don't just wade in with posts that you don't know will be appreciated.

But don't be discouraged; keep on programming, but be aware of the fact that until you've been programming every day for 8 hours a day for at least 2 years, you're not going to be anything like an expert on it. Don't post demo code until you are an expert and have something worth posting.

lionaneesh 17Feb2011 01:07

Re: Linked List Using C
 
Quote:

Originally Posted by angad_aks (Post 79426)
sir i am very new to this forum. but ya tryd . n it ws not shw off. n i gues dis is pretty discouraging 4m such a highly reputed ppl.

Please dont use SMS language here...
Its difficult for us to read and it seems too unprofessional..
Try and understand...

Quote:

Originally Posted by xpi0t0s (Post 79433)
Well, I don't understand most of that as you seem to have half the letters missing on your keyboard. When new to a forum, it's always worth hanging around for a while to see what's going on. Don't just wade in with posts that you don't know will be appreciated.

But don't be discouraged; keep on programming, but be aware of the fact that until you've been programming every day for 8 hours a day for at least 2 years, you're not going to be anything like an expert on it. Don't post demo code until you are an expert and have something worth posting.

Yes , He's right....First of all angad be prepared to get the warning and negative feedbacks as at some point i used to get that too and still i get it many a times...So don't ever be discourages as you'll ultimately end up not programming!!!!


All times are GMT +5.5. The time now is 05:55.