Monday, 28 November 2016

Doubly Circular linkedlist


In this program i used to pointer first and last its create less complexity. It also done by one pointer but it is difficult to handle

Header File
#ifndef _DOUBLY_CIRCULAR_LINKEDLIST_
#define _DOUBLY_CIRCULAR_LINKEDLIST_
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *prev,*next;
};
class dclinkedlist
{
private:
Node *first,*last;
public:
dclinkedlist();
void insetfirst(int);
void print();
bool isEmpty();
void deletefirst();
void append(int );
void deletelast();
};


#endif

Cpp file
#include "doublycircularlinkedlist.h"
dclinkedlist::dclinkedlist()
{
first=last=NULL;
}
void dclinkedlist::insetfirst(int val)
{
Node *temp=new Node;
temp->data=val;
if (first==NULL)
{
  first=last=temp;
  temp->next=temp->prev=first;
}
else
{
temp->next=first;
temp->prev=last;
last->next=temp;
first->prev=temp;
first=temp;
}
}
void dclinkedlist::print()
{
if(isEmpty())
{
cout <<"No any node to display "<<endl;
}
else
{
Node *ptr=first;
do

{
cout <<ptr->data<<endl;
ptr=ptr->next;
}while (ptr!=last->next);
}
}
bool dclinkedlist::isEmpty()
{
return first==NULL;
};

void dclinkedlist::deletefirst()
{
if (isEmpty())
{
cout <<"No node for delete "<<endl;
}
else if (first==last)
{
first=last=NULL;
}
else
{
Node *temp=first;
first=first->next;
first->prev=last;
last->next=first;
delete temp;
}
}
void dclinkedlist::append(int val)
{
if (isEmpty())
{
insetfirst(val);
}else
{
Node *temp=new Node;
temp->data=val;
temp->next=first;
temp->prev=last;
last->next=temp;
last=last->next;
}
}
void dclinkedlist::deletelast()
{
if (isEmpty())
{
cout <<"No Any Node to delete "<<endl;
}else if (first==last)
{
deletefirst();
}
else
{
Node *temp=last;
last=last->prev;
last->next=first;
delete temp;
}
}

Main function
#include "doublycircularlinkedlist.h"
int main ()
{
dclinkedlist dc;
cout <<"inset first "<<endl;
        dc.insetfirst(10);
dc.insetfirst(36);
dc.insetfirst(70);
dc.insetfirst(88);
dc.print();
cout <<"delte first "<<endl;
dc.deletefirst();
dc.print();
cout <<"append fucntion "<<endl;
dc.append(99);
dc.print();
cout <<"Dele last function"<<endl;
dc.deletelast();
dc.print();
return 0 ;
}

Copy constructor singly linkedlist

Working copy constructor for singly linked list




linkedlist::linkedlist(const linkedlist &l)
{
first=NULL;
if (l.first!=NULL)

{
Node *ptr=l.first;

while (ptr!=NULL)
{


append(ptr->data);
ptr=ptr->next;
}
}
}

Saturday, 26 November 2016

Circular Linked list


Header file

#ifndef _CIRCULARLINKEDLIST_
#define _CIRCULARLINKEDLIST_
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class circularLinkedlist
{
private:
Node *last;
public:
circularLinkedlist();
circularLinkedlist(const circularLinkedlist &);
void insertfirst(int);
void deletefirst();
void append(int);
void deletelast();
bool isEmpty();
void print();
void makeEmpty();
~circularLinkedlist();
};
#endif


Cpp File

#include "CircularLinkedList.h"
circularLinkedlist::circularLinkedlist()
{
last=NULL;
}
void circularLinkedlist::insertfirst(int val)
{
Node *temp=new Node;
temp->data=val;
if (isEmpty())
{
last=temp;
temp->next=last;
}else
{
temp->next=last->next;
last->next=temp;

}

}
void circularLinkedlist::print()
{
if(isEmpty())
{
cout<<"No any value in linkedlist "<<endl;
}else
{
Node *ptr=last->next;
do
{
cout <<ptr->data<<endl;
ptr=ptr->next;
}while (ptr!=last->next);
}
}
bool circularLinkedlist::isEmpty()
{
return last==NULL;
}
void circularLinkedlist::deletefirst()
{
if (isEmpty())
{
cout <<"Their is no any node to delete "<<endl;
}else
{
Node *ptr=last->next;
last->next=ptr->next;
if (ptr==last)
{
last=NULL;
}
delete ptr;
}
}
void circularLinkedlist::append(int val)
{
if (isEmpty())
{
insertfirst(val);
}else
{
Node *temp=new Node;
temp->data=val;
temp->next=last->next;
last->next=temp;
last=last->next;
}
}
void circularLinkedlist::deletelast()
{
Node *prev=last->next,*curr=last->next;
if (isEmpty())
{
cout <<"Linkedlist is empty "<<endl;
}else if (curr==last)
{
deletefirst();

}else
{
do
{
prev=curr;
curr=curr->next;
}while (curr!=last);
prev->next=last->next;
last=prev;
delete curr;
}
}
circularLinkedlist::circularLinkedlist(const circularLinkedlist & c)
{
if (c.last==NULL)
{
last=NULL;
}else
{
Node *ptr=c.last->next;
do
{
append(ptr->data);
ptr=ptr->next;
}while (ptr!=last);
}
}
void circularLinkedlist::makeEmpty()
{
if (!isEmpty())
{
Node *ptr=last->next,*del=last->next;
do
{
del=ptr;
ptr=ptr->next;
delete del;
}while (ptr!=last);
ptr=last=NULL;
}
}
circularLinkedlist::~circularLinkedlist()
{
makeEmpty();
}

Main
#include "CircularLinkedList.h"
int main ()
{
circularLinkedlist c;
c.insertfirst(89);
    c.insertfirst(75);
c.insertfirst(100);
c.print();
cout <<"After first deletion "<<endl<<endl;
c.deletefirst();
c.print();
cout <<"Append "<<endl<<endl;
c.append(56);
c.print();
cout <<"after last delition "<<endl<<endl;
c.deletelast();
c.deletelast();
c.print();
cout <<"After calling make empty "<<endl;
c.makeEmpty();
c.print();
return 0;
}

Double Linked list


Header file
#ifndef _DOUBLELINKEDLIST_
#define _DOUBLELINKEDLIST_
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *prev,*next;
};
class doublelinkedlist
{
private:
Node *first;
public:
doublelinkedlist();
void insetfirst(int);
void deletefirst();
void print();
bool isEmty();
void makeEmpty();
~doublelinkedlist();
void deletelast();
void append(int);
};
#endif

Cpp File

#include "doublelinkedlist.h"
doublelinkedlist::doublelinkedlist()
{
first=NULL;
}
void doublelinkedlist::insetfirst(int val)
{
Node *temp=new Node;
temp->data=val;
if (isEmty())
{
temp->next=NULL;
temp->prev=NULL;
first=temp;
}else
{
temp->prev=NULL;
temp->next=first;
first->prev=temp;
first=temp;
}
}
void doublelinkedlist::print()
{
if (isEmty())
{
cout <<"their is no node to display"<<endl;
}else
{
Node *temp=first;
while (temp!=NULL)
{
cout <<temp->data<<endl;
temp=temp->next;
}
}

}
bool doublelinkedlist::isEmty()
{
return first==NULL;
}
void doublelinkedlist::makeEmpty()
{
if (!isEmty())
{
Node *temp;
while (first!=NULL)
{
temp=first;
first=first->next;
delete temp;
}

}
}
doublelinkedlist::~doublelinkedlist()
{
makeEmpty();
}
void doublelinkedlist::deletefirst()
{
if (isEmty())
{
cout <<"Their is no node to delete "<<endl;
}
else if (first->next==NULL)
{
first=NULL;
}else
{
Node *temp=first;
first=temp->next;
first->prev=NULL;
delete temp;
}
}
void doublelinkedlist::deletelast()
{
if (!isEmty())
{
Node *temp=first;
while(temp->next!=NULL)
{
temp=temp->next;
}
if (first==temp)
{
deletefirst();
}else
{
temp->prev->next=NULL;
delete temp;
}
}
}
void doublelinkedlist::append(int val)
{
if (isEmty())
{
insetfirst(val);
}else
{
Node *temp=new Node,*ptr=first;
while (ptr->next!=NULL)
{
ptr=ptr->next;
}
temp->data=val;
ptr->next=temp;
temp->prev=ptr;
temp->next=NULL;
}
}


Main

#include "doublelinkedlist.h"
int main ()
{
doublelinkedlist d;
cout <<"Inset first "<<endl;
d.insetfirst(0);
d.insetfirst(1);
d.insetfirst(2);
d.print();
cout <<"calling make Empty "<<endl;
d.makeEmpty();
d.print();
cout <<"delete first "<<endl;
d.insetfirst(0);
d.insetfirst(1);
d.insetfirst(2);
cout <<"Before delete  "<<endl;
d.print();
cout <<"After delete  "<<endl;
d.deletefirst();
d.print();
cout <<"Delete last "<<endl;
d.deletelast();
d.print();
cout <<"Using append function "<<endl;
d.append(7);
d.print();
return 0;
}

Friday, 25 November 2016

Bracket Balance Check


Use Stack class for this Application

#include <iostream>
using namespace std;
#include "Stack.h"
int main ()
{
Stack s;
char expression[]="[36{(25*1569)}(2+5)]";
for (int i=0; expression[i]!='\0'; i++)
{
if (expression[i]=='('|| expression[i]=='[' || expression[i]=='{')
{
s.push(expression[i]);
}else if (expression[i]==')'|| expression[i]==']' || expression[i]=='}')
{
if (s.isEmpty())
{
cout <<"Bracket are not balance "<<endl;
return 0;
}
else
{
   s.pop();
}
}

}
if (s.isEmpty())
{
cout <<"Brackets are balance "<<endl;
}else
{
cout <<"Bracket are not balance"<<endl;
}
return 0;
}

Number Guessing Game






This is Small Game.
In this game computer guess any number between 1 to 10. User has 3 chance to guess that number.
when game over. user have a choice to play again or quit.
Program

#include <iostream>
using namespace std;
#include <ctime>
#include <cstdlib>

int main ()
{
int secret,i,chance,guess;
char answer;
srand(time(0));

do {
secret=rand()%11;
i=0;
chance=1;
do{
cout <<"Enter any number betwwen 1-10"<<endl;
cin >> guess;
while (guess>10 || guess<0){
cout <<"YOU Enter invalid guess which is out of range enter number between 1-10: ";
cin >>guess;}
if (guess<secret){
cout <<"sorry you enter very small value" <<endl;
}else if (guess>secret){
cout <<"sorry you enter very large value" <<endl;
}else {
cout <<"congratulation you enter right answer"<<endl;
i++;
}
chance++;
if (chance==4){
cout <<"Sorry you have no any chance left correct answer is " <<secret <<endl;
}

}while (chance!=4 && i!=1);
cout <<"Do you want to play again [y/n]" <<endl;
cin >>answer;
while (answer!='Y'&& answer!='y' && answer!='n' && answer!='N')
               {
cout <<"YOu enter in walid character please enter y/n" <<endl;
cin >>answer;
}

}while (answer=='Y' || answer=='y');
cout <<"Thanks for playing" <<endl;
cout <<"Have a nice day" <<endl;
return 0;
}



Thursday, 24 November 2016

Hollow Diamond



#include <iostream>
using namespace std;
int main ()
{
    int space,line,rows,start;
    cout <<"Enter number of rows want display  " <<endl;
    cin >>rows;
    for (line=1; line<=rows; line=line+2){
        for (space=1; space<=rows-line; space=space+2){
            cout <<' ';
        }for (start=1; start<=line; start++){
            if (start==1){
                cout <<"*";}
            else if (start==line){
                cout <<"*";
            }else {
                cout <<" ";}
        }
        cout <<endl;

    }
    int row;
    row=rows-2;
    for (line=row; line>=1; line=line-2){
        for (space=1; space<=rows-line; space=space+2){
            cout <<' ';
        }for (start=1; start<=line; start++){
            if (start==1){
                cout <<"*";}
            else if (start==line){
                cout <<"*";
            }else {
                cout <<" ";
        }
        }
        cout <<endl;

        }
   
    return 0;
}

Star Pattern Diamond



 this program help you in creating different star pattern.
diamond created when user enter odd number not even
#include <iostream>
using namespace std;
int main ()
{
    int space,line,rows,start;
    cout <<"Enter number of rows want display  " <<endl;
    cin >>rows;
    while (rows%2==0)
    {
        cout <<"rwos number  must be odd re enter number of rows"<<endl;
        cin >>rows;
    }
    for (line=1; line<=rows; line=line+2){
        for (space=1; space<=rows-line; space=space+2){
            cout <<' ';
        }for (start=1; start<=line; start++){
            cout <<"*";
        }
        cout <<endl;

}
    int row;
    row=rows-2;
    for (line=row; line>=1; line=line-2){
        for (space=1; space<=rows-line; space=space+2){
            cout <<' ';
        }for (start=1; start<=line; start++){
            cout <<"*";
        }
        cout <<endl;

}
   
    return 0;
}

Saturday, 19 November 2016

Split singly linked list into two



  
This Function split singly linked list in to two linked list In this program some data store in linked list. Function used two parameters one is break point and other is 2nd linked list which pass as reference and break point is 100.
 Function
void linkedlist::split(int val ,linkedlist &l)
 {      Node *temp=first;
       if (isEmpty())
{
cout <<"Have no node "<<endl;
}else if (first->next==NULL && first->data==val)
{   cout <<"threr is only one node "<<endl;    }else
{
    while (temp->next!=NULL)
{
           if (temp->data!=val)
  {
              temp=temp->next;
}else
{  
                  break;
 }
     }
    if (temp->next==NULL && temp->data==val)
  {  
        cout <<"this is last node of link list "<<endl;  
}else if(temp->next==NULL && temp->data!=val)
 {
       cout <<"vlaue not found "<<endl;       }else
   {
        l.first=temp->next;    temp->next=NULL; }
   }
     }
 Main
 linkedlist l1,l2;
l1.insertfirst(24);
l1.insertfirst(12);
l1.insertfirst(36);
l1.insertfirst(41);
l1.insertfirst(45);
l1.insertfirst(50);
l1.insertfirst(60);
l1.insertfirst(100);
l1.insertfirst(20);
cout <<"Before split l1 is"<<endl;
l1.print();
cout<<"Split"<<endl;
l1.split(100,l2);
cout <<"L2 -----------------------------"<<endl; l2.print(); cout<<"l1-------------------------------"<<endl;  l1.print();  

Friday, 18 November 2016

Welcome to C++ Solutions

AOA
Now In Shaa Allah we will post that programs which help you in your studies.
If Someone have problems in doing his \her assignment or in programs I hope you get solution from here
Thanks