Advertisement
sanpai

Student information through double linked list

Sep 3rd, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<string.h>
  4. struct student
  5. {
  6. char name[10];
  7. int m1;
  8. int m2;
  9. struct student *llink;
  10. struct student *rlink;
  11. };
  12.  
  13. struct student * addelement(struct student *,char name[],int,int,int,int);
  14. void display( struct student *,char name[]);
  15.  
  16. int main()
  17. {
  18.  
  19. int num,i,m1,m2;
  20. struct student *head;
  21. head=NULL;
  22. char name[10];
  23. printf("\n Enter the number of students data to be entered :");
  24. scanf("%d",&num);
  25. for(i=1;i<=num;i++)
  26. {
  27. printf("Enter the name of the student :");
  28. scanf("%s",name);
  29. printf("\n Enter the marks in maths :");
  30. scanf("%d",&m1);
  31. printf("\n Enter the marks in science : ");
  32. scanf("%d",&m2);
  33. head=addelement(head,name,m1,m2,i,num);
  34. }
  35.  
  36. printf("\n Enter the student name to be searched : ");
  37. scanf("%s",name);
  38. display(head,name);
  39.  
  40. }
  41.  
  42. struct student * addelement(struct student *head,char name[10],int m1,int m2,int i,int num)
  43. {
  44.  
  45. struct student *newnode;
  46.  
  47. newnode=(struct student *)malloc(sizeof(struct student));
  48.  
  49. strcpy(newnode->name,name);
  50. newnode->m1=m1;
  51. newnode->m2=m2;
  52.  
  53. if(i==num)
  54. {
  55. newnode->llink=NULL;
  56. newnode->rlink=head;
  57. head->llink=newnode;
  58. return newnode;
  59. }
  60.  
  61.  
  62.  
  63. else if(head==NULL)
  64.  
  65. {
  66.  
  67. newnode->rlink=NULL;
  68. newnode->llink=NULL;
  69. return newnode;
  70.  
  71. }
  72. else
  73.  
  74. {
  75. head->llink=newnode;
  76. newnode->rlink=head;
  77. return newnode;
  78. }
  79. }
  80.  
  81. void display(struct student *p3,char name[10])
  82.  
  83. {
  84. struct student *temp;
  85. temp=p3;
  86. int flag=0;
  87.  
  88. if(p3==NULL)
  89. {
  90.  
  91. printf("\n The list is empty \n");
  92.  
  93. }
  94.  
  95. while(p3!=NULL)
  96.  
  97. {
  98. printf("\n Student Name: %s, Maths Marks: %d,Science Marks: %d",p3->name,p3->m1,p3->m2);
  99.  
  100. if(p3->m1>=90 && p3->m2>=90)
  101. {
  102. printf("\n Student Name: %s, Maths Marks: %d,Science Marks: %d",p3->name,p3->m1,p3->m2);
  103. p3=p3->rlink;
  104. }
  105. else
  106. {
  107.  
  108. p3=p3->rlink;
  109.  
  110. }
  111.  
  112.  
  113. }
  114.  
  115. p3=temp;
  116.  
  117. while(p3!=NULL)
  118. {
  119. if(strcmp(p3->name,name)==0)
  120. {
  121. flag=1;
  122. break;
  123. }
  124.  
  125. else
  126. {
  127. p3=p3->rlink;
  128. }
  129. }
  130.  
  131. if(flag==1)
  132. {
  133.  
  134. printf("\n The student information exists in the database \n ");
  135. }
  136. else
  137. {
  138. printf("\n The student information does not exist in the database \n");
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement