Advertisement
romaine876

code

Mar 5th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node
  5. {
  6. private:
  7. int data;
  8. Node *nextnode;
  9. public:
  10. Node()
  11. {
  12. data = 0;
  13. nextnode = NULL;
  14. }
  15. int getdata()
  16. {
  17. return data;
  18. }
  19. void setdata(int x)
  20. {
  21. data = x;
  22. }
  23. Node *getnextnode()
  24. {
  25. return nextnode;
  26. }
  27. void setnextnode(Node *x)
  28. {
  29. nextnode = x;
  30. }
  31. };
  32. class linkedlist
  33. {
  34. private:
  35. Node *head;
  36. public:
  37. linkedlist()
  38. {
  39. head = NULL;
  40. }
  41. void insert(int x)
  42. {
  43. Node *temp = new Node();
  44. //temp = head;
  45. temp->setnextnode(NULL);
  46. if (temp != NULL)
  47. {
  48. temp->setdata(x);
  49. temp->setnextnode(NULL);
  50. if (head == NULL)
  51. {
  52. head = temp;
  53. }
  54. else
  55. {
  56. temp->setnextnode(head);
  57. head = temp;
  58. }
  59. }
  60. else
  61. {
  62. cout << "out of memory;";
  63.  
  64. }
  65.  
  66. }
  67. void display()
  68. {
  69. Node *temp;
  70. temp = head;
  71. while (temp != NULL)
  72. {
  73. cout << temp->getdata() << endl;
  74. temp = temp->getnextnode();
  75. }
  76. }
  77. void search(int x)
  78. {
  79. Node *temp;
  80. bool check=false;
  81. temp = head;
  82. while (temp != NULL)
  83. {
  84. if (x == temp->getdata())
  85. {
  86. cout << "vaalue found"<<endl;
  87. check = true;
  88. }
  89. temp = temp->getnextnode();
  90.  
  91. }
  92. if (check==false)
  93. {
  94. cout << "value not found";
  95. }
  96. }
  97. };
  98. void main()
  99. {
  100. linkedlist *list = new linkedlist;
  101. list->insert(4);
  102. list->insert(6);
  103. list->insert(7);
  104. list->insert(4);
  105. list->insert(3);
  106. list->insert(7);
  107. list->insert(3);
  108. list->insert(8);
  109. list->insert(2);
  110. list->insert(12);
  111. list->display();
  112. list->search(7);
  113. system("pause");
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement