Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node{
  5. int x;
  6. struct node *next;
  7. }NODE;
  8.  
  9. void printMenu() {
  10. printf("MENU:\n");
  11. printf("[1] Add number\n");
  12. printf("[2] Delete Number\n");
  13. printf("[3] Exit\n");
  14. printf("\n");
  15. }
  16.  
  17. void addNum(NODE **head){
  18. NODE * new_node;
  19. int value;
  20. printf("---Add Number---\n");
  21. printf("Enter Number: ");
  22. scanf("%d", &value);
  23. if(head==NULL){
  24. new_node = (NODE*)malloc(sizeof(NODE));
  25. new_node -> x = value; // Gawa ng bagong node
  26. new_node -> next = NULL;
  27. new_node -> next = *head;
  28. *head = new_node;
  29.  
  30. NODE *temp;
  31. temp = head;
  32. while(temp != NULL) {
  33. printf("%d\n", temp -> x);
  34. temp = temp -> next;
  35. }
  36. }else{
  37. if(value<new_node->x){
  38. new_node = (NODE*)malloc(sizeof(NODE));
  39. new_node -> x = value; // Gawa ng bagong node
  40. new_node -> next = NULL;
  41. new_node -> next = *head;
  42. *head = new_node;
  43. }else if(value>new_node->x){
  44. NODE *new_node, *last = NULL;
  45. last = *head;
  46. while(last->next!=NULL){
  47. // Finds the last node
  48. /* Habang hindi NULL ang next ng node,
  49. magpunta sa susunod na node */
  50. last = last->next;
  51. }
  52. // Ipapoint sa last node si new_node
  53. last->next = new_node;
  54. }
  55. }
  56. }
  57.  
  58. void delNum(NODE **head){
  59.  
  60. }
  61.  
  62. int main(){
  63. NODE *head = NULL, *temp, *new_node;
  64. int value, choice=0;
  65.  
  66. while(choice!=3){
  67. printMenu();
  68. printf("Enter choice: ");
  69. scanf("%d", &choice);
  70. getchar();
  71.  
  72. if(choice==1){
  73. addNum(&head);//pass by ref kaya address ni head
  74. }else if(choice==2){
  75. delNum(&head);
  76. }else if(choice==3){
  77. printf("Exit.\n");
  78. }else{
  79. printf("Choose from 1-3 only.");}
  80. }
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement