Tuesday, December 28, 2021

single link list in cpp | oop in c++ single link list operations in cpp data structure | ds


#include<iostream>
#include<string.h>
#include<string>
using namespace std;
class node{
    public:
        int roll_no;
        string name;
        node *next;
};
class std_list{
    private:
        node *start,*current,*temp;
    public:
        std_list(void){
            start=NULL;
        }
        void insert(int,string);
        void insert_end(int,string);
        void delete_end();
        void count();
        void search(int);
        void display();
};

void std_list::delete_end(){
    node *previous;
    if(start==NULL){
        cout<<"List is empty"<<endl;
        return;
    }
    else
    {
        current=start;
        while (current->next!=NULL)
        {
            previous=current;
            current=current->next;
        }
        cout<<"Data of last student is "<<current->name<<endl;
        previous->next=NULL;
        delete current;
        cout<<"Data of last Student is Deleted"<<endl;
    }
}

void std_list::insert_end(int rn,string name){
    temp=new node;
    temp->name=name;
    temp->roll_no=rn;
    temp->next=NULL;
    if(start==NULL){
        start=temp;
    }
    else{
        current=start;
        while (current->next!=NULL)
        {
            current=current->next;
        }
    }
    current->next=temp;
   
}

void std_list::insert(int rn,string nm){
    temp=new node;
    temp->roll_no=rn;
    temp->name=nm;
    temp->next=NULL;
    if(start==NULL){
        start=temp;
    }
    else
    {
        current=start;
        while (current->next!=NULL)
        {
            current=current->next;
        }
        current->next=temp;
       
    }
}
// end of insert function

void std_list::display(){
    if(start==NULL){
        cout<<"List is empty";
        return;
    }
    else{
        int rec=1;
        current=start;
        while (current!=NULL)
        {
            cout<<"Student record of #"<<rec<<endl;
            cout<<"Name: "<<current->name<<endl;
            cout<<"Roll: "<<current->roll_no<<endl;
            current=current->next;
            rec++;
        }
       
    }

} //end of display function

void std_list::count(){
    node *temp;
    current=start;
    int n=0;
    while (current!=NULL)
    {
        n++;
        current=current->next;
    }
    cout<<"Student in this is school is "<<n<<endl;
   
} //end of count function

void std_list::search(int rn){
    node *temp;
    int f=0;
    current=start;
    while (current!=NULL)
    {
        if(current->roll_no==rn){
            f=1;
        }
        current=current->next;
    }

    if(f==1){
        cout<<"Student with the roll #"<<rn<<" is Exist in the school"<<endl;
    }
    else{
        cout<<"No Student with the roll #"<<rn<<endl;
    }
   
}
int main()
{
    std_list obj;
    // insert data
    obj.insert(14,"Zoha");
    obj.insert(11,"Zoha");

    // insert data at end
    obj.insert_end(22,"Zoha Ali");

    // dispaly data
    obj.display();
    // coutn data  
    obj.count();  
    // search data
    obj.search(22);
    // delete data at the end
    obj.delete_end();
    return 0;
}

No comments:

Post a Comment