Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. // inserting aand deleting nodes in a quene
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. struct queneNode {
  7. char data; // define data as a char
  8. struct queneNode* nextPtr; // queneNode pointer
  9. };
  10.  
  11. typedef struct queneNode QueneNode;
  12. typedef QueneNode* QueneNodePtr;
  13.  
  14. void instructions();
  15. void enquene(QueneNodePtr* headPtr, QueneNodePtr* tailPtr, char value);
  16. int dequene(QueneNodePtr* headPtr, QueneNodePtr* tailPtr);
  17. void printQuene(QueneNodePtr currentPtr);
  18. int isEmpty(QueneNodePtr headPtr);
  19.  
  20. int main()
  21. {
  22. QueneNodePtr headPtr = NULL; // points to quene head
  23. QueneNodePtr tailPtr = NULL; // points to quene tail
  24. int choice; // user's menu choice
  25. int item; // char input by user
  26.  
  27. instructions(); // display the menu
  28. scanf_s("%d", &choice);
  29.  
  30. // while user does not enter 3
  31. while (choice != 3)
  32. {
  33. switch (choice)
  34. {
  35. case 1: // push value onto stack
  36. printf("Enter a cahractor : ");
  37. scanf_s("\n%c", &item);
  38. enquene(&headPtr, &tailPtr, item);
  39. printQuene(headPtr);
  40. break;
  41.  
  42. case 2: // pop value off stack
  43. if (!isEmpty(headPtr))
  44. {
  45. item = dequene(&headPtr, &tailPtr);
  46. printf("%c has been dequened\n", item);
  47. }
  48. printQuene(headPtr);
  49. break;
  50.  
  51. default:
  52. printf("Invalid choice\n");
  53. break;
  54. }
  55. printf("\n");
  56. instructions(); // display the menu
  57. scanf_s("%d", &choice);
  58. }
  59. printf("End of run\n");
  60. return 0;
  61. }
  62.  
  63. // display program instructions to user
  64. void instructions()
  65. {
  66. printf("1 to add an item to the quene\n");
  67. printf("2 to remove an item from the quene\n");
  68. printf("3 to end program\n");
  69. printf("Enter choice : ");
  70. } // end function instructions
  71.  
  72. // insert a node in at quene tail
  73. void enquene(QueneNodePtr* headPtr, QueneNodePtr* tailPtr, char value)
  74. {
  75. QueneNodePtr newPtr; // pointer to new node
  76.  
  77. newPtr = malloc(sizeof(QueneNode));
  78.  
  79. if (newPtr != NULL) // is space avaliable
  80. {
  81. newPtr->data = value;
  82. newPtr->nextPtr = NULL;
  83.  
  84. // if empty , insert node at head
  85. if (isEmpty(*headPtr))
  86. {
  87. *headPtr = newPtr;
  88. }
  89. else
  90. {
  91. (*tailPtr)->nextPtr = newPtr;
  92. }
  93. *tailPtr = newPtr;
  94. }
  95. else
  96. {
  97. printf("%c not inserted. No memory avaliable.\n", value);
  98. }
  99. } // end function enquene
  100.  
  101. // remove a node from quene head
  102. int dequene(QueneNodePtr* headPtr, QueneNodePtr* tailPtr)
  103. {
  104. char value; // node value
  105. QueneNodePtr tempPtr; // temporary node pointer
  106.  
  107. value = (*headPtr)->data;
  108. tempPtr = *headPtr;
  109. *headPtr = (*headPtr)->nextPtr;
  110.  
  111. // if quene is empty
  112. if (*headPtr == NULL)
  113. {
  114. tailPtr == NULL;
  115. }
  116.  
  117. free(tempPtr);
  118. return value;
  119. } // end function dequene
  120.  
  121. void printQuene(QueneNodePtr currentPtr)
  122. {
  123. // if quene is empty
  124. if (currentPtr == NULL)
  125. {
  126. printf("The quene is empty\n");
  127. }
  128. else
  129. {
  130. printf("The quene is : ");
  131.  
  132. // while not the end of the stack
  133. while (currentPtr != NULL)
  134. {
  135. printf("%c --> ", currentPtr->data);
  136. currentPtr = currentPtr->nextPtr;
  137. }
  138. printf("NULL\n");
  139. }
  140. } // end function printQuene
  141.  
  142. // Return 1 if stack is empty
  143. // Return 0 if not empty
  144. int isEmpty(QueneNodePtr headPtr)
  145. {
  146. return headPtr == NULL;
  147. } // end function isEmpty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement