+ All Categories
Home > Documents > DS With C C++ Lab Manual VTU

DS With C C++ Lab Manual VTU

Date post: 04-Jun-2018
Category:
Upload: manohar-nv
View: 250 times
Download: 2 times
Share this document with a friend

of 50

Transcript
  • 8/13/2019 DS With C C++ Lab Manual VTU

    1/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 1 - NARENDRA KUMAR S AND MANOHAR V NELLI

    JAWAHARLAL NEHRU NATIONAL COLLEGE OF

    ENGINEERING

    DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

    LAB MANUAL

    ON

    DATA STRUCTURES WITH C/C++ LABORATORY (10CSL37)

    PREPARED BY:

    NARENDRA KUMAR S

    MANOHAR V NELLI

  • 8/13/2019 DS With C C++ Lab Manual VTU

    2/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 2 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /*ASSIGNMENT 1: Using circular representation for a polynomial, design, develop, and execute aprogram in C to accept two polynomials, add them, and then print the resulting polynomial. */#include#include#include

    typedef struct

    {int c, e;

    }poly;

    poly *term;

    int av=0,sa=-1,fa=-1,sb=-1,fb=-1,sd=-1,fd=-1,sum=0;

    void attach(int coef,int exp){

    term[av].c=coef;

    term[av++].e=exp;}

    void read(int s,int f,int n){

    int i=0,j;if(n==0){printf("zero polynomial\n");}else

    {for(j=s;jy)return 1;

    elsereturn -1;

    }

  • 8/13/2019 DS With C C++ Lab Manual VTU

    3/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 3 - NARENDRA KUMAR S AND MANOHAR V NELLI

    void padd(){

    sd=av;while(sa

  • 8/13/2019 DS With C C++ Lab Manual VTU

    4/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 4 - NARENDRA KUMAR S AND MANOHAR V NELLI

    void main(){

    int n,m;clrscr();printf("enter the number of terms for first polynomial\n");scanf("%d",&n);printf("enter the number of terms for second polynomial\n");

    scanf("%d",&m);term=(poly*)malloc(sizeof(poly)*((n+m)*2));sa=0;fa=sa+n-1;sb=fa+1;fb=sb+m-1;av=fb+1;printf("enter the coef and exp for first polynomial\n");read(sa,fa,n);printf("enter the coef and exp for second polynomial\n");read(sb,fb,m);

    printf(" first polynomial A(x) =");display(sa,fa);printf("\n second polynomial B(x) =");display(sb,fb);printf("\n______________________________________________________\n");padd();printf("\nResultant polynomial D(x)=");display(sd,fd);getch();

    }

  • 8/13/2019 DS With C C++ Lab Manual VTU

    5/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 5 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 1: Using circular representation for a polynomial, design, develop, and executea program in C to accept two polynomials, add them, and then print the resulting polynomial. */

    #include#include

    #include#include

    struct node{

    int coef,exp;struct node *link;

    };struct node *a,*b,*c;int n,m,sum=0;

    struct node* read(int n){struct node *temp,*x=NULL,*last;int e,c,i;for(i=0;icoef=c;temp->exp=e;

    temp->link=NULL;if(x==NULL){x=temp;x->link=x;

    }else{last=x;while(last->link!=x){

    last=last->link;}temp->link=x;last->link=temp;

    }}return x;

    }void display(struct node *a){

    struct node *temp;

  • 8/13/2019 DS With C C++ Lab Manual VTU

    6/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 6 - NARENDRA KUMAR S AND MANOHAR V NELLI

    int i;if(a==NULL){

    printf("empty polynomial equation\n");}else{

    for(temp=a;temp->link!=a;temp=temp->link){printf("%dx^%d+",temp->coef,temp->exp);}printf("%dx^%d",temp->coef,temp->exp);

    }}

    int compare(int x,int y){

    if(x>y)

    return 1;else if(xcoef=co;temp->exp=ex;temp->link=NULL;if(c==NULL){c=temp;c->link=c;

    }else{last=c;

    while(last->link!=c){last=last->link;

    }last->link=temp;temp->link=c;}

    }

  • 8/13/2019 DS With C C++ Lab Manual VTU

    7/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 7 - NARENDRA KUMAR S AND MANOHAR V NELLI

    void padd(){

    int c1=n,c2=m;struct node *t1,*t2;while(c1!=0&&c2!=0){switch(compare(a->exp,b->exp))

    {case 1: attach(a->coef,a->exp);

    t1=a;a=a->link;free(t1);c1--;break;

    case -1:attach(b->coef,b->exp);t2=b;b=b->link;free(t2);

    c2--;break;case 0: sum=a->coef+b->coef;

    if(sum){attach(sum,a->exp);}t1=a;t2=b;a=a->link;b=b->link;

    free(t1);free(t2);c1--;c2--;break;

    }}for(;c1!=0;){attach(a->coef,a->exp);t1=a;

    a=a->link;free(t1);c1--;

    }for(;c2!=0;){attach(b->coef,b->exp);t2=b;b=b->link;free(t2);c2--;

  • 8/13/2019 DS With C C++ Lab Manual VTU

    8/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 8 - NARENDRA KUMAR S AND MANOHAR V NELLI

    }}

    void main(){

    clrscr();a=NULL;b=NULL;c=NULL;printf("enter the no of terms for 1st polynomial equation\n");scanf("%d",&n);printf("enter the no of terms for 2nd polynomial equation\n");scanf("%d",&m);if (n==0||m==0){printf("empty polynomial equation cant be added");

    getch();exit(0);}printf("enter the elements for first poly\n");a=read(n);printf("enter the elements for second poly\n");b=read(m);printf("\t polynomial A(X)= ");display(a);printf("\n\t polynomial B(X)= ");display(b);

    padd();printf("\n____________________________");printf("\nresultant polynomial D(X)= ");display(c);getch();

    }

  • 8/13/2019 DS With C C++ Lab Manual VTU

    9/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 9 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 2:Design, develop, and execute a program in C to convert a given validparenthesized infix arithmetic expression to postfix expression and then to print both theexpressions. The expression consists of single character operands and the binary operators + (plus),- (minus), * (multiply) and / (divide).*/

    #include#include

    #include#include

    int F(char); //prototypeint G(char); //prototype

    int top=-1,i,j;char *s,*postfix,*infix,symbol;

    void infix_postfix(){

    j=0;s[++top]='#';for(i=0;iG(symbol)){postfix[j]=s[top--];j++;}if(F(s[top])!=G(symbol))

    {s[++top]=symbol;}else{top--;}

    }while(s[top]!='#'){postfix[j]=s[top--];

    j++;}postfix[j]='\0';printf("postfix expression is %s \n ",postfix);

    }

    int F(char symbol){

    switch(symbol){case '+':

  • 8/13/2019 DS With C C++ Lab Manual VTU

    10/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 10 - NARENDRA KUMAR S AND MANOHAR V NELLI

    case '-':return 2;case '*':case '/':return 4;case '^':case '$':return 5;case '(':return 0;case '#':return -1;

    default :return 8;}

    }

    int G(char symbol){

    switch(symbol){case '+':case '-':return 1;case '*':

    case '/':return 3;case '^':case '$':return 6;case '(':return 9;case ')':return 0;default: return 7;

    }}

    void main(){

    int n;clrscr();printf("enter the size of the infix expression\n");scanf("%d",&n);infix=(char *)malloc(sizeof(char)*n+1);postfix=(char *)malloc(sizeof(char)*n+1);s=(char *)malloc(sizeof(char)*n+1);printf("enter the infix expression\n");scanf("%s",infix);infix_postfix();getch();

    }

  • 8/13/2019 DS With C C++ Lab Manual VTU

    11/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 11 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 2: Design, develop, and execute a program in C to convert a given validparenthesized infix arithmetic expression to postfix expression and then to print both theexpressions. The expression consists of single character operands and the binary operators + (plus),- (minus), * (multiply) and / (divide).*/

    #include#include

    #include#include#include#include

    int F(char);int G(char);

    int top=-1,i,j;char *s,*postfix,*infix,symbol;

    void infix_postfix(){j=0;s[++top]='#';for(i=0;iG(symbol)){postfix[j]=s[top--];j++;

    }if(F(s[top])!=G(symbol)){s[++top]=symbol;

    }else{top--;}

    }while(s[top]!='#')

    {postfix[j]=s[top--];j++;}postfix[j]='\0';printf("postfix expression is %s \n ",postfix);

    }

    int F(char symbol){

    switch(symbol)

  • 8/13/2019 DS With C C++ Lab Manual VTU

    12/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 12 - NARENDRA KUMAR S AND MANOHAR V NELLI

    {case '+':case '-':return 2;case '*':case '/':return 4;case '^':case '$':return 5;

    case '(':return 0;case '#':return -1;default :return 8;

    }}

    int G(char symbol){

    switch(symbol){

    case '+':case '-':return 1;case '*':case '/':return 3;case '^':case '$':return 6;case '(':return 9;case ')':return 0;default :return 7;

    }}

    void validate(){

    int count1=0,count2=0,i,flag1=0,flag2=0,top=-1,j,k;char s[20];for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    13/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 13 - NARENDRA KUMAR S AND MANOHAR V NELLI

    s[++top]='(';if(infix[i]==')')top--;

    }for (j=0;j

  • 8/13/2019 DS With C C++ Lab Manual VTU

    14/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 14 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 3: Design, develop, and execute a program in C to evaluate a valid postfixexpression using stack. Assume that the postfix expression is read as a single line consisting ofnon-negative single digit operands and binary arithmetic operators. The arithmetic operators are +(add), - (subtract), * (multiply) and / (divide). */

    #include#include

    #include#include#include

    int *s,top=-1,res=0,op1,op2;char *postfix, symbol;

    int compute(char symbol,int op1,int op2){

    int result;switch(symbol)

    {case '+':result=op1+op2;break;

    case '-':result=op1-op2;break;

    case '*':result=op1*op2;break;

    case '/':result=op1/op2;break;

    }return result;

    }

    void evaluate(){

    int i;for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    15/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 15 - NARENDRA KUMAR S AND MANOHAR V NELLI

    void main(){

    int n;clrscr();printf("Enter the size of postfix expression\n");scanf("%d",&n);postfix=(char*)malloc((sizeof(char)*n)+1);

    s=(int*)malloc(sizeof(int)*n);printf("Enter the valid postfix expression\n");scanf("%s",postfix);evaluate();getch();

    }

  • 8/13/2019 DS With C C++ Lab Manual VTU

    16/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 16 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 3: Design, develop, and execute a program in C to evaluate a valid postfixexpression using stack. Assume that the postfix expression is read as a single line consisting ofnon-negative single digit operands and binary arithmetic operators. The arithmetic operators are +(add), - (subtract), * (multiply) and / (divide). */

    #include#include

    #include#include#include

    int *s,top=-1,res,op1,op2;char *postfix, symbol;

    int compute(char symbol,int op1,int op2){

    int result;switch(symbol)

    {case '+':result=op1+op2;break;

    case '-':result=op1-op2;break;

    case '*':result=op1*op2;break;

    case '/':result=op1/op2;break;

    }return result;

    }

    void evaluate(){

    int i;for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    17/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 17 - NARENDRA KUMAR S AND MANOHAR V NELLI

    void validate(){

    int count1=0,count2=0,i,j;for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    18/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 18 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 4: Design, develop, and execute a program in C to simulate the working of aqueue of integers using an array. Provide the following operations: a. Insert b. Delete c. Display */

    #include#include#include#include

    int *q, size, f=0,r=-1;

    void insert(){int ele;if(r==(size-1)){printf("Queue overflow\n");}else

    {printf("enter the elements to be inserted\n");scanf("%d",&ele);r=r+1;q[r]=ele;

    }}

    void deletq(){

    if(r==-1)

    {printf("Queue underflow\n");}else{printf("deleted element=%d\n",q[f]);f=f+1;if(f>r){f=0;r=-1;

    }}}

    void display(){

    int i;if(r==-1){printf("Queue underflow\n");}

  • 8/13/2019 DS With C C++ Lab Manual VTU

    19/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 19 - NARENDRA KUMAR S AND MANOHAR V NELLI

    else{printf("Queue elements are\n");for(i=f;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    20/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 20 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 5: Design, develop, and execute a program in C++ based on the followingrequirements:An EMPLOYEE class is to contain the following data members and member functions:Data members: Employee_Number (an integer), Employee_Name (a string of characters),Basic_Salary (an integer) ,All_Allowances (an integer), IT (an integer), Net_Salary (an integer).Member functions: to read the data of an employee, to calculate Net_Salary and to print the values

    of all the data members. (All_Allowances = 123% of Basic; Income Tax (IT) = 30% of the grosssalary (= basic_Salary - All_Allowance); Net_Salary =( Basic_Salary + All_Allowances - IT) */

    #include#include

    class EMPLOYEE{

    char employee_name[20];int employee_number;int basic_salary;

    int allowance;int gross_salary;int IT;int net_salary;

    public:void getdata();void compute();void display();

    };

    void EMPLOYEE::getdata()

    { cin>>employee_name>>employee_number>>basic_salary;}

    void EMPLOYEE::compute(){

    allowance=basic_salary*1.23;gross_salary=basic_salary+allowance;IT=gross_salary*0.3;net_salary=gross_salary-IT;

    }

    void EMPLOYEE::display(){

    cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    21/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 21 - NARENDRA KUMAR S AND MANOHAR V NELLI

    {int n,i;clrscr();coutn;EMPLOYEE *e=new EMPLOYEE [n];for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    22/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 22 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 5: Design, develop, and execute a program in C++ based on the followingrequirements:An EMPLOYEE class is to contain the following data members and member functions:Data members: Employee_Number (an integer), Employee_Name (a string of characters),Basic_Salary (an integer) ,All_Allowances (an integer), IT (an integer), Net_Salary (an integer).Member functions: to read the data of an employee, to calculate Net_Salary and to print the valuesof all the data members. (All_Allowances = 123% of Basic; Income Tax (IT) = 30% of the gross

    salary (= basic_Salary - All_Allowance); Net_Salary =(Basic_Salary + All_Allowances - IT) */

    #include#include#include

    class EMPLOYEE{

    char employee_name[20];int employee_number;int basic_salary;

    int allowance;int gross_salary;int IT;int net_salary;

    public: void getdata();void compute();void display();

    };

    void EMPLOYEE::getdata(){

    cin>>employee_name>>employee_number>>basic_salary;}

    void EMPLOYEE::compute(){

    allowance=basic_salary*1.23;gross_salary=basic_salary+allowance;IT=gross_salary*0.3;net_salary=gross_salary-IT;

    }

    void EMPLOYEE::display(){cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    23/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 23 - NARENDRA KUMAR S AND MANOHAR V NELLI

    void main(){

    int n,i;

    clrscr();coutn;EMPLOYEE *e=new EMPLOYEE [n];for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    24/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 24 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 6: Design, develop, and execute a program in C++ to create a class calledSTRING and implement the following operations. Display the results after every operation byoverloading the operator

  • 8/13/2019 DS With C C++ Lab Manual VTU

    25/50

  • 8/13/2019 DS With C C++ Lab Manual VTU

    26/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 26 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 6: Design, develop, and execute a program in C++ to create a class calledSTRING and implement the following operations. Display the results after every operation byoverloading the operator

  • 8/13/2019 DS With C C++ Lab Manual VTU

    27/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 27 - NARENDRA KUMAR S AND MANOHAR V NELLI

    strcpy(t.str,str);strcat(t.str," ");strcat(t.str,s.str);return t;

    }

    void main()

    {char a[100],b[100];clrscr();couta;coutb;STRING s1=a;STRING s2=b;cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    28/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 28 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 7: Design, develop, and execute a program in C++ to create a class calledSTACK using an array of integers and to implement the following operations by overloading theoperators + and - :i. s1=s1 + element; where s1 is an object of the class STACK and element is an integer to bepushed on to top of the stack.ii. s1=s1-1 ; where s1 is an object of the class STACK andoperator pops off the top element.

    Handle the STACK Empty and STACK Full conditions. Also display the contents of the stackafter each operation, by overloading the operator

  • 8/13/2019 DS With C C++ Lab Manual VTU

    29/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 29 - NARENDRA KUMAR S AND MANOHAR V NELLI

    cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    30/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 30 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 8: Design, develop, and execute a program in C++ to create a class called LIST(linked list) with member functions to insert an element at the front of the list as well as todelete an element from the front of the list. Demonstrate all the functions after creating a list object.*/

    #include

    #include#include

    struct node{

    int data;struct node *link;

    };

    class LIST{

    struct node *first;public: LIST(){first=NULL;

    }void insert_front(int);void delet_front();void display();

    };

    void LIST::insert_front(int ele)

    { struct node *temp;temp=new (struct node);temp->data=ele;temp->link=NULL;if(first==NULL){first=temp;

    }else{

    temp->link=first;first=temp;}

    }

    void LIST::delet_front(){

    struct node *temp;if(first==NULL){cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    31/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 31 - NARENDRA KUMAR S AND MANOHAR V NELLI

    }else if(first->link==NULL){cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    32/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 32 - NARENDRA KUMAR S AND MANOHAR V NELLI

    break;case 3:l.display();

    break;case 4:exit(0);default:cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    33/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 33 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 9: Design, develop, and execute a program in C to read a sparse matrix ofinteger values and to search the sparse matrix for an element specified by the user. Print the resultof the search appropriately. Use the triple to represent an element in thesparse matrix. */

    #include

    #include#includetypedef struct{

    int r,c,v;}sm;

    sm *a;

    void read()

    { int i,j,r1,c1,ele,k=1,m,flag=0;printf("enter the row size\n");scanf("%d",&r1);printf("enter the column size\n");scanf("%d",&c1);m=r1*c1;if (m%2==0){a=(sm*)malloc(sizeof(sm)*(m/2));flag=1;

    }elsea=(sm*)malloc(sizeof(sm)*(m/2+1));

    a[0].r=r1;a[0].c=c1;printf("enter the sparse matrix elements\n");for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    34/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 34 - NARENDRA KUMAR S AND MANOHAR V NELLI

    printf("entered is not a sparse matrix\n");getch();exit(0);

    }if ((a[0].v>=(m/2+1))&&flag==0){printf("entered is not a sparse matrix\n");

    getch();exit(0);

    }}

    void display(){ int i;

    printf("sparse matrix is\n");printf("\trow\tcol\tvalue\n");printf("____________________________________\n");for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    35/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 35 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 10: Design, develop, and execute a program in C to create a max heap ofintegers by accepting one element at a time and by inserting it immediately in to the heap. Use thearray representation for the heap. Display the array at the end of insertion phase. */

    #include#include#include

    #include

    int *heap,n,ele,count=0;

    void insert(int ele){

    int i,j;if(count==n){printf("Heap is full\n");

    }else{for (j=1;jheap[i/2])){heap[i]=heap[i/2];i=i/2;

    }heap[i]=ele;

    }}

    void display()

    { int i;if(count==0){printf("Heap is empty\n");}else{printf("contents of the heap are\n");for(i=1;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    36/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 36 - NARENDRA KUMAR S AND MANOHAR V NELLI

    printf("%d\t",heap[i]);}printf("\n");}

    }

    void main()

    {int ch;clrscr();printf("enter the size of heap\n");scanf("%d",&n);heap=(int*)malloc(sizeof(int)*(n+1));while(1){printf("1->insert\n2->display\n3->exit\n");printf("enter your choice\n");scanf("%d",&ch);

    switch(ch){case 1: printf("enter the item to be insert\n");

    scanf("%d",&ele);insert(ele);display();break;

    case 2: display();break;

    case 3: exit(0);default: printf("invalid choice\n");

    }}}

  • 8/13/2019 DS With C C++ Lab Manual VTU

    37/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 37 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 11: Design, develop, and execute a program in C to implement a doubly linkedlist where each node consists of integers. The program should support the following operations:i. Create a doubly linked list by adding each node at the front.ii. Insert a new node to the left of the node whose keyvalue is read as an input.iii. Delete the node of a given data if it is found, otherwise display appropriate message.iv. Display the contents of the list.

    (Note: Only either (a, b and d) or (a, c and d) may be asked in the examination) */

    #include#include#include#include

    struct DLL{

    int data;struct DLL *llink;

    struct DLL *rlink;};//typedef can also be used

    struct DLL *first=NULL;

    void create(int n){

    int ele,i;struct DLL *temp;for(i=0;idata=ele;temp->llink=NULL;temp->rlink=NULL;if (first==NULL)first=temp;

    else{temp->rlink=first;

    first->llink=temp;first=temp;}

    }}

    void insertkey(){

    int key,ele;struct DLL *temp,*prev,*cur;if (first==NULL)

  • 8/13/2019 DS With C C++ Lab Manual VTU

    38/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 38 - NARENDRA KUMAR S AND MANOHAR V NELLI

    {printf("empty linked list\n");return;

    }temp=(struct DLL*)malloc(sizeof(struct DLL));printf("enter the key \n");scanf("%d",&key);

    printf("enter the element \n");scanf("%d",&ele);temp->data=ele;temp->llink=temp->rlink=NULL;if(key==first->data){temp->rlink=first;first->llink=temp;first=temp;

    }else

    {prev=NULL;cur=first;while(key!=cur->data&&cur!=NULL){prev=cur;cur=cur->rlink;

    }if(key!=cur->data){printf("key not found\n");

    return;}prev->rlink=temp;temp->llink=prev;temp->rlink=cur;cur->llink=temp;

    }}

    void deletekey(){

    int key;struct DLL *temp,*prev,*cur,*next;if(first==NULL){printf("Empty Doubly Linked List \n");return;

    }printf("enter the key to be deleted\n");scanf("%d",&key);if(key==first->data){

  • 8/13/2019 DS With C C++ Lab Manual VTU

    39/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 39 - NARENDRA KUMAR S AND MANOHAR V NELLI

    temp=first;first=first->rlink;first->llink=NULL;printf("key found! and deleted i.e=%d\n",temp->data);free(temp);

    }else

    {prev=NULL;cur=first;while(key!=cur->data&&cur!=NULL){prev=cur;cur=cur->rlink;

    }if(cur==NULL){printf("key not found \n");

    return;}next=cur->rlink;prev->rlink=next;next->llink=prev;printf("key found! and deleted i.e = %d\n",cur->data);free(cur);

    }}

    void display()

    { struct DLL *temp;if(first==NULL){printf("Empty Doublu Linked List \n");}else{printf("contents of DLL\n");printf("NULL");for(temp=first;temp!=NULL;temp=temp->rlink)

    {printf("%d",temp->data);}printf("NULL \n");}

    }

    void main(){

    int ch,n;clrscr();

  • 8/13/2019 DS With C C++ Lab Manual VTU

    40/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 40 - NARENDRA KUMAR S AND MANOHAR V NELLI

    printf("enter the number of nodes\n");scanf("%d",&n);create(n);while(1){printf("1->insert left \n2->delete key \n3->display \n4->exit \n");printf("Enter your choice\n");

    scanf("%d",&ch);switch(ch){case 1: insertkey();

    break;case 2: deletekey();

    break;case 3: display();

    break;case 4:exit(0);default: printf("invalid choice\n");

    }}}

  • 8/13/2019 DS With C C++ Lab Manual VTU

    41/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 41 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 12: Design, develop, and execute a program in C++ to create a class calledDATE with methods to accept two valid datesin the form dd/mm/yy and to implement thefollowing operations by overloading the operators + and -. After every operation the results are tobe displayed by overloading the operator

  • 8/13/2019 DS With C C++ Lab Manual VTU

    42/50

  • 8/13/2019 DS With C C++ Lab Manual VTU

    43/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 43 - NARENDRA KUMAR S AND MANOHAR V NELLI

    DATE d;d.day=day;d.month=month;d.year=year;for(int i=1;ib[d.month]){d.day=1;d.month++;}

    }else if(d.day>a[d.month]){d.day=1;d.month++;

    }

    if(d.month>12){d.month=1;d.year++;

    }}return d;

    }

    void DATE :: getdate(){

    int f=0;cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    44/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 44 - NARENDRA KUMAR S AND MANOHAR V NELLI

    flag=0;if(month

  • 8/13/2019 DS With C C++ Lab Manual VTU

    45/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 45 - NARENDRA KUMAR S AND MANOHAR V NELLI

    {cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    46/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 46 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 13: Design, develop, and execute a program in C++ to create a class calledOCTAL, which has the characteristics of an octal number. Implement the following operations bywriting an appropriate constructor and an overloaded operator +.i. OCTAL h = x; where x is an integerii. int y = h + k ; where h is an OCTAL object and k is an integer.Display the OCTAL result by overloading the operator

  • 8/13/2019 DS With C C++ Lab Manual VTU

    47/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 47 - NARENDRA KUMAR S AND MANOHAR V NELLI

    int sum=0,i=0,r;while(x!=0){r=x%10;sum=sum+(r*pow(8,i));i++;x=x/10;

    }return sum;

    }

    void main(){

    int x,k;clrscr();coutx;OCTAL h=x;

    cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    48/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 48 - NARENDRA KUMAR S AND MANOHAR V NELLI

    /* ASSIGNMENT 14: Design, develop, and execute a program in C++ to create a class calledBIN_TREE that represents a Binary Tree, with member functions to perform inorder, preorder andpostorder traversals. Create a BIN_TREE object and demonstrate the traversals. */

    #include#include#include

    #include

    struct NODE{

    int data;struct NODE *lchild,*rchild;

    };

    class BIN_TREE{public: struct NODE *root;

    BIN_TREE(){root=NULL;

    }void insert(int);void inorder(struct NODE*);void preorder(struct NODE*);void postorder(struct NODE*);

    };

    void BIN_TREE::insert(int item)

    { int i;char direction[10];struct NODE *temp,*prev,*cur;temp=new (struct NODE);temp->data=item;temp->lchild=temp->rchild=NULL;if(root==NULL){root=temp;return;

    }coutdirection;prev=NULL;cur=root;for(i=0;i

  • 8/13/2019 DS With C C++ Lab Manual VTU

    49/50

    DS WITH C/C++ LAB

    _______________________________________________________________________________Dept. of CSE - 49 - NARENDRA KUMAR S AND MANOHAR V NELLI

    cur=cur->lchild;else if (direction[i]=='R' || direction[i]=='r')cur=cur->rchild;

    else{cout

  • 8/13/2019 DS With C C++ Lab Manual VTU

    50/50

    DS WITH C/C++ LAB

    void main(){

    int ch,ele;BIN_TREE bt;clrscr();while(1)

    {cout


Recommended