Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 26

PART-A

Write a c++ program to perform insertion and deletion operation using arrays:

#include<iostream.h>

int a[20],b[20],c[40];

int m,n,p,val,i,j,key,pos,temp;

//Function Prototype//

void display();

void insert();

void del();

int main()

int choice;

cout<<"\nEnter the size of the array elements:\t";

cin>>n;

cout<<"\nEnter the elements for the array:\n";

for (i=0;i<n;i++)

cin>>a[i];

do {

cout<<"\n\n--------Menu-----------\n";

cout<<"1.Insert\n";

cout<<"2.Delete\n";

cout<<"3.Exit\n";

cout<<"-----------------------";

cout<<"\nEnter your choice:\t";

cin>>choice;

switch (choice)

case 1: insert();

break;

case 2: del();

break;

case 3:break;

default :cout<<"\nInvalid choice:\n";

} while (choice!=3);

return 0;

void display()//displaying an array elements

int i;

cout<<"\nThe array elements are:\n";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

}//end of display()
void insert()//inserting an element in to an array

cout<<"\nEnter the position for the new element:\t";

cin>>pos;

cout<<"\nEnter the element to be inserted :\t";

cin>>val;

for (i=n; i>=pos-1; i--)

a[i+1]=a[i];

a[pos-1]=val;

n=n+1;

display();

}//end of insert()

void del()//deleting an array element

cout<<"\n Enter the position of the element to be deleted:\t";

cin>> pos;

val= a [pos];

for (i= pos;i<n-1;i++)

a[i]=a[i+1];

n=n-1;

cout<<"\nThe deleted element is = "<<val;

display();

OUTPUT:

Enter the size of the array elements:      3

Enter the elements for the array:

--------Menu-----------

1.Insert

2.Delete

3.Exit

-----------------------

Enter your choice:            2

Enter the position of the element to be deleted:              1

The deleted element is = 3

The array elements are:


42

--------Menu-----------

1.Insert

2.Delete

3.Exit

-----------------------

Enter your choice:            1

Enter the position for the new element: 1

Enter the element to be inserted :            5

The array elements are:

542

--------Menu-----------

1.Insert

2.Delete

3.Exit

-----------------------

Enter your choice:            3

2.Write a C++ program to convert infix arithmetic expression to post fix expression.
#include<iostream>

#include<stack>

using namespace std;

bool isOperator(char c)

if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')

return true;

else

return false;

int precedence(char c)

        if(c == '^')

        return 3;

        else if(c == '*' || c == '/')

        return 2;

        else if(c == '+' || c == '-')


        return 1;

        else

        return -1;

string InfixToPostfix(stack<char> s, string infix)

string postfix;

for(int i=0;i<infix.length();i++)

if((infix[i] >= 'a' && infix[i] <= 'z')

||(infix[i] >= 'A' && infix[i] <= 'Z'))

postfix+=infix[i];

else if(infix[i] == '(')

s.push(infix[i]);

else if(infix[i] == ')')

while((s.top()!='(') && (!s.empty()))

char temp=s.top();

postfix+=temp;

s.pop();

if(s.top()=='(')

s.pop();

else if(isOperator(infix[i]))

if(s.empty())

s.push(infix[i]);

else

if(precedence(infix[i])>precedence(s.top()))

s.push(infix[i]);

else if((precedence(infix[i])==precedence(s.top()))&&(infix[i]=='^'))

s.push(infix[i]);
}

else

while((!s.empty())&&( precedence(infix[i])<=precedence(s.top())))

postfix+=s.top();

s.pop();

s.push(infix[i]);

while(!s.empty())

postfix+=s.top();

s.pop();

return postfix;

int main()

{   

   string infix_exp, postfix_exp;

   cout<<"Enter a Infix Expression :"<<endl;

   cin>>infix_exp;

   stack <char> stack;

cout<<"INFIX EXPRESSION: "<<infix_exp<<endl;

   postfix_exp = InfixToPostfix(stack, infix_exp);

   cout<<endl<<"POSTFIX EXPRESSION: "<<postfix_exp;

return 0;

OUTPUT:

Enter a Infix Expression :

a+b*c+d

INFIX EXPRESSION: a+b*c+d

POSTFIX EXPRESSION: abc*+d+

3.Perform Stack operations using Array implementation.


#include <iostream.h>

int stack[100], n=100, top=-1;

void push(int val) {

      if(top>=n-1)
      cout<<"Stack Overflow"<<endl;

      else {

            top++;

            stack[top]=val;

   }

void pop() {

      if(top<=-1)

      cout<<"Stack Underflow"<<endl;

      else {

            cout<<"The popped element is "<< stack[top] <<endl;

            top--;

   }

void display() {

      if(top>=0) {

            cout<<"Stack elements are:";

            for(int i=top; i>=0; i--)

            cout<<stack[i]<<" ";

            cout<<endl;

      } else

      cout<<"Stack is empty";

int main() {

      int ch, val;

      cout<<"1) Push in stack"<<endl;

      cout<<"2) Pop from stack"<<endl;

      cout<<"3) Display stack"<<endl;

      cout<<"4) Exit"<<endl;

      do {

            cout<<"Enter choice: "<<endl;

            cin>>ch;

            switch(ch) {

                  case 1: {

                        cout<<"Enter value to be pushed:"<<endl;

                        cin>>val;

                        push(val);

                        break;

         }

                  case 2: {

                        pop();

                        break;

         }

                  case 3: {

                        display();

                        break;

         }

                  case 4: {
                        cout<<"Exit"<<endl;

                        break;

         }

                  default: {

                        cout<<"Invalid Choice"<<endl;

         }

      }

      }while(ch!=4);

      return 0;

OUTPUT:

1) Push in stack

2) Pop from stack

3) Display stack

4) Exit

Enter choice:

Enter value to be pushed:

Enter choice:

Enter value to be pushed:

Enter choice:

Enter value to be pushed:

Enter choice:

Stack elements are:5 4 3

Enter choice:

The popped element is 5

Enter choice:

Stack elements are:4 3

Enter choice:

The popped element is 4

Enter choice:

Stack elements are:3

Enter choice:

The popped element is 3

Enter choice:

Stack is emptyEnter choice:


2

Stack Underflow

Enter choice:

Exit

4. Write a C++ program to simulate the working of Circular Queue using an array.

#include <iostream>

using namespace std;

#define SIZE 5

int A[SIZE];

int front = -1;

int rear = -1;

//Function to check if queue is empty or not

bool isempty()

if(front == -1 && rear == -1)

return true;

else

return false;

//function to enter elements in queue

void enqueue ( int value )

//queue is full

if ((rear + 1)%SIZE == front)

        cout<<"Queue is full \n";

else

    //first element inserted

    if( front == -1)

          front = 0;

//insert element at rear

rear = (rear+1)%SIZE;

      A[rear] = value;

//function to delete/remove element from queue

void dequeue ( )

if( isempty() )

    cout<<"Queue is empty\n";

else

//only one element


if( front == rear )

    front = rear = -1;

else

    front = (front + 1)%SIZE;

//function to show the element at front

void showfront( )

if( isempty())

cout<<"Queue is empty\n";

else

cout<<"element at front is:"<<A[front];

//function to display queue

void displayQueue()

if(isempty())

    cout<<"Queue is empty\n";

else

    int i;

    if( front <= rear )

  {

      for( i=front ; i<= rear ; i++)

      cout<<A[i]<<" ";

  }

    else

  {

      i=front;

      while( i < SIZE)

   {

      cout<<A[i]<<" ";

      i++;

   }

      i=0;

      while( i <= rear)

   {

      cout<<A[i]<<" ";

      i++;

   }

  }

//Main Function

int main()
{

int choice, flag=1, value;

while( flag == 1)

    cout<<"\n1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit\n";

    cin>>choice;

    switch (choice)

  {

    case 1: cout<<"Enter Value:\n";

                    cin>>value;

                    enqueue(value);

                    break;

    case 2: dequeue();

                    break;

    case 3: showfront();

                    break;

    case 4: displayQueue();

                    break;

    case 5: flag = 0;

                    break;

  }

return 0;

OUTPUT:

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

Enter Value:

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

Enter Value:

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

Enter Value:

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

567

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

3
element at front is:5

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

67

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

element at front is:6

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

element at front is:7

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

Queue is empty

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

Queue is empty

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

Queue is empty

1.enqueue 2.dequeue 3.showfront 4.displayQueue 5.exit

5
5. Write a C++ Program to perform Create and Display operations using Linked List.

#include <iostream>

using namespace std;

struct node *createLinkedList(int);

void dispalyLinkedList(struct node *);

//Declaring node

struct node{

        int data;

        struct node *next;

};

int main()

        int n;

        struct node *head;

        cout<<"Enter the size of linked list : ";

        cin>>n;

        //Calling function to create node

        head=createLinkedList(n);

        //Calling function to display list

        dispalyLinkedList(head);

        return 0;

struct node *createLinkedList(int n)

        int i;

        struct node *head=NULL;

        struct node *newNode, *ptr;

        for(i=0;i<n;i++)

       {

                //Creating a node

                newNode=(struct node *)malloc(sizeof(struct node));

                //Assigning data to newly created node

                cout<<"Enter the"<<" "<<i+1<<" "<<"node data:";

                cin>>newNode->data;

                newNode->next=NULL;

                /*If list is empty assign the address of newly created node

to head*/

                if(head==NULL)
       {

                        head=newNode;

        }

              else

      {

                        /* If list already have few elements then loop through

list and add newly created node at the end of list*/

                        ptr=head;

                        while(ptr->next!=NULL)

              {

                                ptr=ptr->next;

            }

                        ptr->next=newNode;

        }

    }

        return head;

void dispalyLinkedList(struct node *head){

        struct node *ptr;

        //If list is empty

        if(head==NULL){

                cout<<"The Linked List is empty";

    }

else{

                /*If list has elements then loop through the loop and

print elements one by one in sequential manner*/

                ptr=head;

                cout<<"the elements of the list are:"<<" ";

                while(ptr!=NULL)

        {

                        cout<<ptr->data;

                        cout<<"    ";

                        ptr=ptr->next;

        }

    }

OUTPUT

Enter the size of linked list : 5

Enter the 1 node data:50

Enter the 2 node data:60

Enter the 3 node data:30

Enter the 4 node data:20

Enter the 5 node data:10

the elements of the list are: 50    60    30    20    10


6.Write a program to search an element from a list. Give user the option to perform Linear or Binary search.
#include <iostream.h>

#include <stdlib.h>

#define MAX 10

int searchLinear(int a[], int n, int x)

        int i;

        for (i = 0; i < n; i++)

    {

                if (a[i] == x)

                        return (i + 1);

    }

        return -1;

int searchBinaryNR(int a[], int l, int h, int x)

        int i, mid;

        while (l <= h)

    {

                mid = (l + h) / 2;

                if (a[mid] == x)

                        return (mid + 1);

                else if (x > a[mid])

                        l = mid + 1;

                else

                        h = mid - 1;

    }

        return -1;

void inputArray(int a[], int n)

        int i;

        cout<<"\n Enter array elements:";

        for (i = 0; i < n; i++)

    {

                cin>>a[i];

    }

int main()

        int a[MAX], n, s, x, pos;

        while (1)

    {

              cout<<"\n Press 1 to search using Linear Search Algorithm";

                cout<<"\n Press 2 to search using Non - Recursive Binary Search Algorithm";

                cout<<"\n Press 3 to exit";

                cout<<"\n";
                cin>>s;

                switch (s)

        {

                case 1:

                        cout<<"\n Enter how many elements<= :", MAX;

                        cin>>n;

                        inputArray(a, n);

                        cout<<"\n Enter the element to be searched : ";

                        cin>>x;

                        pos = searchLinear(a, n, x);

                        break;

                case 2:

                        cout<<"\n Enter how many elements<= %d :", MAX;

                        cin>>n;

                        inputArray(a, n);

                        cout<<"\n Enter the element to be searched :";

                        cin>>x;

                        pos = searchBinaryNR(a, 0, n, x);

                        break;

                default:

                        exit(0);

        }

                if (pos != -1)

            {

                        cout<<"\n Element found at position";

                        cout<<" "<<pos;

            }

                else

                        cout<<"\n Element not found";

    }

        return 0;

OUTPUT

Press 1 to search using Linear Search Algorithm

Press 2 to search using Non - Recursive Binary Search Algorithm

Press 3 to exit

Enter how many elements<= :4

Enter array elements:10

20

30

40

Enter the element to be searched : 30


Element found at position 3

Press 1 to search using Linear Search Algorithm

Press 2 to search using Non - Recursive Binary Search Algorithm

Press 3 to exit

Enter how many elements<= %d :4

Enter array elements:4

Enter the element to be searched :2

Element found at position 3

Press 1 to search using Linear Search Algorithm

Press 2 to search using Non - Recursive Binary Search Algorithm

Press 3 to exit

3
7.Write a program using templates to sort a list of elements. Give user the option to perform sorting using Insertion sort, Bubble sort or Selection
sort.
#include<iostream.h>
void accept(int Arr[], int s);
void display(int Arr[], int s);
void isort(int Arr[], int s);
void ssort(int Arr[], int s);
void bsort(int Arr[],int s);
int main()
{
int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort method";
cout<<"\n4. Sort the array using selection sort method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: display(Arr,n);
break;
case 3: isort(Arr,n);
break;
case 4: ssort(Arr,n);
break;
case 5: bsort(Arr,n);
break;
case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n";
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{
int I,J,Temp;
for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1;
while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{
int I,J,Temp,Small;
for(I=0;I<s-1;I++)
{
Small=I;
for(J=I+1;J<s;J++) //finding the smallest element
if(Arr[J]<Arr[Small])
Small=J;
if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{
int I,J,Temp;
for(I=0;I<s-1;I++)
{
for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}

OUTPUT:-
Enter Size of Array 5
MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit

Enter your choice 1-6 :1


Enter element 1:55
Enter element 2:65
Enter element 3:75
Enter element 4:25
Enter element 5:15
MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit

Enter your choice 1-6 :2


The elements of the array are:
55 65 75 25 15

MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit

Enter your choice 1-6 :4


MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit

Enter your choice 1-6 :2


The elements of the array are:
15 25 55 65 75

MENU
1. Accept elements of array
2. Display elements of array
3. Sort the array using insertion sort method
4. Sort the array using selection sort method
5. Sort the array using bubble sort method
6. Exit

Enter your choice 1-6 :6

          PART-B

1.Write a C++ program to perform concept of Constructor and Destructor.


#include<iostream.h>
class Department
{
        public:
        Department()
    {
                cout<<"constructor invoked for the class Department"<<endl;
    }
        ~Department()
    {
                cout<<"destructor invoked for the classs Department"<<endl;
    }
};
class Employee
{
          public:
        Employee()
    {
                cout<<"constructor invoked for the class Employee"<<endl;
    }
        ~Employee()
    {
                cout<<"destructor invoked for the class Employee"<<endl;
    }
};
int main()
{
        Department d1;
        Employee e1;
        return 0;
}

OUTPUT:
constructor invoked for the class Department
constructor invoked for the class Employee
destructor invoked for the class Employee
                destructor invoked for the classs Department

2.Write a C++ Program to perform Memory allocation during run time.


#include<iostream.h>
int main()
{
        int *ptrvar1;
        float *ptrvar2;
        ptrvar1=new int;
        ptrvar2=new float;
        *ptrvar1=45;
        *ptrvar2=45.6;
        cout<<*ptrvar1<<endl;
        cout<<*ptrvar2<<endl;
        delete ptrvar1;
        delete ptrvar2;
        return 0;
}

OUTPUT:
45
45.55555556

3.Write a C++ program to swap 2 numbers using pointers.


#include <iostream.h>

int main()

        int x,y,temp;

        int *a,*b;

        cout<<"enter x & y : ";

        cin>>x>>y;

        a=&x;

        b=&y;

        cout<<"before swap : "<<endl;

        cout<<"x = "<<x<<endl;

        cout<<"y = "<<y<<endl;

        temp=*a;

        *a=*b;

        *b=temp;

        cout<<"after swap : "<<endl;

        cout<<"x = "<<x<<endl;
        cout<<"y = "<<y<<endl;

        return 0;

OUTPUT:

enter x & y : 4

before swap :

x=4

y=3

after swap :

x=3

y=4
4.Perform Queue operations using Array implementation.
#include <iostream.h>

int queue[100], n = 100, front = - 1, rear = - 1;

void Insert() {

      int val;

      if (rear == n - 1)

      cout<<"Queue Overflow"<<endl;

      else {

            if (front == - 1)

            front = 0;

            cout<<"Insert the element in queue : "<<endl;

            cin>>val;

            rear++;

            queue[rear] = val;

   }

void Delete() {

      if (front == - 1 || front > rear) {

            cout<<"Queue Underflow ";

            return ;

      } else {

            cout<<"Element deleted from queue is : "<< queue[front] <<endl;

            front++;;

   }

void Display() {

      if (front == - 1)

      cout<<"Queue is empty"<<endl;

      else {

            cout<<"Queue elements are : ";

            for (int i = front; i <= rear; i++)

            cout<<queue[i]<<" ";

                  cout<<endl;

   }

}
int main() {

      int ch;

      cout<<"1) Insert element to queue"<<endl;

      cout<<"2) Delete element from queue"<<endl;

      cout<<"3) Display all the elements of queue"<<endl;

      cout<<"4) Exit"<<endl;

      do {

            cout<<"Enter your choice : "<<endl;

            cin>>ch;

            switch (ch) {

                  case 1: Insert();

                  break;

                  case 2: Delete();

                  break;

                  case 3: Display();

                  break;

                  case 4: cout<<"Exit"<<endl;

                  break;

                  default: cout<<"Invalid choice"<<endl;

      }

      } while(ch!=4);

      return 0;}

OUTPUT:

1) Insert element to queue

2) Delete element from queue

3) Display all the elements of queue

4) Exit

Enter your choice :

Insert the element in queue :

Enter your choice :

Insert the element in queue :

Enter your choice :

Insert the element in queue :

Enter your choice :

Queue elements are : 3 2 1

Enter your choice :

Element deleted from queue is : 3

Enter your choice :

3
Queue elements are : 2 1

Enter your choice :

Element deleted from queue is : 2

Enter your choice :

Queue elements are : 1

Enter your choice :

Element deleted from queue is : 1

Enter your choice :

Queue elements are :

Enter your choice :

Queue Underflow Enter your choice :

Queue elements are :

Enter your choice :

Exit

5.Write a C++ Program to sort the elements using Merge Sort.


#include<iostream.h>
#include<conio.h>
void merge(int *, int, int, int);
void merge_sort(int *a, int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort (a, low,mid);
merge_sort(a,mid+1,high);
merge (a, low, high,mid);
}
}
void merge(int *a, int low, int high, int mid)
{
int i,j,k,b[50];
i=low;
j=mid+1;
k=low;
while(i<=mid && j<=high)
{
if(a[i] < a[j])
{
b[k++]=a[i++];
}
else
{
b[k++]=a[j++];
}
}
while(i<=mid)
{
b[k++]=a[i++];
}
while(j<=high)
{
b[k++]=a[j++];
}
for(i=low; i<k; i++)
{ a[i]=b[i];
}
}
void main()
{
int myarray[30],n,i;
clrscr();
cout<<"ENTER THE NUMBER OF ELEMENTS IN AN ARRAY"<<"\n";
cin>>n;
cout<<"ENTER"<<n<<"ELEMENTS OF AN ARRAY TO BE SORTED";
for(i=0; i<n; i++)
{
cin>>myarray[i];
}
merge_sort (myarray ,0,n-1);
cout<<"SORTED ARRAY" <<"\n";
for(i=0;i<n; i++)
{
cout<<myarray[i]<<"\t";
}
getch();
}

OUTPUT:
ENTER THE NUMBER OF ELEMENTS IN AN ARRAY
5
ENTER5ELEMENTS OF AN ARRAY TO BE SORTED50
40
30
20
10
SORTED ARRAY
10         20            30            40            50

6.Write a C++ Program to sort the elements using Quick Sort.


#include<iostream.h>
int partition(int a[],int low,int high)
{
    int pvt,temp,i,j,k;
    pvt=a[low];
    i=low+1;
    j=high;
    while(i<=j)
    {
            while(i<=high && a[i]<=pvt)
                  i++;
            while(a[j]>pvt)
                  j--;
            if(i<j)
        {
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
        }
          else
        {
                    k=a[j];
                    a[j]=a[low];
                    a[low]=k;
        }
   }
    return j;
}
void quick_sort(int a[],int low,int high)
{
      int pvt;
      if(low<high)
    {
            pvt=partition(a,low,high);
            quick_sort(a,low,pvt-1);
            quick_sort(a,pvt+1,high);
    }
}
int main()
{
int a[50],n,i,ch;
while(1)
{
    cout<<"\nEnter 1 for Sorting and 2 for exit";
    cout<<"\nEnter your choice";
    cin>>ch;
      switch(ch)
    {
          case 1:
     cout<<"enter number of elements\n";
cin>>n;
cout<<"enter the elements of an array";
for(i=0;i<n;i++)
      cin>>a[i];
quick_sort(a,0,n-1);
cout<<"sorted array is\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
                break;
        case 2: return 0;
        default: cout<<"Invalid Input";
    }
}
}
OUTPUT:

Enter 1 for Sorting and 2 for exit


Enter your choice1
enter number of elements
5
enter the elements of an array30
50
20
90
70
sorted array is
20 30 50 70 90
Enter 1 for Sorting and 2 for exit
Enter your choice2

7.Write a C++ program to perform Tree Traversal.


#include <iostream.h>
struct Node {
        int data;        struct Node *left, *right;
};
Node* newNode(int data)
{
        Node* temp = new Node;
        temp->data = data;
        temp->left = temp->right = NULL;
        return temp;
}
"bottom-up" postorder traversal. */
void printPostorder(struct Node* node)
{
        if (node == NULL)
                return;
        printPostorder(node->left);
        printPostorder(node->right);
        cout << node->data << " ";
}
void printInorder(struct Node* node)
{
        if (node == NULL)
                return;
        printInorder(node->left);
        cout << node->data << " ";
        printInorder(node->right);
}
void printPreorder(struct Node* node)
{
        if (node == NULL)
                return;
        cout << node->data << " ";
        printPreorder(node->left);
        printPreorder(node->right);
}

int main()
{
        struct Node* root = newNode(1);
        root->left = newNode(2);
        root->right = newNode(3);
        root->left->left = newNode(4);
        root->left->right = newNode(5);
        cout << "\nPreorder traversal of binary tree is \n";
        printPreorder(root);
        cout << "\nInorder traversal of binary tree is \n";
        printInorder(root);
        cout << "\nPostorder traversal of binary tree is \n";
        printPostorder(root);
        return 0;
}
Output:   

Preorder traversal of binary tree is 1 2 4 5 3


Inorder traversal of binary tree is 4 2 5 1 3
Postorder traversal of binary tree is 4 5 2 3 1

You might also like