bipo143

Quewe full lab 8 tarik

Apr 8th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8.  
  9. void insertion(struct node *f,struct node *r,int vv);
  10. void display(struct node *f);
  11. void deletion(struct node *f,struct node *r);
  12. void Count(struct node *f);
  13.  
  14. int main()
  15. {
  16. int v,c;
  17. struct node *front, *rear;
  18. front=(struct node *) malloc(1*sizeof(struct node));
  19. rear=(struct node *) malloc(1*sizeof(struct node));
  20. front->data=0;
  21. front->next=NULL;
  22. rear->data=0;
  23. rear->next=NULL;
  24. while(1)
  25. {
  26. printf("\n----------Menu-----------\n");
  27. printf("\n Press 1 for insertion\n");
  28. printf("\n Press 2 for deletion\n");
  29. printf("\n Press 3 for display\n");
  30. printf("\n Press 4 for Count\n");
  31. printf("\n Press 0 for quite\n");
  32. printf("\n Enter your choice\n");
  33. scanf("%d",&c);
  34. switch(c)
  35. {
  36. case 0:exit(0);
  37. break;
  38.  
  39. case 1:printf("\n choice = insertion \n");
  40. printf("\n Enter new value \n");
  41. scanf("%d",&v);
  42. insertion(front,rear,v);
  43. break;
  44.  
  45. case 2:printf("\n choice=deletion\n");
  46. if(front->next!=NULL)
  47. deletion(front,rear);
  48. else
  49. printf("\n The quewe is empty\n");
  50. break;
  51.  
  52. case 3:printf("\nchoice = display\n");
  53. if(front->next!=NULL)
  54. display(front);
  55. else
  56. printf("\nThe queue is empty\n");
  57. break;
  58.  
  59. case 4: printf("\n choice=Count\n");
  60. Count(front);
  61. break;
  62.  
  63. default: printf("\n Wrong position\n\n");
  64. break;
  65. }
  66.  
  67. }
  68. }
  69.  
  70. void insertion(struct node *f,struct node *r,int vv)
  71. {
  72.  
  73. struct node *temp;
  74. temp=(struct node *) malloc(1*sizeof(struct node));
  75. temp->data=vv;
  76. temp->next=NULL;
  77. if(f->next==NULL)
  78. {
  79. f->next=temp;
  80. r->next=temp;
  81. }
  82. else
  83. {
  84. r->next->next=temp;
  85. r->next=temp;
  86. }
  87. }
  88.  
  89. void display(struct node *f)
  90. {
  91. while(f->next!=NULL)
  92. {
  93. printf("\t%d",f->next->data);
  94. f=f->next;
  95. }
  96. }
  97.  
  98. void deletion(struct node *f,struct node *r)
  99. {
  100. struct node *temp;
  101. temp=f->next;
  102. if(f->next==r->next)
  103. {
  104. f->next=NULL;
  105. r->next=NULL;
  106. }
  107. else
  108. f->next=temp->next;
  109. printf("\n the deleted node=%d\n",temp->data);
  110. free(temp);
  111. }
  112.  
  113. void Count(struct node *f)
  114. {
  115. int t=0;
  116. while(f->next!=NULL)
  117. {
  118. t=t+1;
  119. f=f->next;
  120. }
  121. printf("\n total node=%d\n",t);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment