Advertisement
HmHimu

stack final

Mar 1st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. struct Node
  4. {
  5. int data;
  6. struct Node *next;
  7. } *temp_st, *node_st;
  8.  
  9. struct Head
  10. {
  11. int count;
  12. struct Node *top;
  13. } *head;
  14.  
  15. void Head_create()
  16. {
  17. head = (struct Head*) malloc(sizeof(struct Head));
  18. head->count = 0;
  19. head->top = NULL;
  20. }
  21.  
  22. void push(int value)
  23. {
  24. node_st = (struct Node*) malloc(sizeof(struct Node));
  25. node_st->data = value;
  26. node_st->next = head->top;
  27. head->top = node_st;
  28. (head->count)++;
  29. }
  30.  
  31. void pop()
  32. {
  33. if(head->count != 0)
  34. {
  35. temp_st = head->top;
  36. head->top = head->top->next;
  37. free(temp_st);
  38. (head->count)--;
  39. }
  40. else
  41. printf("\t\tStack is empty.\n");
  42. }
  43. void Search_values(int value)
  44. {
  45. int flag = 0,i;
  46. for(temp_st = head->top,i=1; temp_st != NULL; temp_st = temp_st->next,i++)
  47. {
  48. if(temp_st->data == value)
  49. {
  50. printf("\t\t%d Found in the stack in %d th position.\n",temp_st->data,i);
  51. flag=1;
  52. break;
  53. }
  54. }
  55. if(flag == 0)
  56. {
  57. printf("\t\t%d not Found in the list!\n",value);
  58. }
  59. }
  60.  
  61. void Show_values()
  62. {
  63. temp_st = head->top;
  64. printf("\t\tCurrent Values in the stack\n");
  65. while(temp_st != NULL)
  66. {
  67. printf("\t\t%d\n",temp_st->data);
  68. temp_st = temp_st->next;
  69.  
  70. }
  71. }
  72.  
  73. void Menu_function()
  74. {
  75. printf("\n\n\t\t-------------------------------------------------\n");
  76. printf("\t\t: Stack :\n");
  77. printf("\t\t-------------------------------------------------\n");
  78. printf("\t\t: 1. Push :\n");
  79. printf("\t\t-------------------------------------------------\n");
  80. printf("\t\t: 2. Pop :\n");
  81. printf("\t\t-------------------------------------------------\n");
  82. printf("\t\t: 3. Search a Data :\n");
  83. printf("\t\t-------------------------------------------------\n");
  84. printf("\t\t: 4. Show All Data :\n");
  85. printf("\t\t-------------------------------------------------\n");
  86. printf("\t\t: 5. Stack empty or not!! :\n");
  87. printf("\t\t-------------------------------------------------\n");
  88. printf("\t\t: 6. Exit program :\n");
  89. printf("\t\t-------------------------------------------------\n");
  90. printf("\t\tEnter choice :");
  91. }
  92.  
  93. int Take_number()
  94. {
  95. int value;
  96. printf("\t\tEnter a value: ");
  97. scanf("%d", &value);
  98. return value;
  99. }
  100.  
  101. int main()
  102. {
  103. int choice;
  104. Head_create();
  105. for( ; ; )
  106. {
  107. Menu_function();
  108. scanf("%d", &choice);
  109. switch (choice)
  110. {
  111. case 1:
  112. push(Take_number());
  113. break;
  114. case 2:
  115. pop();
  116. break;
  117. case 3:
  118. Search_values(Take_number());
  119. break;
  120. case 4:
  121. Show_values();
  122. break;
  123. case 5:
  124. if(head->count==0)
  125. printf("\t\tStack is empty\n");
  126. else
  127. printf("\t\tStack is not empty\n");
  128. break;
  129. case 6:
  130. free(temp_st);
  131. free(node_st);
  132. free(head);
  133. return EXIT_SUCCESS;
  134. default:
  135. printf("\t\tplease only press 1 to 9\n");
  136. break;
  137. }
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement