Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. /* Code by Kowsik Nandagopan D. CSE S3 */
  2. /* Use txt file to input IN to insert, DEL to pop element, DIS to display whole queue*/
  3. /* Header file declaration */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. /* Funtion Declaration */
  9. void inv_stk();
  10. void insert(int);
  11. void inv_stk_A();
  12. void display();
  13.  
  14. /* Node structure to implement the STACK data structure */
  15. struct Node{
  16. int data;
  17. struct Node* next;
  18. };
  19.  
  20. /* Two Global STACK data structures A and B */
  21. struct Node* STACK_A_TOP = NULL;
  22. struct Node* STACK_B_TOP = NULL;
  23.  
  24. /* Invert funtion to invert the STACK A to STACK B to get the last elemennt in the STACK A */
  25. void inv_stk(){
  26. struct Node* temp_A = STACK_A_TOP;
  27. STACK_B_TOP = NULL;
  28. struct Node* temp_B = STACK_B_TOP;
  29. while (temp_A != NULL){
  30. struct Node* node = (struct Node*) malloc(sizeof(struct Node));
  31. node->data = temp_A->data;
  32.  
  33. if (STACK_B_TOP == NULL){
  34. node->next = NULL;
  35. STACK_B_TOP = node;
  36. } else {
  37. node->next = STACK_B_TOP;
  38. STACK_B_TOP = node;
  39. }
  40. temp_A = temp_A->next;
  41. }
  42. }
  43.  
  44.  
  45. /* Inset operation on the STACK A to push the newely added elements to the QUEUE. Inserting to the TOP of STACK A */
  46. void insert(int x){
  47. struct Node* temp = (struct Node*) malloc(sizeof(struct Node));
  48. temp->data = x;
  49. if (STACK_A_TOP == NULL){
  50. temp->next = NULL;
  51. STACK_A_TOP = temp;
  52. } else {
  53. temp->next = STACK_A_TOP;
  54. STACK_A_TOP = temp;
  55. }
  56. inv_stk();
  57. }
  58.  
  59.  
  60. /* Inverting back the STACK B back to STACK A as the first element in the STACK B is deleted that is the last element of STACK A */
  61. void inv_stk_A(){
  62. struct Node* temp = STACK_B_TOP;
  63.  
  64. while (temp != NULL){
  65. insert(temp->data);
  66. temp = temp->next;
  67. }
  68. }
  69.  
  70.  
  71. /* To display the QUEUE that is the STACK B after inverting the STACK A */
  72. void display(){
  73. STACK_A_TOP = NULL;
  74. struct Node* temp = STACK_B_TOP;
  75. while (temp != NULL){
  76. printf("%d ", temp->data);
  77. temp = temp->next;
  78. }
  79. }
  80.  
  81. void pop_element(){
  82. int x;
  83. if (STACK_B_TOP == NULL) printf("Queue is empty");
  84. else {
  85. x = STACK_B_TOP->data;
  86. struct Node* temp;
  87. temp = STACK_B_TOP;
  88. STACK_B_TOP = STACK_B_TOP->next;
  89. free(temp);
  90. // Invert the stack B back to A
  91. inv_stk_A();
  92. printf("%d", x);
  93. }
  94. }
  95.  
  96.  
  97. void main(int argc, char* argv[]){
  98. char c[1000];
  99. int i=0, d;
  100. /* Reading input form a text file based on the command line argument */
  101. FILE* f= fopen(argv[1], "r");
  102. if (!f) printf("NO file named %s", argv[1]);
  103. else {
  104. while (fscanf(f, "%s %d\n", c, &d) >= 1)
  105. /* Insert operation */
  106. if (strcmp(c, "IN") == 0){
  107. // printf("IN %d", d);
  108. insert(d);
  109. // printf("\n");
  110. /*Pop Operation*/
  111. } else if (strcmp(c, "DEL") == 0){
  112. // printf("POP");
  113. pop_element();
  114. printf("\n");
  115. /* Display whole QUEUE*/
  116. } else if (strcmp(c, "DIS") == 0){
  117. // printf("DIS\n");
  118. display();
  119. printf("\n");
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement