Advertisement
immuntasir

Linked List 3

Sep 6th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 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. void menu(node_t *head) {
  45. int n;
  46. printf("What operation do you want to execute?\n");
  47. printf("Press 1 to print all elements.\n");
  48. printf("Press 2 to enter an integer at the end of the list.\n");
  49. printf("Press 3 to enter an integer at the beginning of the list.\n");
  50.  
  51. printf("Press 0 to exit.\n");
  52. scanf("%d", &n);
  53. switch(n) {
  54. case 1:
  55. print(head);
  56. break;
  57. case 2:
  58. push_pichone(head);
  59. break;
  60. case 3:
  61. push_shamne(&head);
  62. break;
  63.  
  64. }
  65. if (n!=0) menu(head);
  66. }
  67. int main() {
  68. node_t *head = NULL;
  69. head = malloc(sizeof(node_t));
  70. int num;
  71. printf("Enter the first integer: ");
  72. scanf("%d", &num);
  73. head->val = num; // head er address e jei node ta ache tar val er man rakhlam num
  74. head->next = NULL; // porer address hishebe rakhlam NULL. orthat ekhanei shes
  75.  
  76. menu(head);
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement