Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2. typedef struct list{
  3. char value[20];
  4. struct list *next;
  5. };
  6. int cp=0;
  7. #include <stdio.h>
  8. #pragma hdrstop
  9. #include <stdlib.h>
  10. #include <conio.h>
  11. #include <string.h>
  12. int strt=0;
  13. void deleteList(struct list **);
  14. //---------------------------------------------------------------------------
  15.  
  16. #pragma argsused
  17. void end(struct list **top){
  18. struct list *prev=NULL;
  19. if(strt){
  20. if((*top)!=NULL){
  21. while ((*top)) {
  22. prev = (*top);
  23. (*top) = (*top)->next;
  24.  
  25. free(prev);
  26. free(*top);
  27. }
  28. cp=0;
  29. }
  30. strt=0;
  31. printf("end of work");
  32. cp=0;
  33. }
  34. else
  35. printf("work isn't started yet");
  36. getch();
  37. }
  38. void start(struct list **top){
  39. if (!strt)
  40. {
  41. int i;
  42. char pw[50];
  43. int size;
  44.  
  45. printf("Input password.\n");
  46. printf("> ");
  47.  
  48. fgets(pw, 50, stdin);
  49. size = strlen(pw);
  50. pw[size - 1] = '\0';
  51.  
  52. if (!strcmp(pw, "Jade"))
  53. {
  54. strt = 1;
  55.  
  56. printf("Correct password.");
  57. }
  58. else
  59. printf("Incorrect password,try again.");
  60. }
  61. else
  62. printf("Work already started.");
  63. getch();
  64. }
  65. void pop(struct list **top){
  66. struct list *list2=NULL;
  67. if(strt){
  68. if((*top)!=NULL){
  69. list2=(*top);
  70. (*top)=(*top)->next;
  71.  
  72. free(list2);
  73. cp--;
  74. }
  75. else
  76. printf("Stack is empty\n");
  77. }
  78. else
  79. printf("Work isn't started");
  80. getch();
  81. }
  82. void isempty(struct list **top){
  83. if(strt){
  84. if(!(*top))
  85. printf("is empty");
  86. else
  87. printf("not empty");
  88. }
  89. else
  90. printf("Work isn't started");
  91. getch();
  92. }
  93. void takelem(struct list **top){
  94. char c[20];
  95. if(strt){
  96. if((*top)!=NULL){
  97. strcpy(c,((*top)->value));
  98. (*top)=(*top)->next;
  99. printf("we take element=%s",c);
  100. cp--;
  101. }
  102. else
  103. printf("stack is empty");
  104. }
  105. else
  106. printf("Work isn't started");
  107. getch();
  108. }
  109. void chaghetop(struct list **top){
  110. char c[20];
  111. if(strt){
  112. if((*top)!=NULL){
  113. printf("input element\n");
  114. scanf("%s",&c);
  115. strcpy(((*top)->value),c);
  116. }
  117. else
  118. printf("stack is empty");
  119. }
  120. else
  121. printf("Work isn't started");
  122. getch();
  123. }
  124. void printlem(struct list **top){
  125. if(strt){
  126. if(*(top))
  127. printf("%s",(*top)->value);
  128. else
  129. printf("stack is empty");
  130. }
  131. else
  132. printf("Work isn't started");
  133. getch();
  134. }
  135. void push(struct list **top){
  136. struct list *list2=(struct list*)malloc(sizeof(struct list));
  137. char c[20];
  138. if(strt){
  139. if(cp<5){
  140. if(!list2){
  141. printf("memory trouble");
  142. exit;
  143. }
  144. printf("input word\n");
  145. scanf("%s",&c);
  146. strcpy((list2->value),c);
  147. list2->next=(*top);
  148. (*top)=list2;
  149. cp++;
  150. }
  151. else
  152. printf("stack is full");
  153. }\
  154. else
  155. printf("Work isn't started");
  156. getch();
  157. //printf("%c",list2->value);
  158. //getch();
  159. }
  160. void printList(struct list *top){
  161. if(strt){
  162. if(top!=NULL){
  163. while(top){
  164. printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",201,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,187);
  165. printf("\n%c",186);
  166. printf("%17s",(top)->value);
  167. (top)=(top)->next;
  168. printf("%c\n",186);
  169. printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",200,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,205,188);
  170.  
  171. }
  172. }
  173. else
  174. printf("stack is empty");
  175. }
  176. else
  177. printf("Work isn't started");
  178. printf("\n");
  179. //getch();
  180. }
  181. void deleteList(struct list **top) {
  182. struct list *prev = NULL;
  183. if(strt){
  184. if((*top)!=NULL){
  185. while ((*top)) {
  186. prev = (*top);
  187. (*top) = (*top)->next;
  188.  
  189. free(prev);
  190. free(*top);
  191. }
  192. cp=0;
  193. }
  194. else
  195. printf("stack is empty already");
  196. }
  197. else
  198. printf("Work isn't started");
  199. getch();
  200. }
  201. void menu()
  202. {
  203. int fail_safe;
  204. int input = -1;
  205. struct list *list1=NULL;
  206.  
  207. do
  208. {
  209. system("cls");
  210.  
  211. if (strt)
  212. {
  213. printf("<+---- Stack ----+>\n");
  214. printList(list1);
  215. }
  216.  
  217. printf("<+---- Menu ----+>\n");
  218. printf(" 0) Exit.\n");
  219. if(!strt)
  220. printf(" 1) Start work with stack.\n");
  221. printf(" 2) Make stack empty.\n");
  222. printf(" 3) empty/not empty?\n");
  223. printf(" 4) Print top.\n");
  224. printf(" 5) Delete top.\n");
  225. printf(" 6) Take element.\n");
  226. printf(" 7) Change top.\n");
  227. printf(" 8) Add elemnt.\n");
  228. printf(" 9) Print stack.\n");
  229. printf("10) End.\n");
  230.  
  231. printf("<+---- Enter a number from 1 to 10 ----+>\n");
  232. printf("> ");
  233. fail_safe = scanf("%d", &input);
  234. fflush(stdin);
  235.  
  236. if (fail_safe != 1)
  237. {
  238. printf("Incorrect input.\n");
  239. getch();
  240. continue;
  241. }
  242.  
  243. switch (input)
  244. {
  245. case 1:
  246. start(&list1);
  247. break;
  248. case 2:
  249. deleteList(&list1);
  250. break;
  251. case 3:
  252. isempty(&list1);
  253. break;
  254. case 4:
  255. printlem(&list1);
  256. break;
  257. case 5:
  258. pop(&list1);
  259. break;
  260. case 6:
  261. takelem(&list1);
  262. break;
  263. case 7:
  264. chaghetop(&list1);
  265. break;
  266. case 8:
  267. push(&list1);
  268. break;
  269. case 9:{
  270. printList(list1);getch();
  271. break;
  272. }
  273. case 10:
  274. end(&list1);
  275. break;
  276. default:
  277. break;
  278. }
  279. } while ( input != 0 );
  280. }
  281. int main(int argc, char* argv[])
  282. {
  283. menu();
  284.  
  285. return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement