Home / C/C++ / Archive by category 'Data Structures'

Data Structures

Auto variables in different blocks in C

void main()
{
int p=100;
clrscr();
printf("\n P=%d",p);
{
int p=50;
printf("\n P=%d",p);
}
{
int p=40;
printf("\n P=%d",p);
}
printf("\n P=%d",p);
}

Output

P=100
P=50
P=40
P=100

In the above program, the declaration of ‘p’ is made at three places with different values. In the first block, the value of ‘p’ is 100 and the same is displayed on the screen first. In the second block, the value of ‘p’ is 50. The control passes to the second block and ‘p’ is assigned a new value 50 and the same is displayed. When the control goes to third block, ‘p’ is 40 and the displayed value is 40. After it exits from the third block, the value of ‘p’ vanishes, which is initialized in the third block. In the last printf() statement, the value of ‘p’ is 100 which is declared in the first block.


Automatic Variables in C

auto variables are defined inside a function. A variable declared inside the function without storage class name is, by default, an auto variable. These functions are declared on the stack. The stack provides temporary storage. The scope of the variables is local to the block in which they are defined. These variables are available only to the current block or program. Once the executions of the function take place and the return turns off the function, the contents and existence of the auto variables or local variables vanish. Whenever a function is executed, all auto variables are allocated memory, and deallocated when the execution of function ends. The auto variables are safe, i.e. they cannot be accessed directly by other functions.

A keyword auto is used for declaration of automatic variables. By default, every variable pertains to auto storage class.

void main()
{
auto float p=5.5;
clrscr();
call2();
printf("\nP=%2.2f",p);
}
 
void call1()
{
auto float p=10.54;
printf("\nP=%2.2f",p);
}
 
 call2()
 {
 auto float p=20.50;
 call1();
 printf("\nP=%2.2f",p);
 }

Output

P=10.54
P=20.50
P=5.50


Stack implementation with operator overloading in C++

#include <conio.h>
#include <iostream.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
 
int  top=0;
 
//class definition
 
class stack
{
    public:
	    int a[];
	    int n;
 
	    stack ();
	    stack operator << (int);
	    stack operator +  (int);
	    stack operator --(void);
}r;
 
//definig the constructor to   initialize
 
    stack :: stack ()
    {
	    clrscr();
	    cout <<"Enter the size of the stack: ";
	    cin >> r.n;
 
	    a[n];
	    top=0;
     }
 
//operator to print
     stack stack::operator << (int top)
     {
	    clrscr();
 
	    if(top == 0)
	    {
		 cout<<"The stack is empty\n";
		 //getch();
	    }
 
	    else
	    {
		 cout<<"The contents of the stack...\n";
 
		 for(int i=top; i>=1; i--)
		 {
			cout<< "------\n";
			cout<< "| " << r.a[i] <<" |\n";
			cout<< "------\n";
		 }
	    }
 
	    return(r);
    }
 
//operator to push to the stack
    stack stack::operator + (int z)
    {
	    if(top == r.n)
	    {
		  cout<<"THE STACK IS FULL YOU CANNOT PUSH..!!\n";
		  getch();
	    }
	    else
	    {
		  top++;
		  a[top] = z;
 
		  r << top;
	    }
 
	    return(r);
     }
 
//operator to pop from	the stack
     stack stack::operator --(void)
     {
	  if(top==0)
	  {
		cout<<"THE STACK IS EMPTY SO CANNOT POP..!!";
	       //getch();
	  }
	  else
	  {
	       cout <<"Element poped is  |" << r.a[top];
	       top = top-1;
	       r << top;
	  }
	  return(r);
     }
 
 
    // main function
 
     void main()
     {
	   int choice;
	   char ch='y';
 
	   while(ch=='Y'||ch=='y')
	   {
		 clrscr();
 
		 cout<<"*** STACK OPERATIONS ***\n" << endl;
		 cout<<" 1-> PUSH    \n";
		 cout<<" 2-> POP     \n";
		 cout<<" 3-> DISPLAY \n";
		 cout<<" 4-> EXIT    \n";
 
		 cin>>choice;
 
		 switch(choice)
		 {
		     case 1:
			    {  int z;
 
			       cout<<"\nEnter the element to be pushed: ";
			       cin>> z;
 
			       r = r + z;  // + operator is overloaded
			       r << top;
 
			       //getch();
			    }
			    break;
 
		     case 2:
			    {
			       //clrscr();
			       r = --r;   // --operator is overloaded
			       r << top;
 
			       (flushall());
			       //getch();
			    }
			    break;
 
		    case 3:
			    {
			       r << top; // << operator is overloaded
 
			       (flushall());
			       //getch();
			    }
			    break;
 
		   case 4:
			    cout<<"IF EXHAUSTED TRY LATER..!!!!";
			    break;
 
		   default:
			    {
			     cout<<"INVALID CHOICE...!!\n";
			     cout<<"Enter choice between 1 and 4 \n";
			    }
			    break;
		}
 
		cout<<"\nDo you want to continue [y/n]?";
		cin>>ch;
	 }
     }

Enter the size of the stack:3
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed: 56
The contents of the stack…
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed: 67
The contents of the stack…
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed: 50
The contents of the stack…
——
| 50 |
——
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
1

Enter the element to be pushed:90
THE STACK IS FULL YOU CANNOT PUSH..!!

The contents of the stack…
——
| 50 |
——
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
2

The contents of the stack…
——
| 67 |
——
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
2

The contents of the stack…
——
| 56 |
——

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
2

Stack is empty

Do you want to continue [y/n]?y
*** STACK OPERATIONS ***

1-> PUSH
2-> POP
3-> DISPLAY
4-> EXIT
3

Stack is empty

Do you want to continue [y/n]?n


Quicksort of array in c++

#include <iostream.h>
#include <conio.h>
#define SIZE 10
 
template <class T>
class QSORT
{
   private:   T Array[SIZE];
	      int len;
   public:    QSORT(int);	//constructor
	      void getArray(void);
	      void quickSort(int,int);
	      int partition(int, int);
	      void printArray(void);
};	//End of QSORT class
 
template <class T>
QSORT <T> :: QSORT(int length)
{
    len = length;
 
    //Initialize all array elements to 0
    for (int k = 0; k < len; k++)
    {
	 Array[k] = 0;
    }
}  //End of the constructor function
 
template <class T>
void QSORT <T> :: getArray(void)
{
    cout<<"Enter the elements of an array"<<endl;
    for (int k = 0; k < len; k++)
    {
	 cin>>Array[k];
    }
}	//End of getArray function
 
template <class T>
void QSORT <T> :: quickSort(int low, int high)
{
    int pos;
    if(low < high)
    {
	 pos = partition(low,high);
	 quickSort(low, pos-1);
	 quickSort(pos+1,high);
    }
} 	//End of quickSort function
 
template <class T>
int QSORT <T> :: partition(int low, int high)
{
    T key, temp;
    int left,right, true = 1;
 
    left = low;
    right= high;
    key = Array[low];  // Assume that the first element
		       // is a pivot value
 
    while(true)
    {
	while(left <high && (key >= Array[left]))
	{
	  left++;
	}
 
	while(key < Array[right])
	{
	   right--;
	}
 
	if (left < right)
	{
	   temp = Array[left];
	   Array[left] = Array[right];
	   Array[right]=temp;
	}
	else
	{
	   temp = Array[low];
	   Array[low] = Array[right];
	   Array[right]=temp;
	   return (right);
	}
    }   //End of while
}
 
template <class T>
void QSORT <T> :: printArray(void)
{
    cout<<"Sorted array is "<<endl;
    for (int k = 0; k < len; k++)
    {
	 cout<<Array[k]<<endl;
    }
}	//End of printArray function
 
void main()
{
   int size,low, high;
   clrscr();
 
   cout<<"Enter the size of array"<<endl;
   cin>>size;
 
   low = 0;
   high = size - 1;
 
   QSORT<int> IA(size);
   QSORT<double> DA(size);
 
   cout<<"INTEGER ARRAY PROCESSING..."<<endl;
   IA.getArray();
 
   IA.quickSort(low, high);
   IA.printArray();
 
   cout<<"\nDOUBLE-PRECISION ARRAY PROCESSING..."<<endl;
   DA.getArray();
   DA.quickSort(low, high);
   DA.printArray();
}

Output
Enter the size of array
5
INTEGER ARRAY PROCESSING…
Enter the elements of an array
51
23
78
94
11
Sorted array is
11
23
51
78
94

DOUBLE-PRECISION ARRAY PROCESSING…
Enter the elements of an array
55.67
78.90
23.12
11.555
67.89
Sorted array is
11.555
23.12
55.67
67.89
78.9


Operations on Linked Lists in C++

#include <iostream.h>
#include <conio.h>
 
class listNode
{
   public:  int data;
	    listNode *ptr;
};
 
class linkedList
{
   private: listNode *head;
 
   public: linkedList()  //constructor
	   {
	      head = NULL;
	   }
	   void insertFront(void);
	   int deleteFront(void);
	   void showList(void);
 
	   ~linkedList()   //destructor
	   {
	      delete head;
	   }
}; //End of linkedList
 
void linkedList::insertFront(void)
{
      listNode *newNode;
      int item;
 
      newNode = new listNode;
      cout<<"Enter the element to be inserted"<<endl;
      cin>>item;
      newNode->data = item;
      newNode->ptr = head;
      head = newNode;
}
 
int linkedList::deleteFront(void)
{
       listNode *temp;
       int item;
       if (head == NULL)
       {
	   cout<<"List is empty"<<endl;
	   return 1;
       }
       temp = head;
       cout<<"Deleted element is "<<temp->data<<endl;
       item = temp->data;
       head = head->ptr;
       delete(temp);
 
       return item;
}
 
void linkedList::showList(void)
{
      listNode *temp;
      temp = head;
      cout<<"\nContents of the list..."<<endl;
      while(temp != NULL)
      {
	 cout<<temp->data<<"->";
	 temp=temp->ptr;
      }
      cout<<"NULL"<<endl;
}
 
void main()
{
   linkedList ll;
   int choice, rpt;
   clrscr();
 
   while(rpt)
   {
       cout<<"1 --> Insert at Front"<<endl;
       cout<<"2 --> Delete from Front"<<endl;
       cout<<"3 --> Show the status "<<endl;
       cout<<"4 --> Exit "<<endl;
 
       cout<<"Enter your choice"<<endl;
       cin>>choice;
 
       switch(choice)
       {
	 case 1: ll.insertFront();
		 break;
	 case 2: ll.deleteFront();
		 break;
	 case 3: ll.showList();
		 break;
	 case 4: return;
       }
       cout<<"Do you want to continue(Type 1 or 0)?"<<endl;
       cin>>rpt;
   }
}

Output
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
1
Enter the element to be inserted
12
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> xit
Enter your choice
1
Enter the element to be inserted
34
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
1
Enter the eeement to be inserted
67
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
3

Contents of the list…
67->34->12->NULL
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
Deleted element is 67
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
Deleted element is 34
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
Deleted element is 12
Do you want to continue(Type 1 or 0)?
1
1 –> Insert at Front
2 –> Delete from Front
3 –> Show the status
4 –> Exit
Enter your choice
2
List is empty
Do you want to continue(Type 1 or 0)?
0


Add two complex objects and add real part to a complex object in C++

#include <iostream.h>
#include <conio.h>
 
class COMPLEX
{
    private: int realp;
	     int imgp;
    public:  void readValues(void)
	     {
		cout<<"Enter the real part and imaginary part"<<endl;
		cin>>realp>>imgp;
	     }
 
	     friend COMPLEX ADD(COMPLEX,COMPLEX);
	     friend COMPLEX ADD(int,COMPLEX);
	     friend ostream& operator << (ostream&, COMPLEX&);
};
 
COMPLEX ADD(COMPLEX s1, COMPLEX s2)  //To add two COMPLEX objects
{
     COMPLEX c;
     c.realp = s1.realp + s2.realp;
     c.imgp = s1.imgp + s2.imgp;
     return c;
}
 
COMPLEX ADD(int ival, COMPLEX s2)  //To add an int to COMPLEX object
{
     COMPLEX c;
     c.realp = ival + s2.realp;
     c.imgp =  s2.imgp;
     return c;
}
 
//Member function to overload <<
ostream& operator << (ostream& out, COMPLEX& comp)
{
    out << comp.realp << " +i " << comp.imgp <<endl;
    return out;
}
 
void main()
{
    COMPLEX C1, C2, C3;
    int choice, ival;
    clrscr();
 
    cout<<"1. To add two complex objects"<<endl;
    cout<<"2. To add real part to a complex object"<<endl;
    cout<<"Please enter your choice"<<endl;
    cin>>choice;
 
    switch(choice)
    {
	case 1: C1.readValues();
		C2.readValues();
		cout<<"\nC1 = "<<C1;
		cout<<"C2 = "<<C2;
		cout<<"-----------"<<endl;
		C3 = ADD (C1, C2);
		cout<<"C3 = "<<C3;
		break;
	case 2: cout<<"Enter an integer"<<endl;
		cin>>ival;
		COMPLEX C4;
		C1.readValues();
		cout<<"\nival = "<<ival<<endl;
		cout<<"C1   = "<<C1;
		cout<<"----------------"<<endl;
		C4 = ADD(ival, C1);
		cout<<"C4   = "<<C4;
		break;
	default: cout<<"Error in input"<<endl;
		 break;
    }
}

Output:

Output
1. To add two complex objects
2. To add real part to a complex object
Please enter your choice
1
Enter the real part and imaginary part
2
3
Enter the real part and imaginary part
4
5

C1 = 2 +i 3
C2 = 4 +i 5
———–
C3 = 6 +i 8

RUN2
1. To add two complex objects
2. To add real part to a complex object
Please enter your choice
2
Enter an integer
13
Enter the real part and imaginary part
12
45

ival = 13
C1 = 12 +i 45
—————-
C4 = 25 +i 45