Data Structure Rograms

Embed Size (px)

Citation preview

  • 8/7/2019 Data Structure Rograms

    1/50

    Page | 1

    Queue

    A queue is a buffer abstract data structure providing services in computer science,

    transport and operations research where various entities such as data, objects,

    persons, or events are stored and held to be processed later. The most well known

    operation of the queue is the First -In-First-Out (FIFO) queue process. In a FIFO

    queue, the first element in the queue will be the first one out; this is equivalent to therequirement that whenever an element is added, all elements that were added

    before have to be removed before the new element can be invoked. Unless

    otherwise specified, the remainder of the article will refer to FIFO queues. There are

    also non-FIFO queue data structures, like priority queues.

    /******** C Program For Implementation Of Queue ***********/

    #define MAXSIZE 10

    struct st

    {

    int front,rear;

    int queue[MAXSIZE];

    };

    struct st s;

    int empty(void);

    int full(void);

    void add(void);

    void delete(void);

    void display(void);

    void main()

    {

    char ans;

    int ch;

    s.front = 0;

    s.rear = 0;

    do{

    clrscr();

    printf("********Queue Program**********\n");

    printf("1. ADD\n");

    printf("2. DELETE\n");

    printf("3. DISPLAY\n");

    printf("4. QUIT\n");

  • 8/7/2019 Data Structure Rograms

    2/50

    Page | 2

    printf("Enter Your Choice : ");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    add();break;

    case 2:

    delete();

    break;

    case 3:

    display();

    break;

    case 4:

    exit(1);

    break;

    default:

    printf("INVALID CHOICE!!!!!!!!!!!!!!!!\n");

    break;

    }

    printf("\nWant To Go To The Main Menu[y/n]");

    flushall();

    ans = getch();

    }

    while(ans == 'y' ans == 'Y');

    printf("\nPress Any Key To Continue\n");

    getch();

    }

    int full(void)

    {

    if (s.rear == MAXSIZE)

    return(1);

    else

    return(0);

    }int empty(void)

    {

    if (s.front == s.rear + 1)

    return(1);

    else

    return(0);

    }

  • 8/7/2019 Data Structure Rograms

    3/50

    Page | 3

    void add(void)

    {

    char ch;

    int x;

    do{

    if(full() == 1)

    {

    printf("\n\nQueue Full\n");

    break;

    }

    else

    {

    s.rear = s.rear + 1;

    printf("\nEnter An Element to Be Added ");

    scanf("%d",&x);

    s.queue[s.rear] = x;

    if(s.rear == 1) s.front ++;

    }

    printf("\nDo You Want to Add More Elements[y/n]:");

    flushall();

    ch = getch();

    }

    while(ch=='y' ch == 'Y');

    }

    void delete(void)

    {

    char ch;

    do

    {

    if(empty() == 1)

    {

    printf("\n\nQueue Empty\n");

    break;

    }else

    {

    printf("% d Has Been Deleted!",s.queue[s.front]);

    s.front = s.front +1;

    }

    printf("\nWant to Delete More [y\n]");

    flushall();

  • 8/7/2019 Data Structure Rograms

    4/50

    Page | 4

    ch = getch();

    }

    while(ch=='y' ch == 'Y');

    }

    void display(void){

    int i;

    clrscr();

    if(empty () == 1)

    printf("\nQueue Empty!!");

    else

    {

    printf("\nDisplaying Queue\n");

    for(i = s.front;i printf("%d\n",s.queue[i]);

    }

    }

  • 8/7/2019 Data Structure Rograms

    5/50

    Page | 5

    Program for Queue implementation through Array

    #include

    #include

    # define MAXSIZE 200

    int q[MAXSIZE];

    int front, rear;

    void main()

    {

    void add(int);

    int del();

    int will=1,i,num;

    front =0;

    rear = 0;

    clrscr();

    printf(" Program for queue demonstration through array");

    while(will ==1)

    {

    printf(" MAIN MENU:

    1.Add element to queue

    2.Delete element from the queue");

    scanf("%d",&will);

    switch(will)

    {

    case 1:

    printf("Enter the data... ");

    scanf("%d",&num);

    add(num);

    break;

    case 2: i=del();

    printf("Value returned from delete function is %d ",i);

    break;

    default: printf("Invalid Choice ... ");

    }printf(" Do you want to do more operations on Queue ( 1 for yes, any other key to

    exit) ");

    scanf("%d" , &will);

    } //end of outer while

    } //end of main

    void add(int a)

  • 8/7/2019 Data Structure Rograms

    6/50

    Page | 6

    {

    if(rear>MAXSIZE)

    {

    printf(" QUEUE FULL!!!");

    return;}

    else

    {

    q[rear]=a;

    rear++;

    printf("Value of rear = %d and the value of front is %d",rear,front);

    }

    }

    int del()

    {

    int a;

    if(front == rear)

    {

    printf(" QUEUE EMPTY!!!");

    return(0);

    }

    else

    {

    a=q[front];

    front++;

    }

    return(a);

    }

  • 8/7/2019 Data Structure Rograms

    7/50

    Page | 7

    /*Programofqueue usingarray*/

    # include

    # define MAX 5

    int queue_arr[MAX];

    int rear = -1;int front = -1;

    main()

    {

    int choice;

    while(1)

    {

    printf("1.Insert\n");

    printf("2.Delete\n");

    printf("3.Display\n");

    printf("4.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1 :

    insert();

    break;

    case 2 :

    del();

    break;

    case 3:

    display();

    break;

    case 4:

    exit(1);

    default:

    printf("Wrong choice\n");

    }/*End of switch*/

    }/*End of while*/}/*End of main()*/

    insert()

    {

    int added_item;

    if (rear==MAX-1)

    printf("Queue Overflow\n");

  • 8/7/2019 Data Structure Rograms

    8/50

    Page | 8

    else

    {

    if (front==-1) /*If queue is initially empty */

    front=0;

    printf("Input the element for adding in queue : ");

    scanf("%d", &added_item);rear=rear+1;

    queue_arr[rear] = added_item ;

    }

    }/*End of insert()*/

    del()

    {

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

    {

    printf("Queue Underflow\n");

    return ;

    }

    else

    {

    printf("Element deleted from queue is : %d\n", queue_arr[front]);

    front=front+1;

    }

    }/*End of del() */

    display()

    {

    int i;

    if (front == -1)

    printf("Queue is empty\n");

    else

    {

    printf("Queue is :\n");

    for(i=front;i

  • 8/7/2019 Data Structure Rograms

    9/50

    Page | 9

    complete single linked list program

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

    Description : Single linked list implementation using different functions 1.INSERT ANUMBER AT THE BEGINNING; 2.INSERT A NUMBER AT LAST 3.INSERT A

    NUMBER AT A PARTICULAR LOCATION IN LIST 4.PRINT THE ELEMENTS INTHE LIST 5.PRINT THE TOTAL NUMBER OF ELEMENTS IN THE LIST 6.DELETEA NODE IN THE LINKED LIST: 7.REVERSE A LINKED LIST : 8.GET OUT OFLINKED LIST (BYEE BYEE):

    Code :

    /* PROGRAM IMPLEMENTATION OF SINGLE LINKED LIST */

    #include//#define NULL 0/* STRUCTURE CONTANING A DATA PART AND A LINK PART */

    struct node{int data;struct node *next;}*p;

    /* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE FIRST NODE INLIST*/

    /*THIS FUNCTION DELETES A NODE */

    delnode(int num){struct node *temp, *m;temp=p;while(temp!=NULL){if(temp->data==num){if(temp==p){

    p=temp->next;free(temp);return;}else{m->next=temp->next;free(temp);return;

  • 8/7/2019 Data Structure Rograms

    10/50

    Page | 10

    }}else{m=temp;temp= temp->next;}

    }printf("ELEMENT %d NOT FOUND!!!", num);}/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */

    append( int num ){struct node *temp,*r;/* CREATING A NODE AND ASSIGNING A VALUE TO IT */

    temp= (struct node *)malloc(sizeof(struct node));temp->data=num;

    r=(struct node *)p;

    if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */{p=temp;p->next =NULL;}else{ /* GO TO LAST AND ADD*/

    while( r->next != NULL)r=r->next;r->next =temp;r=temp;r->next=NULL;}}/* ADD A NEW NODE AT BEGINNING */

    addbeg(int num){/* CREATING A NODE AND INSERTING VALUE TO IT */

    struct node *temp;

    temp=(struct node *)malloc(sizeof(struct node));temp->data=num;

    /* IF LIST IS NULL ADD AT BEGINNING */if ( p== NULL){p=temp;p->next=NULL;}

  • 8/7/2019 Data Structure Rograms

    11/50

    Page | 11

    else{temp->next=p;p=temp;}

    }

    /* ADD A NEW NODE AFTER A SPECIFIED NO OF NODES */

    addafter(int num, int loc){int i;struct node *temp,*t,*r;r=p; /* here r stores the first location */if(loc > count()+1 || loc data=num;t->next=temp;t=temp;t->next=r;return;}}/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */

    display(struct node *r){r=p;if(r==NULL){printf("NO ELEMENT IN THE LIST :");return;}/* traverse the entire linked list */

  • 8/7/2019 Data Structure Rograms

    12/50

    Page | 12

    while(r!=NULL){printf(" -> %d ",r->data);r=r->next;}printf("
    ");

    }//THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LISTcount(){struct node *n;int c=0;n=p;while(n!=NULL){n=n->next;c++;}

    return(c);}//THIS FUNCTION REVERSES A LINKED LISTreverse(struct node *q){struct node *m, *n,*l,*s;m=q;n=NULL;while(m!=NULL){s=n;n=m;m=m->next;n->next=s;}p=n;}

    /* THIS IS THE MAIN PROGRAM */

    main(){

    int i;clrscr();p=NULL;while(1) /* this is an indefinite loop */{printf("1.INSERT A NUMBER AT BEGINNING);printf("2.INSERT A NUMBER AT LAST);printf("3.INSERT A NUMBER AT A PARTICULAR LOCATION IN LIST);printf("4.PRINT THE ELEMENTS IN THE LIST);

  • 8/7/2019 Data Structure Rograms

    13/50

    Page | 13

    printf("5.PRINT THE NUMBER OF ELEMENTS IN THE LIST);printf("6.DELETE A NODE IN THE LINKED LIST);printf("7.REVERSE A LINKED LIST);printf("8.GET OUT OF LINKED LIST (BYEE BYEE));printf("PLEASE, ENTER THE NUMBER:");

    scanf("%d",&i); /* ENTER A VALUE FOR SWITCH */

    switch(i){case 1:{int num;printf("PLEASE ENTER THE NUMBER :-");scanf("%d",&num);addbeg(num);break;}

    case 2:{int num;printf("PLEASE ENTER THE NUMBER :-");scanf("%d",&num);append(num);break;}

    case 3:{int num, loc,k;printf("PLEASE ENTER THE NUMBER :-");scanf("%d",&num);printf("PLEASE ENTER THE LOCATION NUMBER :-");scanf("%d",&loc);addafter(num,loc);break;} case 4:{struct node *n;printf("THE ELEMENTS IN THE LIST ARE :
    );display(n);

    break;}

    case 5:{struct node *n;display(n);printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count());break;

  • 8/7/2019 Data Structure Rograms

    14/50

    Page | 14

    } case 6:{int num;printf("PLEASE ENTER A NUMBER FROM THE LIST :");scanf("%d",&num);delnode(num);

    break;}case 7:{reverse(p);display(p);break;}case 8:{exit();}

    }/* end if switch */}/* end of while */}/* end of main */

  • 8/7/2019 Data Structure Rograms

    15/50

    Page | 15

    PROGRAM TOADD TWOPOLYNOMIALS

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

    Description : Not Specified

    /******************************************************************************

    PROGRAM TO ADD TWO POLYNOMIALS

    ******************************************************************************/

    #include

    #include

    struct barbie

    {

    int coff;

    int pow;

    struct barbie *link;

    }*ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2;

    typedef struct barbie bar;

    int temp1,temp2;

    void main()

    {

    void create(void);

    void prnt(void);

    void suml(void);

    void sort(void);

    clrscr();

    printf("Enrter the elements of the first poly :");

    node = (bar *) malloc(sizeof (bar));

    start1=node;

    if (start1==NULL)

    {

    printf("Unable to create memory.");

    getch();

    exit();}

    create();

    printf("Enrter the elements of the second poly :");

    node = (bar *) malloc(sizeof (bar));

    start2=node;

    if (start2==NULL)

  • 8/7/2019 Data Structure Rograms

    16/50

    Page | 16

    {

    printf("Unable to create memory.");

    getch();

    exit();

    }

    create();clrscr();

    //printing the elements of the lists

    printf("The elements of the poly first are :");

    ptr=start1;

    prnt();

    printf("The elements of the poly second are :");

    ptr=start2;

    prnt();

    printf("The first sorted list is :");

    ptr=start1;

    sort();

    ptr=start1;

    prnt();

    printf("The second sorted list is :");

    ptr=start2;

    sort();

    ptr=start2;

    prnt();

    printf("The sum of the two lists are :");

    suml();

    ptr=start3;

    prnt();

    getch();

    }

    /*-----------------------------------------------------------------------------*/

    void create()

    {

    char ch;

    while(1)

    {

    printf("Enter the coff and pow :");

  • 8/7/2019 Data Structure Rograms

    17/50

    Page | 17

    scanf("%d%d",&node->coff,&node->pow);

    if (node->pow==0 )

    {

    ptr=node;

    node=(bar *)malloc(sizeof(bar));

    node=NULL;ptr->link=node;

    break;

    }

    printf("Do u want enter more coff ?(y/n)");

    fflush(stdin);

    scanf("%c",&ch);

    if (ch=='n' )

    {

    ptr=node;

    node=(bar *)malloc(sizeof(bar));

    node=NULL;

    ptr->link=node;

    break;

    }

    ptr=node;

    node=(bar *)malloc(sizeof(bar));

    ptr->link=node;

    }

    }

    /*-------------------------------------------------------------------------*/

    void prnt()

    { int i=1;

    while(ptr!=NULL )

    {

    if(i!=1)

    printf("+ ");

    printf(" %dx^%d ",ptr->coff,ptr->pow);

    ptr=ptr->link;

    i++;}

    //printf(" %d^%d",ptr->coff,ptr->pow);

    }

    /*---------------------------------------------------------------------------*/

    void sort()

    {

    for(;ptr->coff!=NULL;ptr=ptr->link)

  • 8/7/2019 Data Structure Rograms

    18/50

    Page | 18

    for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link)

    {

    if(ptr->pow>ptr2->pow)

    {

    temp1=ptr->coff;

    temp2=ptr->pow;ptr->coff=ptr2->coff;

    ptr->pow=ptr2->pow;

    ptr2->coff=temp1;

    ptr2->pow=temp2;

    }

    }

    }

    /*---------------------------------------------------------------------------*/

    void suml()

    {

    node=(bar *)malloc (sizeof(bar));

    start3=node;

    ptr1=start1;

    ptr2=start2;

    while(ptr1!=NULL && ptr2!=NULL)

    {

    ptr=node;

    if (ptr1->pow > ptr2->pow )

    {

    node->coff=ptr2->coff;

    node->pow=ptr2->pow;

    ptr2=ptr2->link; //update ptr list B

    }

    else if ( ptr1->pow < ptr2->pow )

    {

    node->coff=ptr1->coff;

    node->pow=ptr1->pow;

    ptr1=ptr1->link; //update ptr list A}

    else

    {

    node->coff=ptr2->coff+ptr1->coff;

    node->pow=ptr2->pow;

    ptr1=ptr1->link; //update ptr list A

    ptr2=ptr2->link; //update ptr list B

  • 8/7/2019 Data Structure Rograms

    19/50

    Page | 19

    }

    node=(bar *)malloc (sizeof(bar));

    ptr->link=node; //update ptr list C

    }//end of while

    if (ptr1==NULL) //end of list A

    {

    while(ptr2!=NULL)

    {

    node->coff=ptr2->coff;

    node->pow=ptr2->pow;

    ptr2=ptr2->link; //update ptr list B

    ptr=node;

    node=(bar *)malloc (sizeof(bar));

    ptr->link=node; //update ptr list C

    }

    }

    else if (ptr2==NULL) //end of list B

    {

    while(ptr1!=NULL)

    {

    node->coff=ptr1->coff;

    node->pow=ptr1->pow;

    ptr1=ptr1->link; //update ptr list B

    ptr=node;

    node=(bar *)malloc (sizeof(bar));

    ptr->link=node; //update ptr list C

    }

    }

    node=NULL;

    ptr->link=node;

    }

  • 8/7/2019 Data Structure Rograms

    20/50

    Page | 20

    BinarySearch C Program#include //header file#include //header file#include //header fileint s;void bin(int a[],int,int);

    void main(){int l,u,i,a[10];clrscr();l=0;u=9;for(i=0;ia[m])

    {if(l

  • 8/7/2019 Data Structure Rograms

    21/50

    Page | 21

    getch();exit(0);}}else if(s==a[m]){

    printf("element exists at %d",m);getch();exit(0);}}

  • 8/7/2019 Data Structure Rograms

    22/50

    Page | 22

    Infix ToPrefix Conversion

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

    Description : Not Specified

    /*Infix to Prefix And Postfix*/

    /*Assignment:5*/

    /*Roll No:2102*/

    #include

    #include

    #include

    #define MAX 15

    #define true 1

    #define false 0

    /*Structure Declaration*/

    typedef struct

    {

    char data[MAX];

    char top;

    }STK;

    /*Function Declarations*/

    void input(char str[]);

    void intopre(char str1[],char pre[]);

    void intopost(char str1[],char post[]);

    int isoperand(char sym);

    int prcd(char sym);

    void push(STK *s1,char elem);

    int pop(STK *s1);

    int empty(STK *s2);

    int full(STK *s2);

    void dis(char str[]);

    void main()

    {

    STK s;

    int cs,ans;

    char str[MAX],pre[MAX],post[MAX];

    clrscr();

    do /*Using Do-while Loop*/

  • 8/7/2019 Data Structure Rograms

    23/50

    Page | 23

    {

    clrscr();

    printf("-----Program for Expressions-----");

    printf("Input The String:");

    printf("MENU);

    printf("1.Infix to Prefix);printf("2.Infix to Postfix");

    printf("3.Exit");

    cs=getche();

    switch(cs) /*Using Switch Case*/

    {

    case 1:

    intopre(str,pre);

    break;

    case 2:

    intopost(str,post);

    break;

    case 3:

    break;

    default:

    printf("Enter a Valid Choice!"); /*Default Case*/

    break;

    }

    printf("Do you wish to Continue?(y/n)");

    ans=getche();

    }while(ans=='y'||ans=='Y'); /*Condition for Do-while loop*/

    getch();

    }

    /**************************************************/

    /*To Input String*/

    /**************************************************/

    void input(char str)

    {

    printf("Enter the Infix String:");scanf("%s",str);

    }

    /**************************************************/

    /*To Covert Infix To Prefix*/

    /**************************************************/

    void intopre(STK s1,char str1[],char pre[])

  • 8/7/2019 Data Structure Rograms

    24/50

    Page | 24

    {

    int len,flag;

    len=strlen(str1);

    int check=0,cnt=len-1,pos=0;

    char elem;

    while(cnt>=0) /*while condition*/

    {

    flag=0;

    if(isoperand(str1[cnt])) /*Checking for Operand*/

    {

    printf("%c",str1[cnt]);

    cnt--;

    pos++;

    }

    else

    {

    check=prcd(str1[cnt]);

    while(check==false)

    {

    pre[pos]=str1[cnt];

    flag=1;

    pos++;

    cnt--;

    }

    if(flag==0)

    {

    elem=pop(&s1);

    printf("%c",elem);

    }

    }

    }

    }

    /**************************************************/

    /*To Convert Infix To Postfix*//**************************************************/

    void intopost(STK s1,char str1[],char post[])

    {

    int len;

    len=strlen(str1);

    int check=0,cnt=len-1,pos=0;

  • 8/7/2019 Data Structure Rograms

    25/50

    Page | 25

    }

    /**************************************************/

    /*To Check For Operand*/

    /**************************************************/

    int isoperand(char sym){

    if('A'

  • 8/7/2019 Data Structure Rograms

    26/50

    Page | 26

    /******************************************/

    /*Full Function Definition*/

    /******************************************/

    int full(STK *s2)

    {

    if(s2->top==MAX) /*Condition for Full*/return(true);

    return(false);

    }

    /******************************************/

    /*Pop Function Definition*/

    /******************************************/

    int pop(STK *s1)

    {

    char elem;

    if(!empty(s1))

    {

    elem=s1->data[s1->top]; /*Storing top stack element in elem*/

    s1->top--; /*Decrementing top*/

    return(elem);

    }

    return(false);

    }

    /******************************************/

    /*Empty Function Definition*/

    /******************************************/

    int empty(STK *s2)

    {

    if(s2->top==-1) /*Condition For Empty*/

    return(true);

    return(false);

    }

  • 8/7/2019 Data Structure Rograms

    27/50

    Page | 27

    Creation ofBinary search tree#include#includestruct tree{int data;

    struct tree *left;struct tree *right;};struct tree *create();void preorder(struct tree *);void inorder(struct tree *);void postorder(struct tree *);struct tree *create(){struct tree *p,*root;int m,x;char s;

    root=(struct tree *)malloc(sizeof(struct tree));printf("\nenter the value of the main root");scanf("%d",&m);root->data=m;root->left=NULL;root->right=NULL;printf("\nenter n to stop creation of the binary search tree");fflush(stdin);scanf("%c",&s);while(s!='n'){p=root;printf("\nenter the value of the newnode");fflush(stdin);scanf("%d",&x);while(1){if(xdata){if(p->left==NULL){p->left=(struct tree *)malloc(sizeof(struct tree));p=p->left;

    p->data=x;p->right=NULL;p->left=NULL;break;}elsep=p->left;}else

  • 8/7/2019 Data Structure Rograms

    28/50

    Page | 28

    {if(p->right==NULL){p->right=(struct tree *)malloc(sizeof(struct tree));p=p->right;p->data=x;

    p->right=NULL;p->left=NULL;break;}elsep=p->right;}}printf("\nwant to continue");fflush(stdin);scanf("%c",&s);}

    return(root);}void preorder(struct tree *p){if(p!=NULL){printf("%d ",p->data);preorder(p->left);preorder(p->right);}}void inorder(struct tree *p){if(p!=NULL){inorder(p->left);printf("\t%d",p->data);inorder(p->right);}}void postorder(struct tree *p){if(p!=NULL)

    {postorder(p->left);postorder(p->right);printf("\t%d",p->data);}}void main(){int h;

  • 8/7/2019 Data Structure Rograms

    29/50

    Page | 29

    struct tree *root;clrscr();while(1){printf("\nenter 1. for creation of the binary search tree");printf("\nenter 2. for preorder traversal");

    printf("\nenter 3. for inorder traversal");printf("\nenter 4. for postorder traversal");printf("\nenter 5. for exit");printf("\nenter your choice");scanf("%d",&h);switch(h){case 1:root=create();break;case 2:preorder(root);

    break;case 3:inorder(root);break;case 4:postorder(root);break;case 5:exit(0);default:printf("\nentered a wrong choice");}}}

  • 8/7/2019 Data Structure Rograms

    30/50

    Page | 30

    Program to demonstrate linked list operations.

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

    # include

    # include

    # include

    struct node

    {

    int data;

    struct node *link;

    };

    void main()

    {

    int a=111,b=2,c=3,will,wish,num;

    struct node *ptr,*ptr2,*result,*temp;

    void add(struct node **,int );

    struct node * search(struct node *);

    void display(struct node *);

    void invert(struct node *);

    void del(struct node *,int);

    struct node * concat(struct node *,struct node *);

    ptr=NULL;

    ptr2=NULL;

    result=NULL; //result for storing the result of concatenation

    clrscr();

    will=1;

    while(will==1)

    {

    printf("Main Menu

    1. Add element

    2.Delete element

    3.Search element4Linked List concatenation

    5.Invert linked list

    6. Display elements

    Please enter the choice");

    scanf("%d",&wish);

    switch(wish)

    {

  • 8/7/2019 Data Structure Rograms

    31/50

    Page | 31

    case 1:

    printf("Enter the element you want to add ");

    scanf("%d",&num);

    add(&ptr,num);

    display(ptr);

    break;case 2:

    printf("Enter the element to delete ");

    scanf("%d",&num);

    del(ptr,num);

    break;

    case 3:

    printf("Now demonstrating search ");

    temp = search(ptr);

    printf("Address of first occurence is %u ",temp);

    break;

    case 4:

    /* Inputs given internally for demo only */

    printf(" Now demonstrating linked list concatenation

    Press any key to continue...");

    add(&ptr2,2);

    add(&ptr2,4);

    add(&ptr2,6);

    getch();

    printf("Displaying second Linked List );

    display(ptr2);

    getch();

    result = concat(ptr,ptr2);

    clrscr();

    printf("Now Displaying the result of concatenation");

    display(result);

    getch();

    break;

    case 5:

    printf("Inverting the list ...

    Press any key to continue...");invert(ptr);

    break;

    case 6:

    display(ptr);

    break;

    default:

    printf("Illegal choice);

  • 8/7/2019 Data Structure Rograms

    32/50

    Page | 32

    }

    printf("DO you want to continue ( press 1 for yes ");

    scanf("%d",&will);

    } //end of while

    }

    void add(struct node **q,int num)

    {

    struct node *temp;

    temp = *q;

    if(*q==NULL)

    {

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

    temp = *q;

    }

    else

    {

    while((temp->link)!=NULL)

    {

    temp=temp->link;

    }

    temp->link = malloc(sizeof(struct node));

    temp=temp->link;

    }

    temp->data = num;

    temp->link = NULL;

    }

    void display(struct node *pt)

    {

    while(pt!=NULL)

    {

    printf("Data : %d",pt->data);

    printf("Link : %d",pt->link);pt=pt->link;

    }

    }

    void invert(struct node *ptr)

    {

  • 8/7/2019 Data Structure Rograms

    33/50

    Page | 33

    struct node *p,*q,*r;

    p=ptr;

    q=NULL;

    while(p!=NULL){

    r=q;

    q=p;

    p=p->link;

    q->link=r;

    }

    ptr = q;

    display(ptr);

    }

    // CONCATENATION OF LINKED LISTS

    struct node * concat(struct node *p,struct node *q)

    {

    struct node *x,*r;

    if (p==NULL)

    r=q;

    if (q==NULL)

    r=p;

    else

    {

    x=p;

    r=x;

    while(x->link!=NULL)

    x=x->link;

    x->link=q;

    }return(r);

    }

    // SEARCHING AN ELEMENT IN THE LINKED LIST

    // THIS FUNCTION FINDS THE FIRST OCCURENCE OF

    // A DATA AND RETURNS A POINTER TO ITS ADDRESS

  • 8/7/2019 Data Structure Rograms

    34/50

    Page | 34

    struct node * search(struct node *p)

    {

    struct node *temp;

    int num;

    temp = p;printf("Enter the data that you want to search ");

    scanf("%d",&num);

    printf("Link of temp %u", temp->link);

    while(temp->link!=NULL)

    {

    printf("In while ");

    if(temp->data == num)

    return(temp);

    temp=temp->link;

    }

    return(NULL);

    }

    // DELETING DATA FROM THE LINKED LIST//

    void del(struct node *p,int num)

    {

    struct node *temp,*x;

    temp=p;

    x= NULL;

    while (temp->link !=NULL)

    {

    if(temp->data == num)

    {

    if (x==NULL)

    {

    p = temp->link;free(temp);

    return;

    }

    else

    {

    x->link = temp->link;

    free(temp);

  • 8/7/2019 Data Structure Rograms

    35/50

    Page | 35

    return;

    }

    } //end of outer if

    x=temp;

    temp=temp->link;

    } //end of whileprintf("No such entry to delete ");

    } //end of fn.

  • 8/7/2019 Data Structure Rograms

    36/50

    Page | 36

    Threaded Binary Tree C program #include#includestruct tree{int data;

    struct tree *left;struct tree *right;};int i=0,a[20],c=0;struct tree *s,*t,*header;struct tree *create();void inorder(struct tree *);void inorder1(struct tree *);void header_node(struct tree *p);void display(struct tree *p);struct tree *create(){

    struct tree *p,*root;int m,x;char s;root=(struct tree *)malloc(sizeof(struct tree));printf("\nenter the value of the main root");scanf("%d",&m);root->data=m;root->left=NULL;root->right=NULL;printf("\nenter n to discontinue creation of the binary search tree");fflush(stdin);scanf("%c",&s);while(s!='n'){p=root;printf("\nenter the value of the newnode");fflush(stdin);scanf("%d",&x);while(1){if(xdata){if(p->left==NULL)

    {p->left=(struct tree *)malloc(sizeof(struct tree));p=p->left;p->data=x;p->right=NULL;p->left=NULL;break;}else

  • 8/7/2019 Data Structure Rograms

    37/50

    Page | 37

    p=p->left;}else{if(p->right==NULL){

    p->right=(struct tree *)malloc(sizeof(struct tree));p=p->right;p->data=x;p->right=NULL;p->left=NULL;break;}elsep=p->right;}}printf("\nwant to continue");

    fflush(stdin);scanf("%c",&s);}return(root);}void inorder(struct tree *p){if(p!=NULL){inorder(p->left);a[i]=p->data;i++;inorder(p->right);}}void inorder1(struct tree *p){if(p!=NULL){inorder1(p->left);if(a[c]==p->data)s=p;else if(a[c+1]==p->data)

    {t=p;if(s->right==NULL)s->right=t;else if(t->left==NULL)t->left=s;s=t;c++;}

  • 8/7/2019 Data Structure Rograms

    38/50

    Page | 38

    inorder1(p->right);}}void header_node(struct tree *p){struct tree *top;

    int m;top=p;header=(struct tree *)malloc(sizeof(struct tree));printf("\nenter the value of the header node");scanf("%d",&m);header->data=m;while(p->right!=NULL)p=p->right;p->right=header;p=top;while(p->left!=NULL)p=p->left;

    p->left=header;header->left=top;header->right=top;}void display(struct tree *p){char a;while(p->right!=header)p=p->right;printf("\npress n to quit display");scanf("%c",&a);while(a!='n'){while(p->left!=header){printf("\t%d",p->data);p=p->left;}printf("\t%d\t",header->data);while(p->right!=header){printf("\t%d",p->data);p=p->right;

    }printf("\npress n to discontinue");scanf("%c",&a);}}void main(){int h;struct tree *root;

  • 8/7/2019 Data Structure Rograms

    39/50

    Page | 39

    clrscr();while(1){printf("\nenter 1. for creation of the binary search tree");printf("\nenter 2. for creation of a binary threaded tree");printf("\nenter 3. for display");

    printf("\nenter 4. for exit");printf("\nenter your choice");scanf("%d",&h);switch(h){case 1:root=create();break;case 2:inorder(root);inorder1(root);header_node(root);

    break;case 3:display(root);break;case 4:exit(0);default:printf("\nentered a wrong choice");}}}

  • 8/7/2019 Data Structure Rograms

    40/50

    Page | 40

    Simple Linked List#include#include#includestruct linklist{

    int info;struct linklist *next;};struct linklist *start=NULL;int x=0,c=0;void create_node();void display();void insertion_info();void split();void deletion_before();void deletion_after();void deletion_info();

    void insertion_before();void insertion_after();void insertion_after(){struct linklist *newnode,*p,*t;int data,b,i;if(start!=NULL){printf("\nthe total no. of nodes are= %d",c);printf("\nenter the no. of node after which the new node required to b e inserted");scanf("%d",&b);if(b>c){printf("wrong choice entered");goto z;}}printf("\nenter the information of the newnode");scanf("%d",&data);newnode=(struct linklist *)malloc(sizeof(struct linklist));c++;newnode->info=data;p=start;

    if(start==NULL){newnode->next=NULL;start=newnode;}else{for(i=1;i

  • 8/7/2019 Data Structure Rograms

    41/50

    Page | 41

    t=p;p=p->next;}t->next=newnode;newnode->next=p;}

    z:}void create_node(){int m,i;struct linklist *newnode;printf("\nenter the no. of nodes to be inserted");scanf("%d",&x);if(xnext=start;start=newnode;}c=c+x;}}void display(){struct linklist *p;p=start;if(p==NULL)printf("link list is empty");else{while(p!=NULL){printf("\t%d",p->info);

    p=p->next;}}}void insertion_before(){struct linklist *p,*t,*newnode;int n,s,i;if(start!=NULL )

  • 8/7/2019 Data Structure Rograms

    42/50

    Page | 42

    {printf("\nthe total no. of nodes are= %d",c);printf("\nenter the no. before which the node has to be inserted");scanf("%d",&n);if(n>c){

    printf("wrong choice");goto x;}}printf("\nenter the information of the inserted node");scanf("%d",&s);p=start;newnode=(struct linklist*)malloc(sizeof(struct linklist));c++;newnode->info=s;if(start==NULL || n==1){

    newnode->next=start;start=newnode;}else{for(i=1;inext;}t->next=newnode;newnode->next=p;}x:}void insertion_info(){struct linklist *newnode,*p,*t;int i,j,a,s,q,m;printf("\nenter the information to be inserted");scanf("%d",&s);newnode=(struct linklist*)malloc(sizeof(struct linklist));c++;newnode->info=s;

    t=start;p=t->next;if(start==NULL || t->info>s){newnode->next=start;start=newnode;}else{

  • 8/7/2019 Data Structure Rograms

    43/50

    Page | 43

    for(i=1;inext;p=t->next;

    }for(j=i+1;jinfo;m=p->info;if(q>m){a=q;q=m;m=a;}p=p->next;

    }}p=start;while(p->infonext;if(p==NULL){t->next=newnode;newnode->next=p;break;}}if(p!=NULL){t->next=newnode;newnode->next=p;}}}void split(){

    struct linklist *v,*t;int n,i;v=start;printf("\nenter the node no. after which the linked list is to be splitted");scanf("%d",&n);if(start==NULL)printf("\nthe linked list is empty");else{

  • 8/7/2019 Data Structure Rograms

    44/50

    Page | 44

    printf("\nthe splitted linked lists are ->");for(i=1;inext;}

    t->next=NULL;display();printf("\n\t");while(v!=NULL){printf("\t%d",v->info);v=v->next;}}}void deletion_before(){

    struct linklist *p,*t;int j,i;p=start;if(start!=NULL){printf("\nenter the no. of node before which the node has to be deleted");scanf("%d",&j);}if(start==NULL)printf("\nlinked list is empty");else if(j==1)printf("\nwrong choice");else if(j==2){start=p->next;free(p);}else{for(i=1;inext;

    }t->next=p->next;free(p);}}void deletion_after(){struct linklist *p,*t;int y,i;

  • 8/7/2019 Data Structure Rograms

    45/50

    Page | 45

    p=start;if(start!=NULL){printf("\nenter the no. of node before which the node has to be deleted");scanf("%d",&y);}

    if(start==NULL)printf("\nlinked list is empty");else{for(i=1;inext;}t->next=p->next;free(p);}

    }void deletion_info(){int s;struct linklist *p,*t;p=start;if(p==NULL)printf("\nlinked list is empty");else{printf("\nenter the information to be deleted");scanf("%d",&s);while(p->info !=s){if(p==NULL){printf("\ninformation is not present in the linked list");break;}t=p;p=p->next;}if(p==start)

    {start=p->next;free(p);}t->next=p->next;free(p);}}void main()

  • 8/7/2019 Data Structure Rograms

    46/50

    Page | 46

    {int ch;clrscr();while(1){printf("\nenter 1. for creation of node");

    printf("\nenter 2. for display of linked list");printf("\nenter 3. for insertion before a node");printf("\nenter 4. for insertion after a node");printf("\nenter 5. for insertion according to information of the node");printf("\nenter 6. for splitting the linked list");printf("\nenter 7. for deletion before a node");printf("\nenter 8. for deletion after a node");printf("\nenter 9. for deletion according to information");printf("\nenter 10. for exit");printf("\nenter your choice");fflush(stdin);scanf("%d",&ch);

    switch(ch){case 1:create_node();break;case 2:display();break;case 3:insertion_before();break;case 4:insertion_after();break;case 5:insertion_info();break;case 6:split();break;case 7:deletion_before();break;

    case 8:deletion_after();break;case 9:deletion_info();break;case 10:exit(0);default:

  • 8/7/2019 Data Structure Rograms

    47/50

    Page | 47

    printf("\nentered a wrong choice");}}getch();}

  • 8/7/2019 Data Structure Rograms

    48/50

    Page | 48

    Programfordemonstration ofTree Operations - INSERTION . INORDER .

    PREORDER . POSTORDER TRAVERSAL

    # include

    # include

    # include

    struct node

    {

    struct node *left;

    int data;

    struct node *right;

    } ;

    void main()

    {

    void insert(struct node **,int);

    void inorder(struct node *);

    void postorder(struct node *);

    void preorder(struct node *);

    struct node *ptr;

    int will,i,num;

    ptr = NULL;

    ptr->data=NULL;

    clrscr();

    printf("Enter the number of terms you want to add to the tree.");

    scanf("%d",&will);

    /* Getting Input */

    for(i=0;i

  • 8/7/2019 Data Structure Rograms

    49/50

    Page | 49

    printf("POSTORDER TRAVERSAL");

    postorder(ptr);

    getch();

    }

    void insert(struct node **p,int num){

    if((*p)==NULL)

    { printf("Leaf node created.");

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

    (*p)->left = NULL;

    (*p)->right = NULL;

    (*p)->data = num;

    return;

    }

    else

    { if(num==(*p)->data)

    {

    printf(" REPEATED ENTRY ERROR\nVALUE REJECTED");

    return;

    }

    if(numdata)

    {

    printf("Directed to left link.");

    insert(&((*p)->left),num);

    }

    else

    {

    printf("Directed to right link.");

    insert(&((*p)->right),num);

    }

    }

    return;

    }

    void inorder(struct node *p)

    {if(p!=NULL)

    {

    inorder(p->left);

    printf("Data :%d",p->data);

    inorder(p->right);

    }

    else

  • 8/7/2019 Data Structure Rograms

    50/50

    return;

    }

    void preorder(struct node *p)

    {

    if(p!=NULL){

    printf("Data :%d",p->data);

    preorder(p->left);

    preorder(p->right);

    }

    else

    return;

    }

    void postorder(struct node *p)

    {

    if(p!=NULL)

    {

    postorder(p->left);

    postorder(p->right);

    printf("Data :%d",p->data);

    }

    else

    return;

    }