Advertisement
immuntasir

Linked List 2

Sep 6th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 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.  
  10. while (current != NULL) {
  11. printf("%d\n", current->val);
  12. current = current->next;
  13. }
  14. }
  15. void push_pichone(node_t * head) {
  16. printf("Enter the integer: ");
  17. int n;
  18. scanf("%d", &n);
  19. node_t * current = head;
  20. // list er shes element ta khuje ber korbo
  21. while (current->next != NULL) {
  22. current = current->next;
  23. }
  24.  
  25. // ebar amra variable add korbo
  26. current->next = malloc(sizeof(node_t)); // malloc use kora khub e important
  27. current->next->val = n;
  28. current->next->next = NULL;
  29. }
  30. void menu(node_t *head) {
  31. int n;
  32. printf("What operation do you want to execute?\n");
  33. printf("Press 1 to print all elements.\n");
  34. printf("Press 2 to enter an integer at the end of the list.\n");
  35. printf("Press 0 to exit.\n");
  36. scanf("%d", &n);
  37. switch(n) {
  38. case 1:
  39. print(head);
  40. break;
  41. case 2:
  42. push_pichone(head);
  43. break;
  44. }
  45. if (n!=0) menu(head);
  46. }
  47. int main() {
  48. node_t *head = NULL;
  49. head = malloc(sizeof(node_t));
  50. int num;
  51. printf("Enter the first integer: ");
  52. scanf("%d", &num);
  53. head->val = num; // head er address e jei node ta ache tar val er man rakhlam num
  54. head->next = NULL; // porer address hishebe rakhlam NULL. orthat ekhanei shes
  55.  
  56. menu(head);
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement