data structures lab manual.pdf

Upload: ayjagadish

Post on 14-Apr-2018

282 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/27/2019 Data Structures Lab Manual.pdf

    1/36

    Shri Pillappa College of Engineering(Approved by AICTE Ne w Delhi, Affiliated to VTU Govt. of Karnataka)

    #79, Kondasettihalli Road, Hesaraghatta Hobli, Bangalore North Taluk, Bangalore-89

    Department of Computer Science & Engineering

    III Semester

    Data Structures Lab Manual

    (10CSL37)

    Prepared By

    Jagadeesh A YAsst. Professor

    Dept. of CSESPCE

  • 7/27/2019 Data Structures Lab Manual.pdf

    2/36

    Jagadeesh A Y, Dept. of CSE SPCE 2

    DATA STRUCTURES WITH C/C++ LABORATORY

    (Common to CSE & ISE)

    Subject Code: 10CSL37 I.A. Marks : 25

    Hours / Week : 03 Exam Hours: 03

    Total Hours : 42 Exam Marks: 50

    1. Using circular representation for a polynomial, design, develop, and execute a program inC to accept two polynomials, add them, and then print the resulting polynomial.

    2. Design, develop, and execute a program in C to convert a given valid parenthesized infixarithmetic expression to postfix expression and then to print both the expressions. The

    expression consists of single character operands and the binary operators + (plus), -

    (minus), * (multiply) and / (divide).

    3. Design, develop, and execute a program in C to evaluate a valid postfix expression usingstack. Assume that the postfix expression is read as a single line consisting of non-

    negative single digit operands and binary arithmetic operators. The arithmetic operators

    are + (add), - (subtract), * (multiply) and / (divide).

    4. Design, develop, and execute a program in C to simulate the working of a queue of

    integers using an array. Provide the following operations:

    a. Insert b. Delete c. Display

    5. Design, develop, and execute a program in C++based on the following requirements:

    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 gross salary (=

    basic_Salary _ All_Allowance); Net_Salary = Basic_Salary + All_AllowancesIT)

  • 7/27/2019 Data Structures Lab Manual.pdf

    3/36

    Jagadeesh A Y, Dept. of CSE SPCE 3

    6. Design, develop, and execute a program in C++ to create a class called STRING and

    implement the following operations. Display the results after every operation by overloading

    the operator

  • 7/27/2019 Data Structures Lab Manual.pdf

    4/36

    Jagadeesh A Y, Dept. of CSE SPCE 4

    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)

    12. Design, develop, and execute a program in C++ to create a class called DATE with

    methods to accept two valid dates in the form dd/mm/yy and to implement the following

    operations by overloading the operators + and -. After every operation the results are to be

    displayed by overloading the operator

  • 7/27/2019 Data Structures Lab Manual.pdf

    5/36

    Jagadeesh A Y, Dept. of CSE SPCE 5

    Program 1

    Using circular representation for a polynomial, design,

    develop, and execute a program in C to accept two polynomials,

    add them, and then print the resulting polynomial.

    #include#include

    struct node{

    int coeff;int exponent;struct node *link;

    };

    typedef struct node * NODE;

    NODE attach(NODE head, int coeff, int exp){

    NODE temp, pres;temp = (struct node *)malloc(sizeof(struct node));

    temp->coeff = coeff;temp->exponent = exp;

    pres = head->link;

    while(pres->link != head){

    pres = pres->link;}

    pres->link = temp;temp->link = head;

    return head;}

    NODE read_polynomial(NODE head){

    int i = 1;int coeff;int exp;

    printf("Enter coefficient as -999 to end the polynomial\n");while(1){

    printf("Enter %d term\n", i++);

    printf("Coefficient = ");scanf("%d", &coeff);

  • 7/27/2019 Data Structures Lab Manual.pdf

    6/36

    Jagadeesh A Y, Dept. of CSE SPCE 6

    if ( coeff == -999)break;

    printf("\npow x =");

    scanf("%d", &exp);head = attach(head, coeff, exp);

    }return head;

    }

    NODE polynomial_add(NODE head1, NODE head2, NODE head3){

    NODE a, b;int coeff;

    a = head1->link;b = head2->link;while( a != head1 && b != head2){

    if( a->exponent == b->exponent){

    coeff = a->coeff + b->coeff;if(coeff != 0){

    head3 = attach(head3, coeff, a->exponent);a = a->link;

    b = b->link;}

    }else if( a->exponent > b->exponent){

    head3 = attach(head3, a->coeff, a->exponent);a = a->link;

    }else{

    head3 = attach(head3, b->coeff, b->exponent);

    b = b->link;}

    }while( a != head1 ){

    head3 = attach(head3, a->coeff, a->exponent);a = a->link;

    }

    while( b != head2 ){

    head3 = attach(head3, b->coeff, b->exponent);b = b->link;

  • 7/27/2019 Data Structures Lab Manual.pdf

    7/36

    Jagadeesh A Y, Dept. of CSE SPCE 7

    }

    return head3;}

    void display(NODE head){

    NODE temp;if(head->link == head){

    printf("Polynomial does not exist\n");return;

    }

    temp = head->link;

    while(temp != head){

    printf("+%2dx^%d", temp->coeff, temp->exponent);temp = temp->link;

    }return;

    }

    void main(){

    NODE head1, head2, head3;

    head1 = (struct node * )malloc(sizeof(struct node));head2 = (struct node * )malloc(sizeof(struct node));head3 = (struct node * )malloc(sizeof(struct node));

    head1->link = head1;head2->link = head2;head3->link = head3;

    printf("\nEnter Polynomial 1:");

    head1 = read_polynomial(head1);

    printf("\nEnter Polynomial 2:");head2 = read_polynomial(head2);

    head3 = polynomial_add(head1, head2, head3);

    printf("\nPolynomial 1 is: ");display(head1);

    printf("\nPolynomial 2 is: ");

    display(head2);

  • 7/27/2019 Data Structures Lab Manual.pdf

    8/36

    Jagadeesh A Y, Dept. of CSE SPCE 8

    printf("\nPolynomial 3 is: ");display(head3);

    }

  • 7/27/2019 Data Structures Lab Manual.pdf

    9/36

    Jagadeesh A Y, Dept. of CSE SPCE 9

    Program 2

    Write a C program to convert & print a valid parenthesizedinfix expression to postfix. The expression should consist ofsingle character operands and binary operators.

    #include#include#include

    int f(char symbol){

    switch(symbol){

    case '+' : return 2;case '-' : return 2;

    case '*' : return 4;case '/' : return 4;case '^' : return 5;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;}

    }

    int infixtoposfix(char infix[],char postfix[]){

    int top, i, j;char s[30], symbol;top = -1;s[++top] = '#';j=0;

    for( i = 0; i

  • 7/27/2019 Data Structures Lab Manual.pdf

    10/36

    Jagadeesh A Y, Dept. of CSE SPCE 10

    symbol = infix[i];while(f(s[top]) > g(symbol)){

    postfix[j] = s[top--];j++;

    }

    if(f(s[top] != g(symbol)))s[++top] = symbol;

    elsetop--;

    }

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

    postfix[j++]=s[top--];

    }postfix[j]='\0';return 0;

    }

    void main(){

    char infix[30],postfix[30];

    clrscr();

    printf("enter valid expression\n");

    scanf("%s", infix);

    infixtoposfix(infix, postfix);

    printf("the infix expression is %s\n", infix);

    printf("the postfix expression is %s\n", postfix);

    getch();

    }

  • 7/27/2019 Data Structures Lab Manual.pdf

    11/36

    Jagadeesh A Y, Dept. of CSE SPCE 11

    Program 3

    Design, develop, and execute a program in C to evaluate a

    valid postfix expression using stack. Assume that the postfix

    expression is read as a single line consisting of non-negative

    single digit operands and binary arithmetic operators. The

    arithmetic operators are + (add), - (subtract), * (multiply)

    and / (divide).

    #include#include

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

    switch(symbol){

    case '+': return (op1+op2);case '-': return (op1-op2);case '*': return (op1*op2);case '/': return (op1/op2);case '$':case '^': return pow(op1,op2);

    }

    }

    void main(){

    double s[20];double res;double op1;double op2;int top, i;char postfix[20];char symbol;

    printf("Enter the postfix expression\n");scanf("%s", postfix);

    top = -1;

    for(i = 0; i< strlen(postfix); i++){

    symbol = postfix[i];

    if( isdigit(symbol) )

    s[++top] = symbol - '0';

  • 7/27/2019 Data Structures Lab Manual.pdf

    12/36

    Jagadeesh A Y, Dept. of CSE SPCE 12

    else{

    op2 = s[top--];op1 = s[top--];

    res = compute(symbol, op1, op2);

    s[++top] = res;}

    }res = s[top--];

    printf("The result is %f\n", res);}

  • 7/27/2019 Data Structures Lab Manual.pdf

    13/36

    Jagadeesh A Y, Dept. of CSE SPCE 13

    Program 4

    Design, develop, and execute a program in C to simulate the

    working of a queue of integers using an array. Provide the

    following operations:

    a. Insert b. Delete c. Display

    #include#define Q_SIZE 20

    int choice, element, front, rear, q[Q_SIZE];

    void q_insert(){

    if(rear == Q_SIZE-1){

    printf("\tQUEUE OVERFLOW\n");return;

    }rear = rear+1;q[rear] = element;return;

    }

    int q_delete()

    { if( front>rear)return -1;

    return q[front++];}

    void display(){

    int i;

    if( front> rear ){printf("\t QUEUE IS EMPTY\n");return;

    }

    printf("Contents of the Queue are\n");for(i = front; i

  • 7/27/2019 Data Structures Lab Manual.pdf

    14/36

    Jagadeesh A Y, Dept. of CSE SPCE 14

    void main(){

    front = 0;rear = -1;

    for( ; ; ){

    printf("1. Insert\n");printf("2. Delete\n");printf("3. Display\n");printf("4. Exit\n");scanf("%d", &choice);switch(choice){

    case 1: printf("Enter the element to insert intoQueue\n");

    scanf("%d", &element);

    q_insert();break;

    case 2: element = q_delete();if(element == -1)

    printf("\tQUEUE IS EMPTY\n");else

    printf("Item deleted = %d\n", element);break;

    case 3: display();break;

    default: exit(0);}

    }}

  • 7/27/2019 Data Structures Lab Manual.pdf

    15/36

    Jagadeesh A Y, Dept. of CSE SPCE 15

    Program 5

    Design, develop, and execute a program in C++ based on the

    following requirements:

    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

    gross salary (= basic_Salary _ All_Allowance); Net_Salary =

    Basic_Salary + All_Allowances IT)

    #include#include

    class EMP{

    char emp_name[20];int emp_number;int basic_salary;int gross_salary;int all_allowances;int IT;int net_salary;

    public: void read_data();

    void calculate_net_salary();void display();

    };

    void EMP :: read_data(){

    coutemp_name;coutemp_number;coutbasic_salary;}

  • 7/27/2019 Data Structures Lab Manual.pdf

    16/36

    Jagadeesh A Y, Dept. of CSE SPCE 16

    void EMP :: calculate_net_salary(){

    all_allowances = 1.23 * basic_salary;gross_salary = basic_salary + all_allowances;IT = 0.3 * gross_salary;

    net_salary = basic_salary + all_allowances - IT;}

    void EMP :: display(){

    cout

  • 7/27/2019 Data Structures Lab Manual.pdf

    17/36

    Jagadeesh A Y, Dept. of CSE SPCE 17

    Program 6

    Design, develop, and execute a program in C++ to create a

    class called STRING and implement the following operations.

    Display the results after every operation by overloading the

    operator

  • 7/27/2019 Data Structures Lab Manual.pdf

    18/36

    Jagadeesh A Y, Dept. of CSE SPCE 18

    }

    void main(){

    clrscr();

    string s1("VTU");string s2("BELGAUM");string s3=s1+s2;cout

  • 7/27/2019 Data Structures Lab Manual.pdf

    19/36

    Jagadeesh A Y, Dept. of CSE SPCE 19

    Program 7

    Design, develop, and execute a program in C++ to create aclass called STACK using an array of integers and to implementthe following operations by overloading the operators + and -:

    i. s1=s1 + element; where s1 is an object of the class STACKand element is an integer to be pushed on to top of thestack.

    ii. s1=s1- ; where s1 is an object of the class STACK and operator pops off the top element.Handle the STACK Empty and STACK Full conditions. Also displaythe contents of the stack after each operation, by overloadingthe operator

  • 7/27/2019 Data Structures Lab Manual.pdf

    20/36

    Jagadeesh A Y, Dept. of CSE SPCE 20

    int empty(stack s1){

    if(s1.top == -1)return 1;

    else

    return 0;}

    int overflow(stack s1){

    if(s1.top == s1.size-1)return 1;

    elsereturn 0;

    }

    ostream& operator

  • 7/27/2019 Data Structures Lab Manual.pdf

    21/36

    Jagadeesh A Y, Dept. of CSE SPCE 21

    switch(ch){

    case 1: clrscr();if(overflow(s1))

    cout

  • 7/27/2019 Data Structures Lab Manual.pdf

    22/36

    Jagadeesh A Y, Dept. of CSE SPCE 22

    Program 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 to

    delete an element from the front of the list. Demonstrate all

    the functions after creating a list object.

    #include#include#includestruct node{

    int info;node *link;

    };

    class LIST{

    node *first;public:

    list(){

    first = NULL;}

    void insert_node(int);void delete_node();void display();

    };

    void LIST::insert_node(int item){

    node *temp=new node;temp->info=item;temp->link=first;first=temp;

    }

    void LIST::delete_node(){

    if(first==NULL){

    cout

  • 7/27/2019 Data Structures Lab Manual.pdf

    23/36

    Jagadeesh A Y, Dept. of CSE SPCE 23

    void LIST ::display(){

    if(first == NULL){

    cout

  • 7/27/2019 Data Structures Lab Manual.pdf

    24/36

    Jagadeesh A Y, Dept. of CSE SPCE 24

    Program 9

    Design, develop, and execute a program in C to read a sparse

    matrix of integer values and to search the sparse matrix for

    an element specified by the user. Print the result of the

    search appropriately. Use the triple to

    represent an element in the sparse matrix.

    #include

    struct MATRIX{

    int row;int col;int val;

    };struct MATRIX a[100];

    read_sparse_matrix(){

    int m;int n;int k=1;int element;int i, j;printf("Enter the number of ROWS, COLUMNS of Sparse

    Matrix\n");scanf("%d%d", &m, &n);

    a[0].row = m;a[0].col = n;

    printf("Enter elements of Sparse Matrix\n");for( i = 0; i

  • 7/27/2019 Data Structures Lab Manual.pdf

    25/36

    Jagadeesh A Y, Dept. of CSE SPCE 25

    a[0].val = k-1;return;

    }

    search()

    {int element, i;

    printf("Enter the element to search\n");scanf("%d", &element);

    for( i = 1; i

  • 7/27/2019 Data Structures Lab Manual.pdf

    26/36

    Jagadeesh A Y, Dept. of CSE SPCE 26

    10. Design, develop, and execute a program in C to create a

    max heap of integers by accepting one element at a time and by

    inserting it immediately in to the heap. Use the array

    representation for the heap. Display the array at the end of

    insertion phase.

    #include#include#include#define MAX_SIZE 10

    int insert_heap(int item, int a[], int n){

    int c, p;if( n == MAX_SIZE){

    printf("Heap is full\n");return n;

    }

    c = n;p = c/2;

    while( c != 1 && item > a[p]){

    a[c] = a[p];c = p;p = c/2;

    }

    a[c] = item;return (n+1);

    }

    void display(int a[], int n)

    { int i;if( n == 0 )

    {printf("Heap is empty\n");return;

    }printf("The priority queue contents are\n");for( i = 1; i

  • 7/27/2019 Data Structures Lab Manual.pdf

    27/36

    Jagadeesh A Y, Dept. of CSE SPCE 27

    void main(){

    int a[MAX_SIZE], n=1, choice, item;clrscr();

    for(;;){

    printf("\n1. Insert");printf("\n2. Display");printf("\n3. Quit");

    printf("\nEnter U R choice:");scanf("%d", &choice);switch(choice){

    case 1: printf("\nEnter item to be inserted:");

    scanf("%d", &item);n = insert_heap(item, a, n);break;

    case 2: display(a, n);break;

    default: exit(0);}

    }}

  • 7/27/2019 Data Structures Lab Manual.pdf

    28/36

    Jagadeesh A Y, Dept. of CSE SPCE 28

    Program 11

    Design, develop, and execute a program in C to implement a

    doubly linked list 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 key value

    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#includestruct node{

    int info;

    struct node *llink;struct node *rlink;};typedef struct node* NODE;

    NODE getnode(void){

    NODE x;x =(NODE)malloc(sizeof(struct node));if(x == NULL){

    printf("out of memory\n");exit(0);}return x;

    }

    void free_node(NODE x){

    free(x);}

  • 7/27/2019 Data Structures Lab Manual.pdf

    29/36

    Jagadeesh A Y, Dept. of CSE SPCE 29

    NODE insert_front (int item, NODE head){

    NODE temp, cur;temp = getnode();temp->info =item;

    cur = head->rlink;head->rlink = temp;temp->llink =head;temp->rlink =cur;cur->llink = temp;return head;

    }

    NODE insert_left(int item,NODE head){

    NODE temp,cur,prev;

    if(head->rlink==head){

    printf("list is empty\n");return head;

    }cur=head->rlink;while(cur!=head&&item!=cur->info){

    cur=cur->rlink;}if(cur==head)

    {printf("key not found\n");return head;

    }prev=cur->llink;printf("\n enter the item to be inserted to the left of

    %d",item);temp=getnode();scanf("%d",&temp->info);prev->rlink=temp;temp->llink=prev;

    cur->llink=temp;temp->rlink=cur;return head;

    }

    NODE delete_item(int item,NODE head){

    NODE cur ,prev, next;if(head->rlink==head){

    printf("list is empty cannot delete\n");

    return head;}

  • 7/27/2019 Data Structures Lab Manual.pdf

    30/36

    Jagadeesh A Y, Dept. of CSE SPCE 30

    cur=head->rlink;while(cur!=head&&item!=cur->info){

    cur=cur->rlink;}

    if(cur==head){

    printf("\n item not found\n");return head;

    }prev=cur->llink;next=cur->rlink;prev->rlink=next;next->llink=prev;free_node(cur);return head;

    }

    void display(NODE head){

    NODE temp;if(head->rlink == head){

    printf("List is empty\n");return;

    }printf("contents of the list is\n");

    for(temp = head->rlink; temp != head; temp=temp->rlink){

    printf("%d\n", temp->info);}printf("\n");

    }

    void main(){

    NODE head;int choice, item;

    head=getnode();head->rlink=head;clrscr();for(;;){

    printf("1:insert front\n2:insert left\n3:deletekey\n4:display\n5:exit");

    printf("\nenter your choice");scanf("%d",&choice);switch(choice){

    case 1: printf("enter the item to beinserted\n");

  • 7/27/2019 Data Structures Lab Manual.pdf

    31/36

    Jagadeesh A Y, Dept. of CSE SPCE 31

    scanf("%d",&item);head=insert_front(item,head);break;

    case 2: printf("enter the key value of the

    node\n");scanf("%d",&item);head=insert_left(item,head);break;

    case 3: printf("enter the item to be deleted");scanf("%d",&item);head=delete_item(item,head);break;

    case 4: display(head);

    break;default: exit(0);

    }}getch();

    }

  • 7/27/2019 Data Structures Lab Manual.pdf

    32/36

    Jagadeesh A Y, Dept. of CSE SPCE 32

    Program 13

    Design, develop, and execute a program in C++ to create a

    class called OCTAL, which has the characteristics of an octal

    number.

    Implement the following operations by writing an appropriate

    constructor and an overloaded operator +.

    i. OCTAL h = x ; where x is an integer

    ii. int y = h + k ; where h is an OCTAL object and k is an

    integer.

    Display the OCTAL result by overloading the operator

  • 7/27/2019 Data Structures Lab Manual.pdf

    33/36

    Jagadeesh A Y, Dept. of CSE SPCE 33

    int octal::operator +(int x){

    x = conv_octal(x);return (octnum + x);

    }

    ostream & operator

  • 7/27/2019 Data Structures Lab Manual.pdf

    34/36

    Jagadeesh A Y, Dept. of CSE SPCE 34

    Program 14

    Design, develop, and execute a program in C++ to create a

    class called BIN_TREE that represents a Binary Tree, with

    member functions to perform inorder, preorder and postorder

    traversals. Create a BIN_TREE object and demonstrate the

    traversals.

    #include#include#includestruct node{

    int num;

    struct node *left, *right;};

    class BIN_TREE{

    node *root;public:

    BIN_TREE(){

    root = NULL;}

    void create();void inorder(node *);void preorder(node *);void postorder(node *);void display();

    };

    void BIN_TREE :: create(){

    node *prev, *pres, *temp;

    temp = new node;temp->left = temp->right = NULL;couttemp->num;

    if( root == NULL ){

    root=temp;return;

    }

    pres = root;prev = NULL;

  • 7/27/2019 Data Structures Lab Manual.pdf

    35/36

    Jagadeesh A Y, Dept. of CSE SPCE 35

    while( pres != NULL ){

    prev = pres;

    if( temp->num > pres->num )pres = pres->right;

    elsepres = pres->left;

    }

    if( temp->num > prev->num )prev->right = temp;

    elseprev->left = temp;

    }

    void BIN_TREE :: inorder( node *r ){

    if( r != NULL ){

    inorder( r->left );cout

  • 7/27/2019 Data Structures Lab Manual.pdf

    36/36

    void BIN_TREE :: display(){

    if( root == NULL ){

    cout