Thursday, October 4, 2012

program for queue Implementation using Linked List in C++

Queue Implementation using Linked List in C++


#include<iostream.h>
#include<conio.h>
struct node
{
 int data;
 struct node *link;
};
class queue
{
 struct node *front,*rear,*ptr;
 public:
 queue(){
 rear=NULL;
 front=NULL;
 }
 void enqueue(int);          
 void dqueue();
 void display();
};
  void queue::enqueue(int data)
{
  ptr=new node;
  if(rear==NULL)
   { front=ptr;
   }
   else
   {
    rear->link=ptr;
    ptr->link=NULL;
    }
    rear=ptr;
  rear->data=data;               
  cout<<"\n>>";
      }
  void queue::dqueue()
{
int item;
 if(front==NULL)
 cout<<"\nQueue is empty:\n";
 else
 {
   item=front->data;
if(front==rear)
   {
   front=NULL;
   rear=NULL;
   }
else
{
   ptr=front;
   front=front->link;
   delete ptr;
   }
   cout<<"deleted elmnt : "<<item;
 }
}

  void queue::display()
{
ptr=front;
 if(front==NULL)
    cout<<"\nQueue is empty";
 else
    {
cout<<"\nElements in queue are: ";
     while(ptr!=NULL)
{
  cout<<ptr->data<<"|";
  ptr=ptr->link;
}
    }         
}
  void main()
{
queue q;
int ch,data;
clrscr();
cout<<"\n........MENU..........\n";
cout<<"1 . enqueue\n";
cout<<"2 . dqueue\n";
cout<<"3 . display\n";
cout<<"4 . exit\n";
while(1)
{
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
   {
    case 1:
    cout<<"\n Enter the element: ";
    cin>>data;
    q.enqueue(data);break;

    case 2:
    q.dqueue();break;

    case 3:
    q.display();break;

    case 4:
    ch=5;break;
    default:
    cout<<"\nInvalid selection\n";break;
    }
    if(ch==5)
    break;
}
       getch();
}

What are queues in C plus plus?

A queue in any language is a singly-linked list structure that permits new data to be inserted only at the end of a list while existing data can only be extracted from the beginning of the list. Queues are also known as a first in, first out (FIFO) structure. Unlike a standard singly-linked list, the list maintains a pointer to the last node as well as the first, in order to insert new data and extract existing data in constant time.
Variations on the queue include the priority queue which permit data to be weighted, such that data with the greatest priority is promoted to the front of the queue, behind any existing data with the same or higher priority, using an insertion sort technique. Insertion is therefore achieved in linear time rather than constant time, however extraction is always in constant time.

Linked Lists


The linked list is used in many libraries/applications and for a good reason. The following are advantages it has over other containers, indirectly from the reference page on cplusplus.com:

  • Efficient insertion and erasure of elements anywhere in the container (constant time).
  • Iterating over the elements in forward order (linear time).
  • Efficient moving elements and block of elements within the container or even between different containers (constant time).

The following only applies to a doubly linked list which I will explain later on:
  • Iterating over the elements in forward or reverse order (linear time).

Something the reference doesn't explain is how it's implemented. Why would you *want* to know about that? For me, simply curiosity. For others, perhaps they may create their own type of linked list container. Either way, it's knowledge that *someone* will eventually use hopefully. 

Design

The linked list is generally pictured as a list of linear nodes that are tethered together somehow. In C/++, you generally have a structure which contains data and a pointer to the next structure container which contains data and a pointer to the next structure container... and so on. The linked list's main advantage is that it doesn't contain data in a contiguous way and rather, a flexible way. This allows for fast insertion and better overall iteration. The linked list is often even used as the basis for other containers (such as queue or stack containers).

There are several variations of the linked-list. Actually, the term "linked list" doesn't really refer to the implementation (thus, an abstract term), just the nature of how the data of the container is held (linked via references). The most common implementation of the linked list is probably the doubly linked list. The doubly linked list is a list that contains nodes that contains references to the previous *and* next link in the list. This allows for fast node erasure, faster fetching of end nodes, and more flexible iteration.

At the cost of flexibility of the doubly-linked list, it generally costs more memory to use. In large lists, consider the idea of a node being twice the size as normal. This can affect the application quite a bit. In the case that you have no reason to iterate backwards, the doubly-linked list can be considered inefficient, simply by design. std::list is a doubly linked list. As a result, if I ever come about the need for a singly-linked list, I implement my own. A singly-linked list can only iterate forward simply because the node it holds only holds a reference to the next node in the list and not the previous but comes with the merit of one less reference.

Another type of linked-list that's not used quite as often is the circular linked-list. All this is a singly/doubly-linked list where the end node iterates forward to the beginning node (and possibly vice versa). There aren't many uses for this as it generally has problems with iterating through the list but take, for instance, a list that iterates through the nodes until a given signal is received. Also, it's really a technique used with linked lists... not always in implementation although special list implementations can handle circular linked lists better than others.

In embedded systems, the use of linked lists can be expensive. The holding of each reference can be so heavy that it's undesirable to use a linked-list with nodes that only hold one location of data. Instead, they generally used what's called unrolled linked lists. In an unrolled linked list, the node holds a reference to the next and previous, like normal, but each node holds arrays of data. When you iterate through each node, you iterate through the data in the array linearly, then move on to the next node. This way we can have three or four nodes of data in one while reducing the amount of nodes altogether (therefor saving memory). However, this generally uses contiguous memory to hold nodes so it becomes difficult to move nodes that are in the array.

c program to generate 1 12 123 pattern

program to generate 1 12 123 pattern

#include<stdio.h>

void main()
{
int i,j,n;
clrscr();
printf("how many lines you need to print :");
scanf("%d",&n);
for(i=0;i<n;i++)
   {
   for(j=0;j<=i;j++)
      {
      printf("%d",j+1);
      }
    printf("\n");
   }
getch();
}

program to generate 333 22 1 series

program to generate 333 22 1 series

#include<stdio.h>

void main()
{
int i,j,n;
clrscr();
printf("how many lines you need to print :");        //program to generate 333 22 1 series
scanf("%d",&n);
for(i=n;i>0;i--)
   {
   for(j=0;j<i;j++)
      {
      printf("%d",i);                                         //  program to generate 333 22 1 series
      }
    printf("\n");
   }
getch();
}

// program to generate 333 22 1 series

C program to display perfect,abudent,and deficiant numbers

C program to display perfect,abudent,and deficiant numbers 


what is an abundant number?
An abundant number is a number with its proper factors equalling a higher number than itself

what is a perfect number?
 A perfect number is one whose positive integer factors, other than itself, add up to the number itself. For example, the factors of 6, other than itself, are 1, 2, 3, and 1 + 2 + 3 = 6. Therefore, 6 is a perfect number.
The first eight perfect numbers are:
  • 6
  • 28
  • 496
  • 8128
  • 33550336
  • 8589869056
  • 137438691328
  • 2305843008139952128
what is a deficient number?
 A deficient number is a number with its proper factors equalling a lower number than itself.

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<math.h>
int test(int n);
void main()
{
int i,lower,upper,temp;
clrscr();
printf("Enter the range\n");
printf("\tLower Bound?");
scanf("%d",&lower);
printf("\tUpper Bound?");
scanf("%d",&upper);
if(lower>upper)
{  
temp=lower;
lower=upper;
upper=temp;
}

    printf("Number\tType\n");
    for(++lower;lower<upper;lower++)
    switch(test(lower))
          {   
           case 0:printf("%d\tperfect\n",lower);break;
           case 1:printf("%d\tAbudant\n",lower);break;
           case 2:printf("%d\tdeficiant\n",lower);break;
          }
     getch();     
}
int test(int n)
{
 int i,sum=0;
 for( i=2;i<n;++i)
   if(n%i==0)
     sum+=i;
     printf("%d",sum);
     getch();
   if(sum==n)
     return 0;
  else
    if(sum>n)
      return 1;
    else 
      return 2;
}

Tuesday, October 2, 2012

program to illustrate pass by reference method


#include<iostream.h>
#include<conio.h>
class sample
{
int x;
public:
void read()
{
cout<<"Enter the value\n";
cin>>x;
}
void display()
{
cout<<"The sum is\n";
cout<<x;
}
void add(sample &s1,sample &s2)
{
x=s1.x+s2.x;
}
};
void main()
{
sample s1,s2,s3;
clrscr();
s1.read();
s2.read();
s3.add(s1,s2);
s3.display();
getch();
}

Pass by value

When you use pass-by-value, the compiler copies the value of an argument in a calling function to a corresponding non-pointer or non-reference parameter in the called function definition. The parameter in the called function is initialized with the value of the passed argument. As long as the parameter has not been declared as constant, the value of the parameter can be changed, but the changes are only performed within the scope of the called function only; they have no effect on the value of the argument in the calling function.

Pass by reference

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function.In C, the corresponding parameter in the called function must be declared as a pointer type. In C++, the corresponding parameter can be declared as any reference type, not just a pointer type.
In this way, the value of the argument in the calling function can be modified by the called function.

Difference between pass by value and pass by reference  

In pass by value approach, the called function creates another copies of the variables passes as arguments. In this approach, the values of the original variables remain unchanged. However, we come across situations where we need to change the values of the original variables. Then the values may b passed by reference

program to represent pass by value concept

c++program code for representing  pass by value concept,also connected with class object concept
#include<iostream.h>
#include<conio.h>
class sample
{
int x;
public:
void read()
{
cout<<"Enter the value\n";
cin>>x;
}
void display()
{
cout<<"The sum is\n";
cout<<x;
}
void add(sample s1,sample s2)
{
x=s1.x+s2.x;
}
};
void main()
{
sample s1,s2,s3;
clrscr();
s1.read();
s2.read();
s3.add(s1,s2);
s3.display();
getch();
}

program to swap the values of 2 different object

c++ program code for swapping the values of different object (uses freind function concept)

c++ program code for swaping the values of different object

#include<iostream.h>
#include<conio.h>
class sample
{
int x;
public:
void read();
friend void display(sample,sample);
friend void swap(sample,sample);
};
void sample::read()
{
cout<<"Enter the value\n";
cin>>x;
}
void display(sample s1,sample s2)
{
cout<<"Before swaping\n";
cout<<"s1  "<<s1.x;
cout<<"\ns2  " <<s2.x;
}

void swap( sample s1,sample s2)
{
int t;
t=s1.x;
s1.x=s2.x;
s2.x=t;
cout<<"\nAfter swaping\n";
cout<<"s1  "<<s1.x;
cout<<"\ns2  " <<s2.x;
}
void main()
{
clrscr();
sample s1,s2;
s1.read();
s2.read();

display(s1,s2);
swap(s1,s2);
getch();
}

 what is an object?

A region of storage with associated semantics.
After the declaration int i; we say that "i is an object of type int." In OO/C++, "object" usually means "an instance of a class." Thus a class defines the behavior of possibly many objects (instances).

program to add n time in hh\mm\ss format(using class)

program to add n time using  class and object concept
program to add n numbers using class and object concept
#include<iostream.h>
#include<conio.h>
 class time
 {
 public:
 int h,m,s,oh,om,os,i,j,n,p,q;
 void read();
 void display();

 }ob;
 void time:: read()
 {
 cout<<"Enter the limit\n";
 cin>>n;
  h=0;
 m=0;
 s=0;

 for(i=0;i<n;i++)
 {
 cout<<"Enter the "<<i+1<<" time in hh/mm/ss format \n";
 cin>>oh;
 h=h+oh;
 cin>>om;
 m=m+om;
 cin>>os;
 s=s+os;
 }
 }
 void time::display( )
 {

  p=ob.s/60;
  ob.s=ob.s%60;
  q= (ob.m+p)/60;
  ob.m=(ob.m+p)%60;
  ob.h=ob.h+q;
  cout<<" time in hh/mm/ss format \n";
  cout<<ob.h<<":"<<ob.m<<":"<<ob.s;
  }
  void main()
  {
  clrscr();
  ob.read();
  ob.display();
  getch();
  }