prog lab-ii-data structures using c-lab manual

140
 ANNAMALAI UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING MASTER OF COMPUTER APPLICATION I SEMESTER  MCAP 107 – PROGRAMMI NG LAB –I I DATA STRUCTURE USING - C

Upload: tanvi-sharma

Post on 01-Nov-2015

224 views

Category:

Documents


0 download

DESCRIPTION

DATA STRUCTURES

TRANSCRIPT

  • ANNAMALAI UNIVERSITY

    FACULTY OF ENGINEERING AND TECHNOLOGY

    DEPARTMENT OF COMPUTER SCIENCE ANDENGINEERING

    MASTER OF COMPUTER APPLICATION

    I SEMESTER

    MCAP 107 PROGRAMMING LAB IIDATA STRUCTURE USING - C

  • MCA1710 Programming Lab- II (Data Structures Using C)List of Experiments1. RECURSION IN C

    a) Factorial numbersb) Fibonacci series

    2. Implementation of Stack Using Array3. Implementation of Queue Using Array4 . Conversion of Infix to Postfix5. Single Linked list6. Doubly Linked List7. Implementation of Stack Using Linked List8. Implementation of Queue Using Linked List9. Binary Tree Traversal10. Binary Search Tree11. SORTING TECHNIQUES

    a) Bubble Sortb) Merge Sortc) QuickSortd) Radix Sort

    12. SEARCHING TECHNIQUESa) Linear Searchb) Binary Search

    Lab Incharge

    (Dr.J.Sasikala)

  • 1.Recursion in Ca) Factorial numbers

    AimWrite a C program to implement Factorial of a

    number using Data StructuresAlgorithm

    (i) Start the program(ii) Read the number n(iii)[Initialize]

    i=1, fact=1(iv)Repeat step 4 through 6 until i=n(v)fact=fact*i(vi) i=i+1(vii) Print fact(viii)Stop the program.

    Program#include#includeint main(){

  • int n,i,fact=1;printf("Enter any number : ");scanf("%d", &n);for(i=1; i
  • 24120The factorial of number 5 is 120.

    ResultThus the program was successfully executed

    and the output was verified.

  • b) Fibonacci seriesAim

    Write a C Program to implement FibonacciSeries using Data Structures

    Algorithm

    (i)Start the program

    (ii)Declare variables n,i=0,j=1,temp,k.

    (iii)Input and read the limit of the Fibonacciseries,n.

    (iv)If n==1 then

    1)Print i and j.

    (v)Else

    1).Print i and j.

  • 2).Initialize k as 2.

    3).while(k

  • #include

    void main()

    {

    int n,i=0,j=1,temp,k;

    clrscr();

    printf("Enter the limit: ");

    scanf("%d",&n);

    printf("fibonacci series\n");

    if(n==1)

    {

    printf("%d\n%d\n",i,j);

    }

  • else

    {

    printf("%d\n%d\n",i,j);

    for(k=2;k

  • }Output

    Enter the limit:13

    Fibonacci Series

    0

    1

    1

    2

    3

    5

    8

  • 13

    21

    34

    55

    89

    144

    ResultThus the program was successfully executed

    and the output was verified.

  • 2. IMPLEMENTATION OF STACK USINGARRAY

    AimTo write a C program to perform the

    implementation of stack using array.AlgorithmStep 1: Start the programStep 2: Initialize the array variable st[100] forstoring elements and declare the requiredvariables.Step 3: Define a function to push , pop and displaythe data item of the queue.Step 4: For push() function

    Get the new elementsCreate the top is greater than array

    size,display stack is overflow.elseInsert the new element and increment the toppointer.

  • Step 5: For pop() functionIf the Stack is empty then display stack is

    underflow.elseDecrement the top pointer.Step 6: For display() function

    Apply the loop for to display the stackelements.Step 7: Stop the program.

    Program//stack using array#include#includeint stack[100],option,n,top,x,i;void push();void pop();void display();

  • void main(){clrscr();top=-1;printf(\n enter the size of STACK[MAX=100]:);scanf(%d,&n);printf(\n\t\t STACK OPERATION);printf(\n\t\t~~~~~~~~~~~~~~~~~);printf(\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.EXIT);do{//clrscr();printf(\n enter UR option:);scanf(%d,&option);switch(option){case 1:{push();break;}

  • case 2:{pop();break;}case 3:{display();break;}case 4:{printf(\n\tUR ON EXIT);break;}default:{printf{\n\t enter only 1,2,3,4);}getch();}while(option!=4);}void push(){if(top>=n-1){printf(\n\t\tSTACK is over flow);getch();}else{

  • printf(enter a value to be pushed:);scanf(%d,&x);top++;stack[top]=x;//printf(\n\t\t%d is pushed\n\n\n,x);//display();}}void pop(){if(top
  • top--;//display();}}void display(){if(top>=0){printf(\n the elements in STACK);printf(\n~~~~~~~~~~~~~~~~~~~);for(i=top;i>=0;;i--)printf(\n\n%d,stack[i]);printf(\n\n\n\n press any key to continue);}else{printf(\n the STACK is empty);}

  • }Sample output:Enter the size of STACK[MAX=100]: 3

    STACK OPERATION1.PUSH2.POP3.DISPLAY4.EXIT

    Enter ur option: 1Enter a value to be pushed:22Enter ur option: 1Enter a value to be pushed:33Enter ur option: 1Enter a value to be pushed:44Enter ur option: 1STACK is over flowEnter ur option:3The elements in STACK

  • 443322Press any key to continueEnter ur option: 2

    The popped elements is 44Enter ur option: 2

    The popped elements is 33Enter ur option: 2

    The popped elements is 22Enter ur option: 2

    Stack is under flowEnter ur option: 3The STACK is emptyEnter ur option: 4

    UR ON EXIT

  • Result:Thus the program was successfully

    executed and the output was verified.

  • 3.IMPLEMENTATION OF QUEUE USINGARRAY

    AimTo write a C program to perform the

    implementation of queue using arrayAlgorithm

    Step 1: start the programStep 2: initialize the array variable a() for

    storing elements and declare the required variableStep 3: Define a function to insert , delete

    display the dataitems for dataStep 4: For insert() function

    *get the new elements*create the rear is greater than arraysize display queue isoverflow.

    Else

  • Insert the new element & increment the toppointer

    Step 5: For delete() functionIf the queue is empty than display queue is

    over flowElse

    Decrement the top pointerStep 6: For display() function

    Apply the loop for to display queueelements.Program//queue using array#include#include#define n 5void main(){int q[n],ch=1,front=0,rear=0,i,j=1,x=n;

  • clrscr();printf(\n\t\tQueue using array);printf(\n1.Insertion);printf(\n2.Deletion);printf(\n3.Display);printf(\n4.Exit);while(ch){printf(\n Enter UR choice);scanf(%d,&ch);switch(ch){case 1:if(rear= =x)printf(\n Queue full);else{printf(\n enter no%d:,j++);

  • scanf(%d,&q[rear++]);}break;case 2:if(front= =rear){printf(\n Queue is empty);}else{printf(\ n Deleted elements is %d,q[front++]);x++;}break;case 3:printf(\n Queue elements);if(front==rear)printf(\n queue is empty);

  • else{for(i=front;i
  • Sample outputQueue using array

    1.Insertion2.Deletion3.Display4.ExitEnter ur choice 1Enter no1: 22Enter ur choice 1Enter no1: 33Enter ur choice 1Enter no1: 11Enter ur choice 3Queue elements 22 33 11Enter ur choice 2Deleted elements is 11Enter ur choice 2Queue is empty

  • Enter ur choice 3Queue elementsQueue is emptyEnter ur choice 4Ur on exit..

    ResultThus the program was successfully

    executed and the output was verified

  • 4. CONVERSION OF INFIX TO POSTFIXEXPRESSION

    AimTo convert given infix expression to its postfix

    formAlgorithm

    Step 1: get the infix expression in a characterarrayStep 2: scan the expression character wise from

    left to right 3Step 3: if the character is an operand add it to the

    character array. If the character is an operator,push it into stack .while pushing check the priorityof the current operator (o/p) inside the stackaccording to the assumed priority given below.

    Operator priorityX 0*/ 2+- 1

  • Step 4: if the I/P priority is less than o/p priority,pop up the operator from the stack and add it to thecharacter array until,an operator having less.Step 5: priority then the I/P priority is reached andpush the current operator into the stackStep 6: if the character is an c,push it into thestack if it is a ) pop up the character from thestack upto opening paranthesis and it to thecharacter array.Step 7: if the stack is not empty after the scanningis over, pop up the remaining operators from thestack till pop=0 is reached and add it to thecharacter array.Step 8: print the character array which are nowcontains the postfix form of the given infixexpression.

    Program/*infix to postfix*/

  • #include#include#includeint isp(char);int icp(char);void convert();char str[50];void main(){clrscr();printf(\n\t\tINFIX TO POSTFIXCONVERSION);printf(\n Enter the infix expression);Printf(\n Enter the infix expression add # as lastcharacter);scanf(%s,&str);convert();getch();

  • }int isp(char c){int pr;switch(c){case&:pr=-1;break;case):pr=0;break;case*:pr=3;break;case/:pr=2;break;

  • case+:pr=1;break;case-:pr=1;break;case(:pr=0;break;}return(pr);}int icp(char c){int pr;switch(c){case ):

  • pr=0;break;case ^:pr=3;break;case *:pr=2;break;case /:pr=2;break;case +:pr=1;break;case -:pr=1;break;case (:

  • pr=4;break;}return(pr);}void convert();{int i=0,top=0;char item,st[50];st[0]=&;printf(\n The postfix expression:);while(str[i]!=#){item=str[i];if(isalpha(item))printf(%c,item);else{

  • if(item= =));{while(st[top]!=();{printf(%c,st[top]);top--;}top--;}else{while(isp(st[top])>=icp(item)){pritnf(%c,st[top]);top--;}top++;st[top]=item;

  • }}i++;}while(top>=1){printf(%c,st[top]);top--;}}

    Sample outputINFIX TO POSTFIX CONVERSION

    Enter the infix expressionEnter the infix expression add # at last character

    (a+b)*c+(d-a)#The postfix expression: ab+c*da-+

  • ResultThus the program was successfully executed and

    the output was verified.

  • 5. SINGLE LINKED LISTAim

    To create a c program to perform create,insert, and delete operations in single linked list.AlgorithmStep 1: define a structure to represent the node of

    the linked list. It contains a data field and apointerfield which points to the next node.Step 2: declare a struct to get the data item andget the position of the node to be added or selectedin the linked list.Step 3: define a function to create, insert, deleteand print the data items of the linked list.

    *for create() function*create a new node to get the value*get the value and assign the value

    into node*create the another node and get the

    value link the previous node

  • *repeat the process until the valuegetting becomes zero.Step 4: for insert() function

    *get the data item and the position ofthe new node to be inserted in the linked list.

    *travel the list to locate thepredecessor and successor nodes.

    Step 5: for delete() function*get the position of the node to be

    deleted*travel the list to locate the

    predecessor and successor nodes of the deletingnode

    *adjust the link fields of deleting nodeand its predecessor and successor.Step 6: for disp() function

    *traverse the list and print the valuesas data item in each node until the pointer nodebecomes null(i.e., till the end of the list).Step 7: Stop the program.

  • Program/*SINGLE LINKED LIST*/

    #include#include#includestruct stack{int info;struct stack *next;}*p,*root,*newn;int ch=1,val;void main(){void create();void insert();void del();void disp();

  • clrscr();printf(\n\t\tLINEAR LINKED LIST );printf(\n1.create\n2.insert\n3.delete\n4.display);while(ch){printf(\n Enter ur choice :);scanf(%d,&ch);switch(ch){case 1:create();break;case 2:insert();break;case 3:del();break;

  • case 4:disp();break;default:exit(0);}}getch();}void create(){newn=malloc(sizeof(struct stack));printf(\nenter val:);scanf(%d,&val);newn->info=val;newn->next=null;root=newn;p=root;

  • printf(\nEnter val:);scanf(%d,&val);while(val!=0){newn=malloc(sizeof(struct stack));newn->info=val;p=root;if(valinfo){newn->next=p;root=newn;printf(\nEnter val:);scanf(%d,&val);}else{while((p->next!=null)&&(p->next->info
  • p=p->next;}newn->next=p->next;p->next=newn;printf(\nEnter val:);scanf(%d,&val);}}}void insert(){int val;newn=malloc(sizeof(struct stack));printf(\nEnter val:);scanf(%d,&val);newn->info=val;p=root;if(valinfo)

  • {newn->next=p;root=newn;}else{while((p->next!=NULL)&&(p->next->infonext;}newn->next=p->next;p->next=newn;}}void del(){struct stack*f;int val;

  • printf(\nEnter val:);scanf(%d,&val);p=root;if(val==root->info){root=root->next;free(p);}else{while(p->next!=NULL){if(p->next!=NULL){f=p->next;p->next=f->next;free(f);if(p->next= =NULL)

  • {p=root;}break;}p=p->next;}if(p->next==NULL){printf(\n Value not found);}}}void disp(){p=root;if(root!=NULL){

  • printf(\nThe elements are:);printf(topnext){printf(%d,p->info);printf(
  • 4.displayEnter ur choice: 1Enter val: 22Enter val: 33Enter val: 44Enter val: 0Enter ur choice: 4The elements are: top
  • ResultThus the program was successfully executed

    and the output was verified

  • 6. DOUBLY LINKED LISTAim

    Write a C program to perform create,insertand delete operations in doubly linked list.AlgorithmStep 1: define a structure to represent the node ofthe linked list. It contains a data field and apointerfield which points to the next node.Step 2: declare a struct to get the data item andget the position of the node to be added or selectedin the linked list.Step 3: define a function to create, insert, deleteand print the data items of the linked list.Step 4: For create() function

    *create a new node to get the value*get the value and assign the value

    into node*update the left and right pointer

  • *create the another node and get thevalue link the previous node and get the value andupdate the left and right pointer.

    *repeat the process until the valuegetting becomes zero.Step 5: for insert() function

    *get the data item and the position ofthe new node to be inserted in the linked list.

    *traverse the list to locate thepredecessor and successor nodes.

    *adjust the link fields of a new nodeand its predecessor and successor node.

    Step 6: for delete() function*get the position of the node to be

    deleted*traverse the list to locate the

    predecessor and successor nodes of the deletingnodes.

    *adjust the link fields of deleting nodeand its predecessor and successor.

  • Step 7: for disp() function*traverse the list and print the values

    as data item in each node until the pointer nodebecomes null(i.e., till the end of the list).Step 8: Stop the program.

    Program/*DOUBLY LINKED LIST*/#include#include#include#includestruct stack{struct stack *left;int info;struct stack *right;}

  • *p,*root,*newn;int ch,val;void main(){void create();void insert();void del();void disp();clrscr();printf(\n\t\tDOUBLY LINKED LIST );printf(\n1.create\n2.insert\n3.delete\n4.display);do{printf(\n Enter ur choice :);scanf(%d,&ch);switch(ch){case 1:

  • create();break;case 2:insert();break;case 3:del();break;case 4:disp();break;default:exit(0);}}while(ch
  • void create(){newn=malloc(sizeof(struct stack));printf(\nEnter val:);scanf(%d,&val);newn->info=val;newn->right=NULL;root=newn;p=root;printf(\nEnter val:);scanf(%d,&val);while(val!=0){newn=malloc(sizeof(struct stack));newn->info=val;p=root;if(valinfo){

  • newn->right=root;root->left=newn;newn->left=null;root=newn;printf(\nEnter val:);scanf(%d,&val);}else{while((p->right!=NULL)&&(p->right->inforight;}newn->right=p->right;newn->left=p;p->right->left=newn;p->right=newn;

  • printf(\nEnter val:);scanf(%d,&val);}}}void insert(){int val;newn=malloc(sizeof(struct stack));printf(\nenter val:);scanf(%d,&val);newn->info=val;p=root;if(valinfo){newn->right=p;newn->left=NULL;p->left=newn;

  • root=newn;}else{while((p->right!=NULL)&&(p->right->inforight;}newn->right=p->right;newn->left=p;p->right->left=newn;p->right=newn;}}void del(){struct stack*f;

  • int val;printf(\nenter val:);scanf(%d,&val);p=root;if(val==root->info){root=root->right;free(p);}else{while(p->right!=NULL){if(p->right->info= =val){f=p->right;p->right=f->right;f->right->left=f->left;

  • free(f);if(p->right= =NULL){p=root;}break;}p=p->right;}if(p->right==NULL){printf(\n value not found);}}}void disp(){p=root;

  • if(root!=NULL){printf(\nThe elements are:);printf(TOPright){printf(%d,p->info);printf(->);printf(
  • 2.insert3.delete4.displayEnter ur choice: 1Enter val: 22Enter val: 44Enter val: 33Enter val: 0Enter ur choice: 4The elements are: top
  • Enter ur choice:4The elements are: top
  • 7. IMPLEMENTATION OF STACK USINGLINKED LIST

    AimWrite a C program for stack using linked list.

    AlgorithmStep 1: start the programStep 2: Initialize the structure stackStep 3: Initialize the global variableStep 4: The operation for stack are:

    (i). push(ii).pop(iii).display(iv).exit

    Step 5: If choice=1Push()functionCreate a newnode using structureEnter the value to be inserted to the stack

  • Assign newnode->next=top andtop=newnode.Step 6: if choice=2

    Pop() functionIf top is equal to null, then the stack is

    underflowElse assign p=top and top=p->nextPrint the p valueFree the value of p.Step 7: if choice=3

    Display() functionIf top is equal to null then stack

    underflow.Else using loop from top to null print the

    values.Step 8: if choice =4

    Call the exit function.Step 9: Stop the program.

  • Program//STACK USING LINKED LIST#include#include#includestruct stack{int data;struct stack *next;}*p,*top,*newn;int ch=1;void main(){void push();void pop();void display();clrscr();

  • printf(\n\t\t STACK USING LINKED LIST);printf(\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~);printf(\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\

    n);while(ch){printf(enter ur option:);scanf(%d,&ch);switch(ch){case 1: push();break;case 2: pop();break;case 3: display(); break;case 4: exit(0);default:exit(0);}}getch();

  • }void push(){printf(Enter ur data);newn=malloc(sizeof(struct stack));scanf(%d,&newn->data);newn->next=top;top=newn;}void pop(){if(top= =NULL){printf(stack is under flow);}else{p=top;

  • top=p->next;printf(the popped elements is %d,p->data);free(p);}printf(\n);}void display(){if(top= =NULL){printf(stack is empty\n);}else{printf(the stack elements are:);for(p=top;p!=NULL;p=p->next)printf(\t%d,p->data);printf(\n);

  • }}

    Sample outputSTACK USING LINKED LIST

    1.PUSH2.POP3.DISPLAY4.EXITEnter ur option: 1Enter ur data 55Enter ur option: 1Enter ur data 35Enter ur option: 1Enter ur data 25Enter ur option: 1Enter ur data 45Enter ur option: 3

  • The stack elements are: 45 25 35 55Enter ur option: 2The popped elements is 45Enter ur option: 2The popped elements is 25Enter ur option: 2The popped elements is 35Enter ur option: 2The popped elements is 55Enter ur option: 2Stack is underflowEnter ur option: 3Stack is emptyEnter ur option: 4

  • ResultThus the program was successfully executed

    and the output was verified.

  • 8. IMPLEMENTATION OF QUEUE USINGLINKED LIST

    AimWrite a C program for queue using linked list.

    AlgorithmStep 1: start the programStep 2: Initialize the structure queueStep 3: Initialize the global variableStep 4: The operations for queue are:

    (i). insert(ii).delete(iii).display(iv).exit

    Step 5: If choice=1For Insert()function

    Get the new node and insert the data.Set the new node link equal to null

  • If the rear is equal to null set front is equalto node and rear to equal node.

    ElseSet rear(next) is equal to node and rear

    equal to the node.Step 6: For delete() function

    If front is equal to null, display queue isemptyElse

    Decrement the value from front node andset front(next)=front.Step 7: Apply the loop condition to display thequeue contentStep 8: Stop the program

    Program//QUEUE USING LINKED LIST#include#include

  • #include#include#define null 0struct queue{int data;struct queue *next;}*p,*front,*rear,*newn;int ch=1,x;void main(){void insert();void delete();void display();clrscr();printf(\n\t\t QUEUE USING LINKED LIST);

  • printf(\n1.Insert\n2.Delete\n3.Display\n4.Exit\n);

    while(ch){printf(Enter ur choice:);scanf(%d,&ch);switch(ch){case 1: insert();break;case 2: delete();break;case 3: display(); break;case 4: exit(0);default:exit(0);}}getch();}void insert()

  • {printf(Enter ur data);newn=malloc(sizeof(struct queue));scanf(%d,&newn->data);newn->next=null;if(rear= =null){front=newn;rear=newn;}else{rear->next=newn;rear=newn;}}void delete(){

  • if(front= =NULL){printf(\nQueue is empty\n);rear=null;}else{p=front;x=p->data;front=p->next;printf(\n Deleted value is %d\n,x);free(p);}}void display(){if(front= =null){

  • printf(\nqueue is empty\n);}else{printf(The Queue elements are:);for(p=front;p!=null;p=p->next)printf(\t%d,p->data);printf(\n);}}

    Sample outputQUEUE USING LINKED LIST

    1.Insert2.Delete3.Display4.ExitEnter ur option: 1

  • Enter ur data 55Enter ur option: 1Enter ur data 33Enter ur option: 1Enter ur data 44Enter ur option: 1Enter ur data 22Enter ur option: 3The queue elements are: 55 33 44 22Enter ur option: 2Deleted value is 55Enter ur option: 2Deleted value is 33Enter ur option: 2Deleted value is 44Enter ur option: 2Deleted value is 22Enter ur option: 2

  • Queue is emptyEnter ur option: 3Queue is emptyEnter ur option: 4

    ResultThus the program was successfully executed

    and the output was verified.

  • 9. BINARY TREE TRAVERSALAim

    To find the inorder, preorder and postordertraversal of a binary tree.AlgorithmStep 1: Start the programStep 2: Initialize the left child and right child toform a binary tree continue the process until theleft child and right child doesnt exit.Step 3: get the root node and get its left child andright child to form a binary tree continue theprocess until the left child and right child doesntexit.Step 4: For inorder traversal first traverse the leftchild then root node and finally right child.Step 5: For preorder traversal, first traverse theroot node , then left child and finally child.Step 6: For postorder traversal, first traverse theleft child, right child and finally the root nodeStep 7: Stop the program.

  • Program/*Binary Tree Traversal*/#include#include#include#includestruct node{int data;struct node *right,*left;}*root,*p,*q;struct node *make(int y){struct node *newnode;newnode=(struct node *)malloc(sizeof(structnode));newnode->data=y;

  • newnode->right=newnode->left=NULL;return(newnode);}void left(struct node *r, int x){if(r->left!=NULL)printf(\n Invalid !);elser->left=make(x);}void right(struct node *r,int x){if(r->right!=NULL)printf(\n Invalid !);elser->right=make(x);}void inorder(struct node *r)

  • {if(r!=NULL){inorder(r->left);printf(\t %d,r->data);inorder(r->right);}}void preorder(struct node *r){if(r!=NULL){printf(\t %d,r->data);preorder(r->left);preorder(r->right);}}void postorder(struct node *r)

  • {if(r!=NULL){postorder(r->left);postorder(r->right);printf(\t %d,r->data);}}void display(struct node *r, int c, int r1, int w){if(r!=NULL){if(r){gotoxy(c,r1);printf(%d,r->data);if(r->left)display(r->left,c-w,r1+2,w/2);

  • if(r->right)display(r->right,c+w,r1+2,w/2);}}}void main(){int no;int choice;clrscr();printf(\n enter the root:);scanf(%d,&no);root=make(no);p=root;while(1){printf(\n Enter another number:);scanf(%d,&no);

  • if(no= =-1)break;p=root;q=root;while(no!=p->data && q!=NULL){p=q;if(nodata)q=p->left;elseq=p->right;}if(nodata){printf(\n left branch of %d is %d,p->data,no);left(p,no);}else

  • {right(p,no);printf(\n right branch of %d is %d,p->data,no);}}while(1){printf(\n 1.Inorder Traversal \n 2.PreorderTraversal\n 3.Postorder Traversal\n 4.Display\n5.Exit);printf(\n Enter choice:);scanf(%d,&choice);switch(choice){case 1: inorder(root);break;case 2: preorder(root);break;

  • case 3: postorder(root);break;case 4:{clrscr();display(root,40,5,16);getch();break;}case 5: exit(0);default:printf(Error ! Invalid Choice);break;}getch();}}

  • Sample outputBINARY TREE TRAVERSAL

    Enter the root: 5Enter another number: 3Left branch of 5 is 3Enter another number: 6Right branch of 5 is 6Enter another number :2Left branch of 3 is 2Enter another number: 8Right branch of 6 is 8Enter another number: -11.Inorder traversal2.Preorder traversal3.Postorder traversal4.display5. exitEnter choice: 4

  • 53 6

    2 81.Inorder traversal2.Preorder traversal3.Postorder traversal4.display5. exitEnter choice:12 3 5 6 81.Inorder traversal2.Preorder traversal3.Postorder traversal4.display5. exitEnter choice:25 3 2 6 8

  • 1.Inorder traversal2.Preorder traversal3.Postorder traversal4.display5. exitEnter choice:32 3 8 6 5

    1.Inorder traversal2.Preorder traversal3.Postorder traversal4.display5. exitEnter choice:5

    ResultThus the program was successfully executed

    and the output was verified.

  • 10. BINARY SEARCH TREEAim

    Write a C program to perform Binary searchtree.AlgorithmStep 1: Start the programStep 2: First check the given searching value tothe root node of the tree.

    (i).if the root node is equal to the searchnode then displayelement found.Step 3: compare root node and search node.

    (i).if searchnode is greater than root node.(ii).check right-> node

    Else(iii).check left->node

    Step 4: Repeat the step 3 process when leaf nodeis reached

    If it found display the element is foundElse

  • Display the element not found.Step 5: Stop the program.Program/*Binary Search Tree*/#include#include#include#includestruct node{int data;struct node *right,*left;}*root,*p,*q;struct node *make(int y){struct node *newnode;

  • newnode=(struct node *)malloc(sizeof(structnode));newnode->data=y;newnode->right=newnode->left=NULL;return(newnode);}void left(struct node *r,int x){if(r->left!=NULL)printf(\n Invalid !);elser->left=make(x);}void right(struct node *r,int x){if(r->right!=NULL)printf(\n Invalid !);else

  • r->right=make(x);}void search(struct node *r,int t){if(r!=NULL){if(r->data= =t)printf(Element found);else if(r->data>t&&(r->left | | r->right!=NULL)){search(r->left,t);}else if(r->dataleft | | r->right!=NULL))search(r->right,t);elseprintf(Element not found);}}

  • void display(struct node *r, int c,int r1,int w){if(r!=NULL){if(r){gotoxy(c,r1);printf(%d,r->data);if(r->left)display(r->left,c-w,r1+2,w/2);if(r->right)display(r->right,c+w,r1+2,w/2);}}}void main(){int no;

  • int choice;clrscr();printf(\n enter the root:);scanf(%d,& no);root=make(no);p=root;while(1){printf(\n Enter another number:);scanf(%d,&no);if(no= =-1)break;p=root;q=root;while(no!=p->data && q!=NULL){p=q;if(nodata)

  • q=p->left;elseq=p->right;}if(nodata){printf(\n Left branch of %d is %d,p->data,no);left(p,no);}else{right(p,no);printf(\n Right branch of %d is %d,p->data,no);}}While(1){printf(\n\n\n\n 1.search\n 2.display \n 3.exit);

  • printf(\n Enter choice:);scanf(%d,&choice);switch(choice){case 1:{int t;printf(Enter the element to be searched);scanf(%d,&t);search(root,t);break;}case 2:{clrscr();display(root,40,5,16);getch();break;

  • }case 3: exit(0);default:printf(error ! invalid choice);break;}getch();}}Sample output:

    BINARY SEARCH TREEEnter the root: 5Enter another number: 4Left branch of 5 is 4Enter another number: 6Right branch of 5 is 6Enter another number: 3Left branch of 4 is 3Enter another number: -1

  • 1.search2.display3.exitEnter choice: 2

    54 6

    3

    1.search2.display3.exitEnter choice: 1Enter the element to be searched 3Element found1.search2.display3.exitEnter choice: 1

  • Enter the element to be searched 6Element found1.search2.display3.exitEnter choice: 1Enter the element to be searched 0Element not found1.search2.display3.exitEnter choice: 3

    ResultThus the program was successfully executed

    and the output was verified.

  • 11(a).BUBBLE SORTAim

    Write a C program to start the given set ofnumbers using bubble sort technique.AlgorithmStep 1: start the program.Step 2: get the inputs for the array.Step 3: bubble sort the smallest element to placefrom sorting of the array by using a nested for loopand swap the element in position i,j-1. If they areout of orders.Step 4: print the values of sorted elements.Step 5: Stop the program.

    Program//Bubble sort#include#includevoid bubble (int a [],int n)

  • {int i,j,t;for(i=n-2;i>=0;i--){for(j=0;ja[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}}//end for 1.}//end function.void main(){int a[100],n,i;

  • clrscr();printf(\n\n Enter integer value for total no.s ofelements to be sorted:);scanf(%d,&n);for(i=0;i
  • Sample outputBUBBLE SORT

    Enter integer value for total no.s of elements to besorted: 6Enter integer value for element no.1: 88Enter integer value for element no.2: -8Enter integer value for element no.3: -66Enter integer value for element no.4: 10Enter integer value for element no.5: 77Enter integer value for element no.6: 20Finally sorted array is: -66 -8 10 20 77 88

    ResultThus the program was successfully executed

    and the output was verified.

  • 11(b).MERGE SORTAim

    To sort the given set of numbers using mergesort technique.AlgorithmStep 1: start the program.Step 2: get the input values of 2 arrays as a [m]and b[n] and n as its array size.Step 3: sort the contents of array a[] and b[].Step 4: in merge()

    (i). it contains two sorted arrays a[m]and b[n] into one.(ii). Scan both arrays from left to right.(iii).compare a[1] with b[1] if a[1] puta[1] as a[1] as compare a[2] with b[1]and so on. Else put b[1] as c[1] andcompare a[1] with b[2] and so on.(iv).repeat step until any one arraybecomes empty.

  • (v).check a array becomes empty, storeall the remaining array elements in thesame order in the C array.(vi)Besides are store all the remaining aarray elements in the same order in theC array.

    Step 5: Stop the program.

    Program//merge sort#include#includevoid main(){int a[100],b[100],c[100],i,j,n;clrscr();printf(Enter the array size\t);scanf(%d,&n);

  • printf(\n Enter the %d values \n,n);for(i=0;i
  • for(i=0;i
  • {{if(a[i]>b[i]){c[k]=b[i];k++;c[k]=a[i];k++;}else{c[k]=a[i];k++;c[k]=b[i];k++;}}}

  • return c;}

    Sample outputMERGE SORT

    Enter the array size 5Enter the 5 values55 11 22 66 44Enter the another 5 values21 07 59 88 99The sorted values are:7112122445559

  • 668899

    ResultThus the program was successfully executed

    and the output was verified.

  • 11(c). QUICK SORTAim

    Write a C program to sort the given set ofnumber using quicksort technique.AlgorithmStep 1: Start the program.Step 2: Get the inputs for the array.Step 3: For a given array A[p:n],if R>n; thenpartition of the given array into two non-empty subarrays

    (i).then the pivot element is q then q isequal to (p+n)/2. p is a starting point (size of thearray) and n is end point (size of the array).

    (ii).A[p:q] and A[q+1:n],such that everykey in A[p:q] is less than or equal to every key inA[q+1:n].Step 4: The two sub arrays are sorted byrecursive calls to quick sortStep 5:print the values to be sorted elementsStep 6: Stop the program.

  • Program//Quick sort#include#includevoid swap(int *x, int *y){int temp;temp = *x;*x = *y;*y = temp;}int choose_pivot(int i,int j){return((i+j)/2);}void quicksort(int list[],int m,int n){int key,i,j,k;

  • if(m
  • quick sort(list,m,j-1);quick sort(list,j+1,n);}}void printlist(int list[],int n){int i;for(i=0;i
  • for(i=0;i
  • 5011887722The list before sorting is:50 11 88 77 22The list after sorting using quicksort algorithm:11 22 50 77 88

    ResultThus the program was successfully executed

    and the output was verified.

  • 11(d). RADIX SORTAim:

    Write a C program to implement RadixSort Using Data Structures.

    Algorithm:i) Start the program.ii) Get the values.iii) Values of the Actual Digits in the

    positional representations of the numbers beingsorted.

    iv) Start at the most-significant digit andadvance through the least-significant digits as longas the corresponding digits in the two numbersmatch.

    v) Using decimal base , the numbers can bepartitioned into ten groups based on their mostsignificant digit.

  • vi) Each number in the order in which itappears in the file and place it into one of tenqueues.

    vii) Restore each queue to the original filestarting with the queue of numbers with a 0 digitand ending with the queue of numbers with a 9digit.

    viii) Stop the program.

    Program#include#includeradix_sort(int array[], int n);void main(){int array[100],n,i;clrscr();printf("Enter the number of elements to be sorted:");scanf("%d",&n);printf("\n Enter the elements to be sorted: \n");for(i =0;i

  • printf("\tArray[%d] = ",i);scanf("%d",&array[i]);}printf("\n Array Before Radix Sort:"); //ArrayBefore Radix Sortfor(i =0;i
  • int i,j,k,l,num,div,large,passes;div=1;num=0;large=arr[0];for(i=0; i large){large = arr[i];}while(large > 0){num++;large = large/10;}for(passes=0; passes
  • l = ((arr[i]/div)%10);bucket[l][buck[l]++] = arr[i];}i=0;for(k=0;k
  • Array[3]=341Array[4]=265Array[5]=989Array[6]=506Array before radix sort: 777 269 158 341 265 989506Array after radix sort: 158 265 269 341 506 777989

    ResultThus the program was successfully executed

    and the output was verified.

  • 12(a).Linear SearchAim

    To develop a C program to implement linearsearch.AlgorithmStep 1: Get the array elements.Step 2: Get the element to search.Step 3: Apply for loop differentiate the arrayelement and apply the below condition in for loop.

    (i).check the array element is equal tosearch element then display element foundElse

    Display element not found.Step 4: Stop the program.

    Program//Linear search#include

  • #includevoid main(){int i,n,item,a[20];clrscr();printf(\n Enter no of elements=);scanf(%d,&n);printf(\n Enter %d elements=,n);for(i=0;i
  • printf(\n\t%d is present at position %d,a[i],i+1);break;}}if(i= =n)printf(\n Element is not present);getch();}Sample output

    LINEAR SEARCHEnter no of elements =5Enter 5 elements = 55 44 66 33 22Enter the element to be search= 3333 is present at position 4Enter no of elements = 5Enter 5 elements = 55 44 66 33 22Enter the element to be search = 11Element is not present

  • ResultThus the program was successfully executed andthe output was verified.

  • 12(b).BINARY SEARCHAim

    To develop a C program to implement binarysearch.AlgorithmStep 1: Get the array elements.Step 2: Get the elements to search.Step 3: Sort the array element using any sortingtechniques.Step 4: Get the mid value by (low+high)/2.Step 5: Compare the search element with that midelements.Step 6: Check the given value is less than the midelement, then find mid=(low+mid-1)/2 else, findmid=(mid+1+high/2).Step 7: Repeat the steps 4 to 6 until the element isfound or high

  • Step 9: Stop the program.

    Program//Binary search#include#includevoid main (){int i,n,item,a[100],b;clrscr();printf(\n Enter no of elements=);scanf(%d,&n);printf(\n Enter %d elements=,n);for(i=1;i

  • printf(\n The sorted values are :\n);for(i=0;iitem){for(i=1;i
  • printf(\n\tElement found);break;}if(i= =(b+1))printf(\nElement not found);}}else if(a[b]
  • elseprintf(\n\tElement not found);getch();}sort(int a[],int n){int i,j,t;for(i=1;i
  • }return 0;}

    Sample outputBINARY SEARCH

    Enter no of elements = 5Enter 5 elements = 22 11 55 44 33The sorted values are:1122334455Enter the element to be search= 33Element foundEnter no of elements = 5Enter 5 elements = 22 11 55 44 33

  • The sorted values are: 24Element not found.

    ResultThus the program was successfully executed

    and the output was verified.