Advertisement
sanpai

Queue Operations

Sep 10th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *ptr;
  7. };
  8.  
  9. struct node * addq(struct node *,int);
  10. void display(struct node *);
  11. struct node * deleteq(struct node *);
  12. struct node *head,*rear;
  13. int main()
  14. {
  15. int choice,item;
  16.  
  17. rear=NULL;
  18.  
  19.  
  20. for(;;)
  21. {
  22. printf("\n Enter the choice : 1-Insert 2- Display");
  23. printf(" \n 3-Delete 4-Exit : ");
  24. scanf("%d",&choice);
  25. switch (choice)
  26. {
  27.  
  28. case 1 :printf(" \n Enter the number to be added to queue");
  29. scanf("%d",&item);
  30. rear=addq(rear,item);
  31. break;
  32. case 2 :display(head);
  33. break;
  34. case 3 :head=deleteq(head);
  35. break;
  36. default:exit(0);
  37. break;
  38. }
  39. }
  40.  
  41. }
  42.  
  43. struct node * addq(struct node *rear,int item)
  44. {
  45. struct node *newnode;
  46. newnode=(struct node *)malloc(sizeof(struct node));
  47. newnode->data=item;
  48. newnode->ptr=NULL;
  49.  
  50. if(rear==NULL)
  51. {
  52. head=newnode;
  53. return newnode;
  54. else
  55. {
  56. rear->ptr=newnode;
  57. return newnode;
  58. }
  59. }
  60.  
  61. void display(struct node *head)
  62. {
  63. while(head!=NULL)
  64. {
  65. printf("%d \t",head->data);
  66. head=head->ptr;
  67. }
  68. }
  69.  
  70. struct node * deleteq(struct node *head)
  71. {
  72.  
  73. struct node *temp;
  74.  
  75. if(head==NULL)
  76. {
  77. printf("\n The queue is empty");
  78. return head;
  79. }
  80.  
  81. else
  82. {
  83. temp=head;
  84. head=head->ptr;
  85. printf(" \n The deleted item is : %d",temp->data);
  86. free(temp);
  87. return head;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement