All Lab Prog

Embed Size (px)

Citation preview

  • 8/8/2019 All Lab Prog

    1/36

    Data Structures Lab - 06CSEL37

    1. Write a C program to create a sequential file with at least five records, each record having thestructure shown below:

    USN Name Marks1 Marks2 Marks3

    Non-zero

    Positiveinteger

    25 Characters Positive

    integer

    Positive

    integer

    Positive

    integer

    Write necessary functions1) To display all the records in the file.2) To search for a specific record based on the USN. In case the

    required record is not found, suitable message should be displayed. Both the options in thiscase must be demonstrated.

    #include#include

    #include

    typedef struct{

    int usn;char name[25];int m1,m2,m3;

    }stu;

    void display(char filename[]){

    FILE *fp;stu s;fp=fopen(filename,"r");puts("\nusn\tname: \tm1\tm2\tm3\n");if(fp!=NULL)

    while(fscanf(fp,"%5d%25s%3d%3d%3d",&s.usn,s.name,&s.m1,&s.m2,&s.m3)!=EOF)printf("\n%-5d\t%-25s\t%-3d\t%-3d\t%-3d",s.usn,s.name,s.m1,s.m2,s.m3);

    }

    void search(char filename[]){

    FILE *fp;stu s;int id,flag=0;

    printf("\n Enter the id :");scanf("%d",&id);

    fp=fopen(filename,"r");

    1

  • 8/8/2019 All Lab Prog

    2/36

    Data Structures Lab - 06CSEL37

    if(fp!=NULL)while(fscanf(fp,"%5d%25s%3d%3d%3d",&s.usn,s.name,&s.m1,&s.m2,&s.m3)!=EOF)if(id==s.usn){ puts("\nusn\tname: \tm1,\tm2,\tm3");

    printf("%-5d\t%-25s\t%-3d%-3d\t%-3d\n",s.usn,s.name,s.m1,s.m2,s.m3);

    flag=1;}if(flag==0)

    {printf("\n No Records");getch();}

    }

    void main(){

    int n,i,ch;char filename[23];

    FILE *fp;stu s;

    clrscr();printf("\n Enter filename :");scanf("%s",filename);

    fp=fopen(filename,"w");

    puts("\n Enter n :");scanf("%d",&n);

    if(fp!=NULL){

    puts("\n Enter USN NAME MARKS1 MARKS2 MARKS3 \n");for(i=0;i

  • 8/8/2019 All Lab Prog

    3/36

    Data Structures Lab - 06CSEL37

    while(1){

    printf("\n 1.Display 2.Search\n");printf("\n Enter your choice :");

    scanf("%d",&ch);

    switch(ch){

    case 1:display(filename);break;

    case 2:search(filename);break;

    default:printf("\n Error in input\n");exit(0);

    } // end Switch

    } // end of while}/**************************************************************************/

    Write and demonstrate the following C functions:NewStrCpy that does the same job as strcpy.NewStrCat that does the same job as strcat without using any library

    Functions#includevoid newstrcpy(char dst [],char src[]){ int i ;

    for(i=0 ; src[i] !='\0'; i++ )dst[i] = src[i] ;

    dst[i] ='\0';}

    void newstrcat( char dst[] ,char src[] ){ int i,j ;

    for( i = 0; dst[i] !='\0' ;i++ ){ }for( j=0 ; src[j] !='\0' ; j++, i++ )

    dst[i] = src[j];dst[i] = '\0';

    }void main(){

    char s1[10], s2[10], s3[10] ;int ch;clrscr();

    3

  • 8/8/2019 All Lab Prog

    4/36

    Data Structures Lab - 06CSEL37

    while(1){

    printf("\n\n 1: string copy 2:string concat \n");scanf("%d",&ch);switch(ch)

    { case 1: printf("\n Enter string to be copied \n");scanf("%s",s1);newstrcpy(s2,s1);printf("\n The original string is %s",s1);printf("\n The copied string is %s",s2);break;

    case 2: printf("\n Enter first string \n");scanf("%s",s1);printf("\n Enter second string \n");

    scanf("%s",s2);newstrcat(s2,s1);printf("\n\n The concatenated string is %s",s2);break;

    default:exit(0);}

    }}/**************************************************************************/

    3. Write a C program, which accepts the internet protocol (IP) address in decimal dot format(ex. 1153.18.8.105) and converts it into 32-bit long integer ( ex. 2568095849) using strtoklibrary functions and unions.

    #include#include

    typedef union{

    unsigned char chaddr[4];unsigned long numaddr;

    }IP ;

    void main ( ){

    IP addr ;char * parser ;char straddr [ 16 ] ;int i ;

    4

  • 8/8/2019 All Lab Prog

    5/36

    Data Structures Lab - 06CSEL37

    clrscr() ;

    printf ("Enter the IP address\n") ;fflush(stdin);gets (straddr);

    parser = strtok ( straddr , "." ) ;addr.chaddr[3]=strtol ( parser, ( char** ) NULL ,10) ;

    for (i = 2 ;i >= 0 ; i--){

    parser = strtok (NULL ,"." ) ;addr.chaddr[ i ]= strtol ( parser ,( char** ) NULL , 10);

    }

    printf( "IP decimal dot : %d.%d.%d.%d\n",

    addr.chaddr[3] ,addr.chaddr[2] ,addr.chaddr[1] ,addr.chaddr[0]);

    printf( "IP long integer : %lu\n", addr.numaddr );

    getch();}

    /**************************************************************************/4. Write a C program to construct a stack of integers and to perform the following operations

    on it.Pusha) PopDisplay

    The program should print appropriate messages for stack overflow, stack underflow and stackempty.

    #define SIZE 5typedef struct {

    int st [SIZE] ;int top ;

    } stack ;

    void push(stack *s , int ele){

    if ( s->top == SIZE-1 ){

    puts( "Stack over flow " ) ;getch();

    }

    5

  • 8/8/2019 All Lab Prog

    6/36

    Data Structures Lab - 06CSEL37

    else{

    s -> top++;s -> st[ s->top ] = ele ;

    }

    }int pop(stack *s){

    int ele ;if ( s->top == -1 )

    return 0;else

    ele = s-> st [ s->top ] ;s ->top--;

    return ele ;}

    void display ( stack s ){

    int i ;if( s.top== -1 )

    puts ( "STACK EMPTY" ) ;else {

    puts ( "STACK ele are" ) ;for ( i=0; i

  • 8/8/2019 All Lab Prog

    7/36

    Data Structures Lab - 06CSEL37

    switch(ch){

    case 1: printf ( " enter ele to be pushed\n" ) ;scanf( "%d", &ele ) ;push ( &s, ele ) ;

    break ;case 2:ele=pop ( &s ) ;printf ( "poped ele is %d\n", ele ) ;getch ( ) ;break ;

    default:exit ( 0 ) ;}

    }}

    /**************************************************************************/5. Write a C program to convert and print a given valid parenthesized infix arithmeticexpression to postfix expression. The expression consists of single character operands andthe binary operators + (plus), - (minus), * (multiply) and / (divide).

    #include

    int inp_prec(char sym){

    switch(sym){

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

    }}int stk_prec(char sym){

    switch(sym){

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

    7

  • 8/8/2019 All Lab Prog

    8/36

    Data Structures Lab - 06CSEL37

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

    }}

    void main(){char in[40],pos[40];int i,j,st[40],top=-1,dont_use;char sym;

    clrscr();

    st[++top]='#';

    puts("enter infix :");gets(in);

    i=j=0;while(in[i]!='\0'){

    sym=in[i];

    while(inp_prec(sym) < stk_prec(st[top]))pos[j++]=st[top--];

    if(inp_prec(sym) != stk_prec(st[top]))st[++top]=sym;

    elsedont_use=st[top--];

    i++;}while(st[top]!='#'){

    pos[j++]=st[top--];}pos[j]='\0';

    puts("infix :");puts(in);puts("postfix :");puts(pos);

    getch();}

    8

  • 8/8/2019 All Lab Prog

    9/36

    Data Structures Lab - 06CSEL37

    /**************************************************************************/6. Write a C program to evaluate a valid suffix/postfix expression-using stack. Assume that the

    suffix / postfix expression is read as a single line consisting of non-negative single digitoperands and binary arithmetic operators. The arithmetic operators are + (ADD), -(SUBTRACT), * (MULTIPLY) and / (DIVIDE).

    #include#define SIZE 20

    typedef struct{int st [ SIZE ] ;int top ;

    } stack ;

    void push ( stack *s , int ele){

    if ( s->top == SIZE ) {printf( "\n\nStack over flow / stack is full " ) ;getch ( ) ;

    }else

    s->top++ ;s->st[ s->top ] = ele ;

    }

    int pop ( stack *s ){

    if ( s->top == -1 )return 0 ;

    elsereturn s->st [ s->top-- ] ;

    }

    int eval(char sym, int op1 ,int op2 ){

    switch( sym ){

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

    }}

    9

  • 8/8/2019 All Lab Prog

    10/36

    Data Structures Lab - 06CSEL37

    int is_digit ( char c ){

    if( c>='0' && c

  • 8/8/2019 All Lab Prog

    11/36

    Data Structures Lab - 06CSEL37

    7. Write a C program to simulate the working of a queue of integers using an array. Provide thefollowing operations.

    a) Insertb) Deletec) Display

    #define size 5

    typedef struct q{int qu[size];int f,r;}QUE;

    int isfull(QUE q){

    if(q.r==size-1)return 1;else

    return 0;}

    int isempty(QUE q){if(q.r==-1)

    return 1;else

    return 0;}void insertq(QUE *q,int e){

    if(isfull(*q)){

    puts("que full");return;

    }else

    q->qu[++q->r]=e;}int deleteq(QUE *q){

    int e=-1;

    if(isempty(*q))puts("Que empty");

    else

    11

  • 8/8/2019 All Lab Prog

    12/36

    Data Structures Lab - 06CSEL37

    {e=q->qu[q->f++];if(q->f>q->r){ q->f=0;

    q->r=-1;

    }}return e;

    }void disp(QUE q){

    int i;if(isempty(q))

    puts("\n Empty QUEUE");else

    {

    printf("\n The elements of Que are :\n");for(i=q.f ; i

  • 8/8/2019 All Lab Prog

    13/36

    Data Structures Lab - 06CSEL37

    if(ele != -1)printf("Deleted element is %d",ele);

    disp(q);break;

    default: exit(0);

    }}}/**************************************************************************/

    8. Write a C program to simulate the working of a circular queue of integers using an array.Provide the following operations.

    ) Insert) Delete) Display

    #define max 5

    typedef struct {int data [ max ] ;int front ;int rare ;

    } cqueue ;

    void insertcq ( cqueue *cq ,int ele ){int tmp = cq ->rare ;if( cq ->rare == max-1 )

    cq ->rare = 0;else

    cq ->rare++;if( cq -> rare != cq ->front )

    cq -> data[cq -> rare ] = ele ;else {

    printf("overflow\n");cq->rare=tmp;getch( );

    }}int deletecq ( cqueue *cq ){

    int ele=-1;if ( cq->rare == max-1 && cq -> front == max -1 ) {

    printf ( "queue underflow\n" ) ;return -1;

    }

    13

  • 8/8/2019 All Lab Prog

    14/36

    Data Structures Lab - 06CSEL37

    if(cq -> front == max -1 )cq->front = 0 ;

    elsecq -> front ++ ;

    ele = cq -> data [ cq -> front ];if ( cq -> front == cq -> rare )cq -> front = cq -> rare = max -1 ;

    return ele;}

    void displaycq(cqueue cq){

    int i;if(cq.rare==max-1 && cq.front==max-1) {

    printf("\n CIRCULAR QUEUE is empty\n\n");

    return;}

    else {if( cq.front == max-1 )

    i = 0;else

    i = cq.front + 1 ;}while(1){

    printf ( "%d => ", cq.data[i] ) ;if( i == cq.rare)break;if( i == max-1 )i = 0;elsei++;

    }printf("\n\n");

    }

    void main( ){

    cqueue cq ;int ch , ele ;clrscr ( ) ;cq.rare = max - 1 ;cq.front = max -1;

    14

  • 8/8/2019 All Lab Prog

    15/36

    Data Structures Lab - 06CSEL37

    while ( 1 ){

    clrscr( ) ;displaycq( cq ) ;printf ("\n enter 1:insert 2:delete \n" ) ;

    scanf( "%d" ,&ch ) ;switch( ch ){

    case 1 : printf ( "enter the ele to be inserted\n" ) ;scanf ( "%d", &ele ) ;insertcq ( &cq , ele ) ;break ;

    case 2: ele = deletecq ( &cq ) ;if ( ele!=-1 )

    printf ( "deleted ele is %d\n" , ele ) ;

    getch( ) ;break;

    default:printf ( "error in input\n" ) ;getch ( ) ;exit ( 0 ) ;

    } //enh of switch()} //end of while()

    } //end of main()

    /**************************************************************************/9. Write a C program using dynamic variables and pointers, to construct a singly linked list

    consisting of the following information in each node: student id (integer), student name(character string) and semester (integer). The operations to be supported are:

    The insertion operation.i) At the front of a list.ii) At the back of the list.iii) At any position in the list.

    Deleting a node based on student id. If the specified node is not present in the list an errormessage should be displayed. Both the options should be demonstrated.

    Searching a node based on student id and update the information content. If the specifiednode is not present in the list an error message should be displayed. Both situationsshould be displayed.

    Displaying all the nodes in the list.

    15

  • 8/8/2019 All Lab Prog

    16/36

    Data Structures Lab - 06CSEL37

    #include

    typedef struct n{

    int rno,sem;char name[20];struct n *next;

    }*NODE;

    NODE getnode( ){

    NODE tmp;tmp=(NODE) malloc(sizeof(struct n));puts("\n enter rno,name,sem");scanf("%d%s%d",&tmp->rno,tmp->name,&tmp->sem);

    tmp->next=NULL;return tmp;}

    int count(NODE *head){

    NODE tmp; int c=0;tmp=*head;while(tmp != NULL){

    c = c++;tmp=tmp->next;

    }return c;

    }

    void insert_beg(NODE *head){

    NODE tmp=getnode();if(tmp==NULL)puts("\n Overflow ?????");

    if(head==NULL)*head=tmp;

    else{

    tmp->next=*head;*head=tmp;

    }}

    16

  • 8/8/2019 All Lab Prog

    17/36

    Data Structures Lab - 06CSEL37

    void insert_end(NODE *head){

    NODE tt,tmp=getnode();if(tmp==NULL)

    puts("\n Overflow ?????");if(head==NULL)*head=tmp;

    else{ tt=*head;

    while(tt->next != NULL)tt=tt->next;

    tt->next=tmp;}

    }

    void insert_pos(NODE *head){NODE tt,tmp;int c,pos,i;c=count(head);puts("\n enter pos :");scanf("%d",&pos);if(pos==1){

    insert_beg(head);return;

    }if(pos == c+1){

    insert_end(head);return;

    }if(pos>c+1 || pos==0){

    puts("NO SUCH INSERTION POSSIBLE\n");getch();return;

    }tmp=getnode();tt=*head;i=1;while(i+1 < pos ){

    i++;tt=tt->next;

    17

  • 8/8/2019 All Lab Prog

    18/36

    Data Structures Lab - 06CSEL37

    }tmp->next=tt->next;tt->next=tmp;

    }

    void disp(NODE *head){NODE tmp;tmp=*head;printf("\n-------------------------------------------\n");while(tmp != NULL){

    printf("\n %d \t %s \t %d ",tmp->rno,tmp->name,tmp->sem);tmp=tmp->next;

    }printf("\n-------------------------------------------\n");

    }

    void deleteid(NODE *head){

    NODE tt,tmp;int id;if(*head==NULL){

    puts("\n No id present");return ;

    }puts("enter id");scanf("%d",&id);tmp=*head;if(tmp -> rno == id){

    *head=tmp->next;free(tmp);return;

    }while((tmp->next->rno != id) && (tmp->next !=NULL))tmp=tmp->next;

    if(tmp->next==NULL){

    puts("NODE DOESNT EXIST");return;

    }

    18

  • 8/8/2019 All Lab Prog

    19/36

    Data Structures Lab - 06CSEL37

    if(tmp->next->rno == id){

    tt=tmp->next;tmp->next=tt->next;free(tt);

    }}

    void searchid(NODE *head){

    NODE tt,tmp;int id;if(*head==NULL){

    puts("\n No id present");return ;

    }puts("enter id");scanf("%d",&id);tmp=*head;if(tmp->rno==id){

    puts("\n enter changed name and sem");scanf("%s%d",tmp->name,&tmp->sem);return;

    }

    while((tmp->next->rno != id) && (tmp->next !=NULL))tmp=tmp->next;

    if(tmp->next==NULL){

    puts("NODE DOESNT EXIST");return;

    }if(tmp->next->rno == id){

    tt=tmp->next;puts("\n enter changed name and sem");scanf("%s%d",tmp->name,&tmp->sem);

    }}

    19

  • 8/8/2019 All Lab Prog

    20/36

    Data Structures Lab - 06CSEL37

    void main( ){NODE head;int ele, ch;head = NULL;

    clrscr( );while(1){

    clrscr( );disp(&head);puts ("\n\n 1:insertbeg 2:insert end 3:insert pos 4:delete ");scanf("%d",&ch);switch(ch){

    case 1: insert_beg(&head);break;

    case 2: insert_end(&head);break;case 3: insert_pos(&head);

    break;case 4: deleteid(&head);

    break;case 5: searchid(&head);

    break;

    default:exit(0);}

    }

    }

    /**************************************************************************/10. Write a C program using dynamic variables and pointers to construct a stack of integers

    using singly linked list and to perform the following operations.Pusha) PopDisplay

    The program should print appropriate messages for stack overflowand stack empty.

    #include

    # define size 5

    int c=0; //// global variable

    20

  • 8/8/2019 All Lab Prog

    21/36

    Data Structures Lab - 06CSEL37

    typedef struct n{int info;struct n *next;

    }*NODE;

    NODE getnode(int ele){

    NODE tmp;tmp=(NODE) malloc(sizeof(struct n));tmp->info=ele;tmp->next=NULL;return tmp;

    }void push(NODE *head,int ele)

    { NODE tmp=getnode(ele);if(c>=size){puts("\n Overflow ");return;}c++; /////// SEE HEREif(head==NULL)

    *head=tmp;else{

    tmp->next=*head;*head=tmp;

    }}void disp(NODE *head){

    NODE tmp;tmp=*head;printf("\n c=%d\n",c);printf("\n-------------------------------------------\n");while(tmp != NULL){

    printf("%d==>",tmp->info);tmp=tmp->next;

    }printf("\n-------------------------------------------\n");

    }

    21

  • 8/8/2019 All Lab Prog

    22/36

    Data Structures Lab - 06CSEL37

    int pop(NODE *head){

    NODE tmp;int ele;

    if(*head==NULL){

    puts("\n UNDERFLOW");return -1;

    }c--; /////// SEE HEREtmp=*head;ele=tmp->info;

    *head=tmp->next;free(tmp);

    return ele;}void main(){NODE head;int ele, ch;head = NULL;clrscr();while(1){

    puts ("\n\n 1:insert 2:delete ");scanf("%d",&ch);switch(ch){ case 1: puts("\n Enter ur element\n");

    scanf("%d",&ele);push(&head,ele);disp(&head);

    break;case 2: ele = pop(&head);

    if (ele != -1)printf("\n The poped element is %d",ele);

    disp(&head);break;

    default:exit(0);}

    } }

    22

  • 8/8/2019 All Lab Prog

    23/36

    Data Structures Lab - 06CSEL37

    /**************************************************************************/11. Write a C program using dynamic variables and pointers to construct a queue of integers

    using singly linked list and to perform the following operations.Insert

    a) Delete

    DisplayThe program should print appropriate messages for queue full and queue empty.

    #include

    #define size 5

    int c=0; //SEE HERE

    typedef struct n

    {

    int info;

    struct n *next;}*NODE;

    NODE getnode(int ele)

    {

    NODE tmp;

    tmp=(NODE) malloc(sizeof(struct n));

    tmp->info=ele;

    tmp->next=NULL;

    return tmp;

    }

    void insert(NODE *rear, NODE *front,int ele)

    {

    NODE tmp=getnode(ele);

    if(c>=size)

    {

    puts("\n Overflow ?????");

    return;

    }

    c++;

    23

  • 8/8/2019 All Lab Prog

    24/36

    Data Structures Lab - 06CSEL37

    if(*rear==NULL)

    *rear = *front=tmp;

    else

    {

    (*rear)->next=tmp;

    *rear=tmp;

    }

    }

    void disp(NODE *front)

    {

    NODE tmp;

    tmp=*front;

    printf("\n-------------------------------------------\n");

    while(tmp != NULL)

    {

    printf("%d==>",tmp->info);

    tmp=tmp->next;

    }

    printf("\n-------------------------------------------\n");}

    int remove(NODE *rear, NODE *front)

    { NODE tmp,tt;

    int ele;

    ele=(*front)->info;

    tmp=*front;

    if(*front==NULL)

    {

    puts("\n UNDERFLOW");

    return -1;

    }

    c--;

    24

  • 8/8/2019 All Lab Prog

    25/36

    Data Structures Lab - 06CSEL37

    if(*rear==*front)

    *rear=*front=NULL;

    else

    *front=(*front)->next;

    free(tmp);

    return ele;

    }

    void main()

    {

    NODE rear,front;

    int ele, ch;

    rear=front=NULL;

    clrscr();

    while(1)

    {

    puts ("\n\n 1:insert 2:delete ");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1: puts("\n Enter ur element\n");scanf("%d",&ele);

    insert(&rear,&front,ele);

    disp(&front);

    break;

    case 2: ele = remove(&rear,&front);

    if (ele != -1)

    printf("\n The poped element is %d",ele);

    disp(&front);

    break;

    default:exit(0);

    }

    }

    }

    25

  • 8/8/2019 All Lab Prog

    26/36

    Data Structures Lab - 06CSEL37

    /**************************************************************************/12. Write a C program to support the following operations on a doubly linked list where each

    node consists of integers.a) Create a doubly linked list by adding each node at the front.b) Insert a new node to the left of the node whose key value is read as an input.

    c) Delete the node of a given data, if it is found, otherwise display appropriate message.d) Display the contents of the list.

    #include#include#include#include#include#include

    typedef struct n

    {int info;struct n *next,*pre;

    }*NODE;

    NODE getnode(){NODE tmp;tmp=(NODE)malloc(sizeof(struct n));puts("enter the info\n");scanf("%d",&tmp->info);tmp->next=tmp->pre=NULL;return tmp;}

    void insertbeg(NODE *head){NODE tmp;tmp=getnode();

    if(tmp==NULL){printf("\noverflow.......!!!!!!\n");return;}if(*head==NULL){*head=tmp;return;}

    26

  • 8/8/2019 All Lab Prog

    27/36

    Data Structures Lab - 06CSEL37

    tmp->next=*head;(*head)->pre=tmp;*head=tmp;return;

    }

    void insertleft(NODE *head){NODE tmp,nn=*head;int id;puts("\nenter the id of node to whose left the new node needs to be inserted\n");scanf("%d",&id);if(*head==NULL)

    {printf("id %d wont exst\n",id);return;

    }if(*head==NULL || (*head)->info==id){

    insertbeg(head);return;

    }tmp=getnode();if(tmp==NULL){puts("overflow\n");return;}while(nn->info!=id && nn!=NULL)

    nn=nn->next;if(nn==NULL){printf("\nerror in pos no. %d\n",id);return;}if(nn->info==id){tmp->pre=nn->pre;(nn->pre)->next=tmp;tmp->next=nn;nn->pre=tmp;}}

    27

  • 8/8/2019 All Lab Prog

    28/36

    Data Structures Lab - 06CSEL37

    void deleteq(NODE *head){NODE nn=*head,tmp=*head;int id;if(*head==NULL)

    {printf("\nunderflow......!!!!!\n");return;}puts("\nenter the id\n");scanf("%d",&id);if(tmp->info==id)

    {printf("deleted item is %d\n",(*head)->info);if(tmp->next==NULL){

    *head=NULL;free(tmp);return ;

    }(tmp->next)->pre=NULL;*head=tmp->next;free(tmp);return;

    }tmp=tmp->next;

    while(tmp!=NULL){

    if(tmp->info==id){

    printf("deleted item is %d\n",tmp->info);nn->next=tmp->next;(tmp->next)->pre=nn;free(tmp);return;

    }tmp=tmp->next;nn=nn->next;

    }printf("\n%d id wont exists\n",id);return;

    }void disp(NODE *head){NODE tmp=*head;

    28

  • 8/8/2019 All Lab Prog

    29/36

    Data Structures Lab - 06CSEL37

    if(*head==NULL){puts("!!!!!no elements!!!!!\n");return;}

    puts("\n--------------------------------------------------------------\n");while(tmp!=NULL){

    printf("%d==>",tmp->info);tmp=tmp->next;}puts("\n--------------------------------------------------------------\n");return;}

    void main()

    {NODE head=NULL;int c;clrscr();while(1){

    puts("\nEenter the choice\n");puts("\n1:insert brgining\t2:delete\t3:insert to left\t4:display\n");scanf("%d",&c);switch(c){

    case 1 : insertbeg(&head);disp(&head);break;

    case 2 : deleteq(&head);disp(&head);break;

    case 3 : insertleft(&head);disp(&head);break;

    case 4 :disp(&head);break;

    default : exit(0);}

    }}/**************************************************************************/

    29

  • 8/8/2019 All Lab Prog

    30/36

    Data Structures Lab - 06CSEL37

    13. Write a C programa) To construct a binary search tree of integers.b) To traverse the tree using all the methods i. e., inorder, preorder and postorderc) To display the elements in the tree.

    #include#include

    #define max1 30

    typedef struct n{int info;struct n *left,*right;}*NODE;

    typedef struct q1{

    NODE q[max1];int r,f;

    }QUE;

    void insertq(QUE *Q,NODE tmp){

    (Q->r)++;Q->q[Q->r]=tmp;

    }

    NODE deleteq(QUE *Q){

    NODE tmp;

    tmp= Q->q[Q->f];(Q->f)++;return tmp;

    }

    void disp(NODE root){NODE tmp=root,tt;QUE Q;

    Q.r=-1;Q.f=0;

    30

  • 8/8/2019 All Lab Prog

    31/36

    Data Structures Lab - 06CSEL37

    if(tmp==NULL){

    printf("\n no elements\n");return;

    }

    printf("\nThe elements are\n");insertq(&Q,tmp);

    while(1){

    tt=deleteq(&Q);printf("%d ",tt->info);

    if(tt->left !=NULL)insertq(&Q,tt->left);

    if(tt->right !=NULL)insertq(&Q,tt->right);

    if(Q.rinfo=el;tmp->left=tmp->right=NULL;

    if(*root==NULL){*root=tmp;return;}

    cur=*root;prev=NULL;

    while(cur != NULL){

    prev=cur;

    31

  • 8/8/2019 All Lab Prog

    32/36

    Data Structures Lab - 06CSEL37

    if(el==cur->info){printf("\n duplicate");return;

    }

    if(elinfo)cur=cur->left;else

    cur=cur->right;}

    if(elinfo)prev->left=tmp;

    elseprev->right=tmp;

    }

    void preorder(NODE root){NODE tmp=root;

    if(tmp!=NULL){ printf("%d ",tmp->info);preorder(tmp->left);preorder(tmp->right);

    }}

    void postorder(NODE root){NODE tmp=root;

    if(tmp!=NULL){

    postorder(tmp->left);postorder(tmp->right);printf("%d ",tmp->info);

    }}

    32

  • 8/8/2019 All Lab Prog

    33/36

    Data Structures Lab - 06CSEL37

    void inorder(NODE root){NODE tmp=root;

    if(tmp!=NULL)

    {inorder(tmp->left);printf("%d ",tmp->info);inorder(tmp->right);

    }}int search(NODE root,int el){NODE tmp=root;int fl=-1;

    if(tmp==NULL)return -1;

    while(tmp != NULL){if(tmp->info==el)

    {fl=0;break;

    }if(el > tmp->info)

    tmp=tmp->right;if(el info)

    tmp=tmp->left;}

    return fl;}

    void main(){NODE r;int ch,el,c;

    r=NULL;clrscr();while(1){printf("\n 1:create 2:inorder 3:preorder 4:postorder \n");

    33

  • 8/8/2019 All Lab Prog

    34/36

    Data Structures Lab - 06CSEL37

    fflush();scanf("%d",&ch);

    switch(ch){

    case 1:printf("\nplease enter element to add to the BST\n");scanf("%d",&el);create(&r,el);disp(r);break;

    case 2:printf("\n the inorder traversal is\n");inorder(r);break;

    case 3:printf("\n the preorder traversal is\n");preorder(r);break;

    case 4:printf("\n the postorder traversal is\n");postorder(r);break;

    default:exit(0);}

    }

    }

    /**************************************************************************/14. Write recursive C programs for

    a) Searching an element on a given list of integers using the Binary search method.b) Solving the Towers of Hanoi problem.

    #include#include#includeint bs(int a[],int n,int key,int low,int high){int mid;if(low>high){

    printf("\n Item is not found\n");return;

    }

    34

  • 8/8/2019 All Lab Prog

    35/36

    Data Structures Lab - 06CSEL37

    mid=(low+high)/2;if(key==a[mid]){

    return mid;}

    if(key

  • 8/8/2019 All Lab Prog

    36/36

    Data Structures Lab - 06CSEL37

    #include#include#include#include

    int count=0;

    void tower(int n,int from,int to,int aux){if((n==1)){

    printf("Move disc 1 from %c to %c\n",from,to);count=count+1;return;

    }tower(n-1,from,aux,to);

    printf("Move disc %d from %c to %c\n",n,from,to);count++;tower(n-1,aux,to,from);}

    void main(){int n;char A,B,C;

    clrscr();printf("Enter the number of discs :");scanf("%d",&n);tower(n,'A','B','C');printf("\nTotal no. of disc moves = %d",count);getch();}