Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class node;
- node* head=NULL;
- //node* ptr=head;
- class node
- {
- private:
- int data;
- public:
- void setvalue(int x)
- {
- data=x;
- }
- int getvalue()
- {
- return data;
- }
- node* ptrnext;
- void display()
- {
- node* ptr=head;
- while(ptr!=NULL)
- {
- cout<<ptr->data<<" ";
- ptr=ptr->ptrnext;
- }
- cout<<endl<<endl;
- }
- ////////////////////////////////////////////////////////////
- void search_item(int item)
- {
- node* ptr=head;
- while(ptr->data != item && ptr-> ptrnext !=NULL)
- ptr = ptr->ptrnext;
- if(ptr->data==item)
- cout<<item<<" "<<"Found"<<endl;
- else
- cout<<item<<" "<<"Not Found"<<endl;
- }
- ///////////////////////////////////////////////////////////
- void ensert_end_stack(int k,node* newn)
- {
- newn->data=k;
- newn->ptrnext=NULL;
- }
- /////////////////////////////////////////////////////////
- void delete_end_stack()
- {
- node* ptr=head;
- node* ptr2;
- while(ptr->ptrnext->ptrnext!=NULL)
- ptr=ptr->ptrnext;
- ptr2=ptr->ptrnext;
- ptr->ptrnext = NULL;
- }
- };
- int main()
- {
- node n1,n2,n3,n4;
- n1.setvalue(10);
- n2.setvalue(20);
- n3.setvalue(30);
- head=&n1;
- n1.ptrnext=&n2;
- n2.ptrnext=&n3;
- n3.ptrnext=NULL;
- cout<<"Existing Linked List : ";
- n1.display();
- int search_value;
- cout<<"Insert data to search : ";
- cin>>search_value;
- cout<<endl;
- n1.search_item(search_value);
- cout<<endl;
- int add_value;
- node* newnode;
- newnode=&n4;
- cout<<"Insert data to add : ";
- cin>>add_value;
- cout<<endl;
- n3.ptrnext=&n4;
- n4.ensert_end_stack(add_value,newnode);
- cout<<"After insertion : ";
- n4.display();
- cout<<endl;
- n4.delete_end_stack();
- cout<<"After Deletion : ";
- n1.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement