#include <iostream> using namespace std; class SLLIST; class node { private: int ele; node *addrs; public: friend class SLLIST; }; class SLLIST { private: node *head; public: SLLIST() { head='\0'; } void insert_beg(); void insert_end(); void display(); bool search(int); }; void SLLIST::insert_beg() { node *temp=new node(); cout<<"enter element:"<<endl; cin>>temp->ele; temp->addrs='\0'; if (head=='\0') head=temp; else { node *t=new node(); t=head; temp->addrs=t; } } void SLLIST::insert_end() { node *temp=new node(); cout<<"enter element:"<<endl; cin>>temp->ele; temp->addrs='\0'; if (head='\0') head=temp; else { node *t=new node(); t=head; while(t->addrs!='\0') t=t->addrs; t->addrs=temp; } } void SLLIST::display() { node *t=new node(); t=head; if (head=='\0') cout<<"list is empty"<<endl; else { while(t->addrs!='\0') { cout<<t->ele<<endl; t=t->addrs; } cout<<t->ele<<endl; } } bool SLLIST::search(int key) { node *t=new node(); t=head; if(head=='\0') return false; else if((t->addrs=='\0')&&(t->ele==key)) return true; else { while(t->addrs!='\0') { if(key==t->ele) return true; else t=t->addrs; } return false; } } int main() { SLLIST s; s.insert_beg(); s.insert_beg(); s.insert_end(); char ch=s.search(56); if (ch=="true") cout<<"key found"; else cout<<"key not found"; s.display(); return 0; }
main
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)