Skip to main content

Single linklist program in c++(only inserting and display function)

#include<iostream>
using namespace std;

struct node{
int data;
node *next;
};

class sll{
private:
         node *head,*tail;

public:
        sll(){
        head=0;
        tail=0;
        }

        insert(int mydata){
        node *tmp=new node;
        tmp->data=mydata;
        tmp->next=0;
         int select;
        cout<<"Select one of this option\n";
        cout<<"1.insert element at first position\n";
        cout<<"2.insert element at Any position\n";
        cout<<"3.insert element at last position\n";
        cin>>select;
            switch(select){
        case 1:if(head==0){
                    head=tmp;
                    tail=tmp;
                }else{
                tmp->next=head;
                head=tmp;
                }
               break;

        case 2:int cont;
               if(head==0 && tail==0){
                 cout<<"please enter the no. at begin or end first\n";
                 }
                  else{
                        int inst;
                        tail=head;
                        cout<<"Enter the element after you want inserting\n";
                        cin>>inst;
                   while(tail->data!=inst)
                {
                    cont=1;
                    tail=tail->next;
                }
                tmp->next=tail->next;
                tail->next=tmp;
                }
                if(cont==0){
                 cout<<"plzzz enter correct element\n";
                }
               break;


        case 3:if(tail==0){
                 tail=tmp;
                 head=tmp;
                }else{
                while(tail->next!=0)
                {
                    tail=tail->next;
                }
                tail->next=tmp;
                tmp->next=0;
                }
                break;

     default : cout<<"wrong output\n";

            }
        }


      void display()
    {
        node *tmp=head;
        if(tmp == NULL)
        {
            cout << "NULL" << endl;
        }
        else
        {
            while(tmp!=0){
             cout << tmp->data << endl;
             tmp=tmp->next;
        }
        }
    }


};

int main()
{
   sll a;
   char again;
   int select,input;
   do{
   cout<<"Enter the given inputs \n";
   cout<<"1.Insert the input\n";
   cout<<"2.display element\n";
   cin>>select;
   switch(select){

   case 1:cout<<"Enter the element\n";
          cin>>input;
          a.insert(input);
          break;

  case 2:cout<<"Your outputs\n";
        a.display();
        break;

default:cout<<"your select wrong output\n";
  }
   cout<<"Do you want to run again yes press y or Y (or) no\n";
   cin>>again;

   }while(again=='y'||again=='Y');

   return 0;
}

Comments

Popular posts from this blog

Maximum possible number of monsters you can defeat?

 Q).Maximum possible number of monsters you can defeat?  =>Introduction: While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are n monsters you'll need to defeat in this quest. Each monster i is described with two integer numbers - poweri and bonusi. To defeat this monster, you'll need at least poweri experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain bonusi experience points if you defeat this monster. You can defeat monsters in any order. The quest turned out to be very hard - you try to defeat the monsters but keep losing repeatedly. Your friend told you that this quest is impossible to complete. Knowing that, you're interested, what is the maximum possible number of monsters you can defeat? (Question difficulty level: Hardest) => Input: The first line contains an integer, n, denoting the number of monsters. The next line contains an

Count Frequency of Character in Any Sentence using C++

 Q)Write the C++ program for character frequency count for any sentence => Program : #include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ string mystr; cout<<"Enter the Any Sentence  for character frequency count \n"; getline(cin,mystr); int charFreq[26]={0}; for(int i=0;i<mystr.length();i++){     int index=mystr[i]-'a';     charFreq[index]++; } cout<<"\n****************************************\n"; cout<<"\nSentence : "<<mystr<<endl<<endl; cout<<setw(12)<<"Character"<<setw(12)<<"Frequency"<<endl<<endl; for(int i=0;i<26;i++){     if(charFreq[i]!=0){         cout<<setw(6)<<char(i+'a')<<setw(10)<<charFreq[i]<<" times \n";     } } } =>Output :