Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Node
- {
- private:
- int data;
- Node *nextnode;
- public:
- Node()
- {
- data = 0;
- nextnode = NULL;
- }
- int getdata()
- {
- return data;
- }
- void setdata(int x)
- {
- data = x;
- }
- Node *getnextnode()
- {
- return nextnode;
- }
- void setnextnode(Node *x)
- {
- nextnode = x;
- }
- };
- class linkedlist
- {
- private:
- Node *head;
- public:
- linkedlist()
- {
- head = NULL;
- }
- void insert(int x)
- {
- Node *temp = new Node();
- //temp = head;
- temp->setnextnode(NULL);
- if (temp != NULL)
- {
- temp->setdata(x);
- temp->setnextnode(NULL);
- if (head == NULL)
- {
- head = temp;
- }
- else
- {
- temp->setnextnode(head);
- head = temp;
- }
- }
- else
- {
- cout << "out of memory;";
- }
- }
- void display()
- {
- Node *temp;
- temp = head;
- while (temp != NULL)
- {
- cout << temp->getdata() << endl;
- temp = temp->getnextnode();
- }
- }
- void search(int x)
- {
- Node *temp;
- bool check=false;
- temp = head;
- while (temp != NULL)
- {
- if (x == temp->getdata())
- {
- cout << "vaalue found"<<endl;
- check = true;
- }
- temp = temp->getnextnode();
- }
- if (check==false)
- {
- cout << "value not found";
- }
- }
- };
- void main()
- {
- linkedlist *list = new linkedlist;
- list->insert(4);
- list->insert(6);
- list->insert(7);
- list->insert(4);
- list->insert(3);
- list->insert(7);
- list->insert(3);
- list->insert(8);
- list->insert(2);
- list->insert(12);
- list->display();
- list->search(7);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement