jaOjaa

MOTOR

Mar 25th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6.  
  7. void clrscr() {
  8. system("@cls||clear");
  9. }
  10.  
  11. struct node {
  12. int qty;
  13. char name[20];
  14. int price;
  15. struct node *next;
  16. };
  17.  
  18. struct node *head = NULL;
  19.  
  20. struct node *create(struct node *head) {
  21. struct node *new_node, *poin;
  22. char name[20];
  23. int qty, harga,var=0;
  24. do{
  25. printf("Input name of motorcycle's part [3...30] \t: ");
  26. fflush(stdin);
  27. scanf("%[^\n]",&name);
  28. } while (strlen(name)<3 || strlen(name)>30);
  29. do{
  30. printf("Input quantity of motorcycle's \t\t\t: ");
  31. scanf("%d",&qty);
  32. }while (qty<1 || qty>20);
  33. printf("Input price of motorcycle's part [$1...$1000] \t: ");
  34. scanf("%d",&harga);
  35.  
  36. new_node = (struct node*)malloc(sizeof(struct node));
  37. strcpy(new_node->name,name);
  38. new_node->qty=qty;
  39. new_node->price=harga;
  40. if (head==NULL) {
  41. new_node->next=new_node;
  42. head = new_node;
  43. }
  44.  
  45. return head;
  46. }
  47.  
  48. struct node *push(struct node *head) {
  49. struct node *new_node, *poin;
  50. char name[20];
  51. int qty, harga,var=0;
  52. do{
  53. printf("Input name of motorcycle's part [3...30] \t: ");
  54. fflush(stdin);
  55. scanf("%[^\n]",&name);
  56. } while (strlen(name)<3 || strlen(name)>30);
  57. do{
  58. printf("Input quantity of motorcycle's \t\t\t: ");
  59. scanf("%d",&qty);
  60. }while (qty<1 || qty>20);
  61. printf("Input price of motorcycle's part [$1...$1000] \t: ");
  62. scanf("%d",&harga);
  63.  
  64. new_node = (struct node*)malloc(sizeof(struct node));
  65. strcpy(new_node->name,name);
  66. new_node->qty=qty;
  67. new_node->price=harga;
  68.  
  69. new_node->next=new_node;
  70. poin=head;
  71. while (poin->next!=head) {
  72. poin=poin->next;
  73. }
  74. poin->next=new_node;
  75. new_node->next=head;
  76. head = new_node;
  77.  
  78. return head;
  79. }
  80.  
  81. struct node *print(struct node *head) {
  82. struct node *poin;
  83. int no=1;
  84. printf("\t\t\t\t ----- Item List -----\n\n");
  85. printf("-------+--------------+-----------+-----------+\n");
  86. printf("| No. | Name \t\t\t | Quantity | Price($) |\n");
  87. printf("-------+--------------+-----------+-----------+\n");
  88. poin = head;
  89. if (poin->next!=head) {
  90. printf("|");
  91. printf("% -5d |",no);
  92. printf("% -25s |",poin->name);
  93. printf("% -10d |",poin->qty);
  94. printf("% -10d |\n",poin->price);
  95. poin = poin->next;
  96. no++;
  97. }
  98. else {
  99. printf("|");
  100. printf("% -5d |",no);
  101. printf("% -25s |",poin->name);
  102. printf("% -10d |",poin->qty);
  103. printf("% -10d |\n",poin->price);
  104. poin = poin->next;
  105. }
  106. printf("-------+--------------+-----------+-----------+\n");
  107.  
  108. }
  109.  
  110. struct node *pop(struct node *head) {
  111. struct node *poin, *ptr;
  112. poin = head;
  113. ptr = head;
  114. while (poin->next!=head) {
  115. poin=poin->next;
  116. }
  117. if (poin->next!=poin) {
  118. poin->next=head->next;
  119. head = poin->next;
  120. free(ptr);
  121. }
  122. else {
  123. poin->next=NULL;
  124. head=NULL;
  125. free(ptr);
  126. }
  127. return head;
  128. }
  129.  
  130. int main() {
  131. int pil;
  132. struct node *poin;
  133. do {
  134. clrscr();
  135. printf("RED MOTORCYCLE PARTS\n");
  136. printf("+++++++++++++++++++++++++++++++\n\n");
  137. printf("1. Item List\n");
  138. printf("2. Add <Push> New Item\n");
  139. printf("3. Delete <Pop> Recently added item\n");
  140. printf("4. Exit\n\n");
  141. do {
  142. printf("Input Your Choice : ");
  143. scanf("%d",&pil);
  144. }while (pil<1 || pil>4);
  145. switch(pil) {
  146. case 1 : {
  147. clrscr();
  148. if (head==NULL) {
  149. printf("\t\t\t\t ----- Item List -----\n\n");
  150. printf("-------+--------------+-----------+-----------+\n");
  151. printf("| No. | Name \t\t | Quantity | Price($) |\n");
  152. printf("-------+--------------+-----------+-----------+\n");
  153. printf("|\t\t\t!!!No data Available!!!\t\t\t\t\t |\n");
  154. printf("-------+--------------+-----------+-----------+\n");
  155. }
  156. else {
  157. print(head);
  158. }
  159. getch();
  160. break;
  161. }
  162. case 2 : { clrscr();
  163. if (head==NULL) {
  164. head = create(head);
  165. }
  166. else {
  167. head = push(head);
  168. }
  169. printf("\n\n---Add Item Success---");
  170. getch();
  171. break;
  172. }
  173. case 3 :{ clrscr();
  174. if (head==NULL) {
  175. printf("\n\n\t\t-----There are no item available-----");
  176. getch();
  177. }
  178. else {
  179. head = pop(head);
  180. printf("\n\n\t\t ---One Stack has been removed---");
  181. getch();
  182. }
  183. break;
  184. }
  185. case 4 : {
  186. clrscr();
  187. printf("\n\n\t\t---This Program Has Finished---");
  188. free(head);
  189. break;
  190. }
  191. }
  192. }while (pil!=4);
  193. }
Advertisement
Add Comment
Please, Sign In to add comment