Wednesday, 20 March 2013

/*A Program by Raghav Raj to create a linked list to store the details of Students,Also to delete any record of student with the specified roll no from the list*/

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
struct student
{
    int roll;
    char name[10];
    char add[10];
    struct student *next;
};
struct student *start = NULL;
void create_Record(int,char*,char*);
void display();
void delete_Record(int);
void main()
{
  int n,i,r;
  char nm[10],ad[10];

  printf("How Many Student's Record You want to create  :");
  scanf("%d",&n);
  for(i=0; i<n; i++)
  {
     clrscr();
      printf("\n\tEnter Record %d : ",i+1);
      printf("\nEnter Roll :");
      scanf("%d",&r);
      printf("\nEnter Name :");
      scanf("%s",nm);
      printf("\nEnter Address :");
      scanf("%s",ad);
      create_Record(r,nm,ad);
  }
  clrscr();
  printf("\n\t\t****Student Records********\n\n");
  display();
  printf("\nEnter Roll No of Student To delete The record from List :");
  scanf("%d",&r);
  delete_Record(r);
  printf("After deletion of Record Of a Student List Is: ");
  display();
  getch();
}

  void create_Record(int rl,char *nm,char *ad)
  {
      struct student *q,*tmp;
      tmp = (struct student*)malloc(sizeof(struct student));
      tmp->roll = rl;
      strcpy(tmp->name,nm);
      strcpy(tmp->add,ad);
      tmp->next = NULL;

      if(start == NULL)
          start = tmp;
      else
      {
         q = start;
         while(q->next != NULL)
              q = q->next;
         q->next = tmp;
      }
  }
  void display()
  {
      struct student *tmp;
      if(start == NULL)
      {
            printf("\nList is Empty : ");
            return;
      }
      tmp = start;
      printf("\nRoll\tName\tAddress\n\n");
      while(tmp != NULL)
      {
          printf("%d\t%s\t%s\n",tmp->roll,tmp->name,tmp->add);
          tmp = tmp->next;
      }
      printf("\n");
  }
  void delete_Record(int rl)
  {
      struct student *tmp,*q;
      if(start->roll == rl)
      {
            tmp = start;
            start = start->next;
            free(tmp);
            return;
      }
      q = start;
      while(q->next->next != NULL)
      {
            if(q->next->roll == rl)
            {
                tmp = q->next;
                q->next = tmp->next;
                free(tmp);
                return;
            }
            q = q->next;
      }//End of While
      if(q->next->roll == rl)
      {
         tmp = q->next;
         free(tmp);
         q->next = NULL;
         return;
      }
      printf("Roll No %d Not Found in the List\n\n",rl);
  }

No comments:

Post a Comment