Advertisement
thinhckhcmus

linklist_bassic

May 23rd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4. struct Node
  5. {
  6. int data;
  7. Node *next;
  8. };
  9. struct list
  10. {
  11. Node *head;
  12. Node *tail;
  13. };
  14. Node *createNode(int x)
  15. {
  16. Node *p = new Node;
  17. if (p == NULL)
  18. {
  19. return NULL;
  20. }
  21. else
  22. {
  23. p->data = x;
  24. p->next = NULL;
  25. }
  26. return p;
  27. }
  28. void createlist(list &l)
  29. {
  30. l.head = l.tail = NULL;
  31. }
  32. void andtail(list &l, int x)
  33. {
  34. Node *p = createNode(x);
  35. if (l.head == NULL)
  36. {
  37. l.head = l.tail = p;
  38. }
  39. else
  40. {
  41. l.tail->next = p;
  42. l.tail = p;
  43. }
  44. }
  45. void input(list &l, int &n)
  46. {
  47. cout << "nhap so node can tao: ";
  48. cin >> n;
  49. createlist(l);
  50. for (int i = 1; i <= n; i++)
  51. {
  52. int x;
  53. cout << "nhap vao data: ";
  54. cin >> x;
  55. andtail(l, x);
  56. }
  57. }
  58. void output(list l)
  59. {
  60. for (Node *p = l.head; p != NULL; p = p->next)
  61. {
  62. cout << " " << p->data;
  63. }
  64. }
  65. void main()
  66. {
  67. int n;
  68. int x;
  69. list(l);
  70. input(l, n);
  71. output(l);
  72. _getch();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement