immuntasir

Linked List 4

Sep 6th, 2015
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct node {
  4. int val;
  5. struct node * next;
  6. } node_t;
  7. void print(node_t * head) {
  8. node_t * current = head;
  9. printf("***The list begins***\n");
  10. while (current != NULL) {
  11. printf("%d\n", current->val);
  12. current = current->next;
  13. }
  14. printf("***The list ends***\n");
  15. }
  16. void push_pichone(node_t * head) {
  17. printf("Enter the integer: ");
  18. int n;
  19. scanf("%d", &n);
  20. node_t * current = head;
  21. // list er shes element ta khuje ber korbo
  22. while (current->next != NULL) {
  23. current = current->next;
  24. }
  25.  
  26. // ebar amra variable add korbo
  27. current->next = malloc(sizeof(node_t)); // malloc use kora khub e important
  28. current->next->val = n;
  29. current->next->next = NULL;
  30. }
  31. void push_shamne(node_t ** head) {
  32. printf("Enter the integer: ");
  33. int n;
  34. scanf("%d", &n);
  35. // notun node
  36. node_t * new_node;
  37. new_node = malloc(sizeof(node_t));
  38. // node er value
  39. new_node->val = n;
  40. new_node->next = *head;
  41. // head er value bodle dicchi
  42. *head = new_node;
  43. }
  44. int pop_shamne(node_t ** head) {
  45. int ret = -1;
  46. node_t * next_node = NULL;
  47. // eta korchi jaate
  48. if (*head == NULL) {
  49. return -1;
  50. }
  51.  
  52. next_node = (*head)->next; // head variable er next address ta rakhtichi save kore
  53. ret = (*head)->val; // value ta save kore rakhtichi jaate pore return korte pari
  54. free(*head); // memory free kore dicchi
  55. *head = next_node; // head hishebe shei purono store kora address ta set kortichi
  56.  
  57. return ret;
  58. }
  59. void menu(node_t *head) {
  60. int n;
  61. printf("What operation do you want to execute?\n");
  62. printf("Press 1 to print all elements.\n");
  63. printf("Press 2 to enter an integer at the end of the list.\n");
  64. printf("Press 3 to enter an integer at the beginning of the list.\n");
  65. printf("Press 4 to remove the first item.\n");
  66. printf("Press 0 to exit.\n");
  67. scanf("%d", &n);
  68. switch(n) {
  69. case 1:
  70. print(head);
  71. break;
  72. case 2:
  73. push_pichone(head);
  74. break;
  75. case 3:
  76. push_shamne(&head);
  77. break;
  78. case 4:
  79. printf("The removed element: %d\n", pop_shamne(&head));
  80. break;
  81. }
  82. if (n!=0) menu(head);
  83. }
  84. int main() {
  85. node_t *head = NULL;
  86. head = malloc(sizeof(node_t));
  87. int num;
  88. printf("Enter the first integer: ");
  89. scanf("%d", &num);
  90. head->val = num; // head er address e jei node ta ache tar val er man rakhlam num
  91. head->next = NULL; // porer address hishebe rakhlam NULL. orthat ekhanei shes
  92.  
  93. menu(head);
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment