Advertisement
Guest User

10

a guest
Nov 15th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. typedef struct node
  5. {
  6. char name[20];
  7. char usn[20];
  8. char branch[20];
  9. int year;
  10. struct node *lptr,*rptr;
  11. }NODE;
  12. void ins_first(NODE *head)
  13. {
  14. NODE *newnode;
  15. newnode=(NODE*)malloc(sizeof(NODE));
  16. printf("\nEnter the details of the student...\n");
  17. printf("Name: ");
  18. scanf("%s",newnode->name);
  19. printf("USN: ");
  20. scanf("%s",newnode->usn);
  21. printf("Branch: ");
  22. scanf("%s",newnode->branch);
  23. printf("Year of admission: ");
  24. scanf("%d",&newnode->year);
  25. newnode->lptr=head;
  26. newnode->rptr=head->rptr;
  27. if(head->rptr!=NULL)
  28. head->rptr->lptr=newnode;
  29. head->rptr=newnode;
  30. printf("Student is added successfully to the list");
  31. head->year++;
  32. }
  33. void display1(NODE *head)
  34. {
  35. NODE *first;
  36. char branch[20];
  37. int flag=0;
  38. if(head->rptr==NULL)
  39. {
  40. printf("\nEmpty list");
  41. return;
  42. }
  43. printf("\nEnter the branch: ");
  44. scanf("%s",branch);
  45. first=head->rptr;
  46. while(first!=NULL)
  47. {
  48. if(strcmp(first->branch,branch)==0)
  49. {
  50. if(flag==0)
  51. {
  52. printf("\nList of students belonging to branch %s\n",branch);
  53. printf("\n\nName\tUSN\tYear of admission\n");
  54. flag=1;
  55. }
  56. printf("%s\t%s\t%d\n",first->name,first->usn,first->year);
  57. }
  58. first=first->rptr;
  59. }
  60. if(flag==0)
  61. printf("\nFailure, no student from branch %s",branch);
  62. }
  63. void display2(NODE *head)
  64. {
  65. NODE *first;
  66. if(head->rptr==NULL)
  67. {
  68. printf("\nEmpty list");
  69. return;
  70. }
  71. printf("\nName\tUSN\tBranch\tYear of admission\n");
  72. first=head->rptr;
  73. while(first!=NULL)
  74. {
  75. printf("%s\t%s\t%s\t%d\n",first->name,first->usn,first->branch,first->year);
  76. first=first->rptr;
  77. }
  78. printf("\nTotal number of students = %d",head->year);
  79. }
  80. int main()
  81. {
  82. NODE *head;
  83. int choice;
  84. head=(NODE*)malloc(sizeof(NODE));
  85. head->lptr=head->rptr=NULL;
  86. head->year=0;
  87. while(1)
  88. {
  89. printf("\n1:Add student\n2:Display based on branch\n3:Display all\n4:exit");
  90. printf("\nEnter your choice: ");
  91. scanf("%d",&choice);
  92. switch(choice)
  93. {
  94. case 1: ins_first(head);
  95. break;
  96. case 2: display1(head);
  97. break;
  98. case 3:display2(head);
  99. break;
  100. case 4: exit(0);
  101. default: printf("\nInvalid choice");
  102. }
  103. }
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement