Advertisement
huutho_96

DSLK đơn

Mar 25th, 2015
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct Node
  4. {
  5. int x;
  6. Node *pNext;
  7. };
  8. struct List
  9. {
  10. Node *pHead, *pTail;
  11. };
  12. void CreateList(List &L)
  13. {
  14. L.pHead = L.pTail = NULL;
  15. }
  16. Node *CreateNode(int x)
  17. {
  18. Node *p = new Node;
  19. if (p == NULL) exit(1);
  20. p->x = x;
  21. p->pNext = NULL;
  22. return p;
  23. }
  24. void AddTail(List &L, Node *p)
  25. {
  26. if (L.pHead == NULL)
  27. {
  28. L.pHead = L.pTail = p;
  29. }
  30. else
  31. {
  32. L.pTail->pNext = p;
  33. L.pTail = p;
  34. }
  35. }
  36. void Input(List &L)
  37. {
  38. Node *p;
  39. int x;
  40. do
  41. {
  42. cout << " nhap x(0 de ket thuc): ";
  43. cin >> x;
  44. if (x == 0) break;
  45. p = CreateNode(x);
  46. AddTail(L, p);
  47. } while (1);
  48. }
  49. void Output(List L)
  50. {
  51. Node *p = L.pHead;
  52. while (p != NULL)
  53. {
  54. cout << p->x << endl;
  55. p = p->pNext;
  56. }
  57. }
  58. void main()
  59. {
  60. List L;
  61. CreateList(L);
  62. Input(L);
  63. cout << endl;
  64. Output(L);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement