Advertisement
asiffff

Untitled

Feb 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct student
  4. {
  5. int id;
  6. char name[20];
  7. struct student *next;
  8. }*start=NULL,*current,*ne;
  9. int search(int a)
  10. {
  11. struct student *c;
  12. c=start;
  13. int po=0;
  14. while(c!=NULL)
  15. {
  16. po++;
  17. if(c->id==a)
  18. {
  19. return po;
  20. }
  21. c=c->next;
  22. }
  23. return -1;
  24. }
  25. void cre()
  26. {
  27. if(start==NULL)
  28. {
  29. ne=(struct student*)malloc(1*sizeof(struct student));
  30. printf("\nEnter id:\n");
  31. scanf("%d",&ne->id);
  32. printf("\nEnter Name:\n");
  33. scanf("%s",&ne->name);
  34. ne->next=NULL;
  35. start=ne;
  36. current=ne;
  37. }
  38. else
  39. {
  40. ne=(struct student*)malloc(1*sizeof(struct student));
  41. printf("\nEnter id:\n");
  42. scanf("%d",&ne->id);
  43. printf("\nEnter Name:\n");
  44. scanf("%s",&ne->name);
  45. ne->next=NULL;
  46. current->next=ne;
  47. current=ne;
  48. }
  49. }
  50. void dis()
  51. {
  52. struct student *c;
  53. c=start;
  54. while(c!=NULL)
  55. {
  56. printf("\n Name: %s \n ID: %d\n",c->name,c->id);
  57. c=c->next;
  58. }
  59. }
  60. int main()
  61. {
  62. int x;
  63. while(5)
  64. {
  65. printf("\nPress 1 For cre, 2 for Dis,3 search\n");
  66. scanf("%d",&x);
  67. switch(x)
  68. {
  69. case 1:
  70. {
  71. cre();
  72. break;
  73. }
  74. case 2:
  75. {
  76. dis();
  77. break;
  78. }
  79. case 3:
  80. {
  81. printf("Enter Id:\n");
  82. int n;
  83. scanf("%d",&n);
  84. int pos = search(n);
  85. if(pos==-1)
  86. {
  87. printf("\n Not Found\n");
  88. }
  89. else
  90. {
  91. struct student *b;
  92. b=start;
  93. int j;
  94. for(j=0;j<pos-1;j++)
  95. {
  96. b=b->next;
  97. }
  98. printf("\nName: %s\n",b->name);
  99. }
  100. }
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement