Top Banner

of 56

Data Structures File

Feb 21, 2018

Download

Documents

Ankit Jain
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/24/2019 Data Structures File

    1/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 1

  • 7/24/2019 Data Structures File

    2/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 2

    PROGRAM 1:

    WAP to find length of a string.

    #include

    #include

    #include

    void main()

    {clrscr();

    char string[20];

    int len;

    printf("Enter the string:\n");

    gets(string);len=strlen(string);

    printf("\nThe length of the given string is %d",len);

    getch();

    }

    OUTPUT

    Enter the string:

    Student

    The length of the given string is 7

  • 7/24/2019 Data Structures File

    3/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 3

    PROGRAM 2:

    WAP to compare two strings.

    #include

    #include

    #include

    void main()

    {clrscr();

    char string1[20],string2[20];

    printf("\nEnter string1:\n");

    gets(string1);

    printf("\nEnter string2:\n");

    gets(string2);

    int l=strcmp(string1,string2);if(l==0)

    printf("\nThe given strings are equal");

    else

    printf("\nThe given strings are not equal");

    getch();

    }

    OUTPUT

    Enter string1:

    What

    Enter string2:

    Perpendicular

    The given strings are not equal

    Enter string1:

    Data

    Enter string2:

    Data

    The given strings are equal

  • 7/24/2019 Data Structures File

    4/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 4

    PROGRAM 3:

    WAP for concatenation of two strings.

    #include

    #include

    #include

    void main()

    {clrscr();

    char a[10],b[10];

    printf("\NEnter string1:\n ");

    gets(a);

    printf("\nEnter string2:\n ");

    gets(b);

    strcat(a,b);printf("\nThe concatenated string is %s",a);

    getch();

    }

    OUTPUT

    Enter string1:Naughty

    Enter string2:

    Student

    The concatenated string is NaughtyStudent

  • 7/24/2019 Data Structures File

    5/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 5

    PROGRAM 4:

    WAP for finding a character in a string.

    #include

    #include

    #include

    void main()

    {

    char string[20],ch,*ptr;

    printf("Enter string:\n");

    gets(string);

    printf("\nEnter the character to be found:\n");

    scanf("%c",&ch);

    ptr=strchr(string,ch);if(ptr)

    printf("\nThe character %c is at position %d\n",ch,(ptr-string+1));

    else

    printf("\nThe given string does not contain the character %c",ch);

    getch();

    }

    OUTPUT

    Enter string:

    Computers

    Enter character to be found:

    u

    The character u is at position 5

    Enter string:

    Computers

    Enter character to be found:

    A

    The given string does not contain the character A

  • 7/24/2019 Data Structures File

    6/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 6

    PROGRAM 5:

    WAP for Finding a substring in a given string.

    #include

    #include

    #include

    void main()

    {

    char string[20],sub[20],*ptr;

    printf("Enter string: ");

    gets(string);

    printf("\nEnter the substring to be found: ");

    gets(sub);

    ptr=strstr(string,sub);if(ptr!=NULL)

    {printf("\nThe given string contains the substring");

    printf("\nThe substring is %s\n", ptr);

    }

    else

    printf("\nThe given string does not contain the substring");

    getch();

    }

    OUTPUT

    Enter string: student

    Enter the substring to be found: den

    The given string contains the substring

    The substring is

    den

  • 7/24/2019 Data Structures File

    7/56

  • 7/24/2019 Data Structures File

    8/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 8

    }

    void main()

    {

    int item,ch,del;

    do

    {printf("\n1.Push");printf("\n2.Pop");

    printf("\n3.Display");

    printf("\n4.Exit");

    printf("\nEnter your choice:");

    scanf("%d",&ch);

    switch(ch)

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

    scanf("%d",&item);

    push(&st,item);

    break;

    case 2: del=pop(&st);

    if(del!=-1)

    printf("\nThe deleted element is %d",del);

    break;

    case 3: show(&st);

    break;

    case 4: printf(\mGOOD BYE!!);

    getch();

    exit(0);

    break;

    default: printf("\n Wrong choice");}

    }while(ch>=1&&ch

  • 7/24/2019 Data Structures File

    9/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 9

    Enter element to be inserted:16

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:1

    Enter element to be inserted:08

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:1

    Enter element to be inserted:93

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:3

    The elements of stack are

    16

    08

    93

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:2

    The deleted element is 93

    1.Push

    2.Pop

  • 7/24/2019 Data Structures File

    10/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 10

    3.Display

    4.Exit

    Enter your choice:4

    GOOD BYE!!

  • 7/24/2019 Data Structures File

    11/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 11

    PROGRAM 7:

    Write a program to implement stack using linked list.

    #include

    #include

    struct stack

    {

    int data;

    struct stack *next;

    };

    typedef struct stack stack;

    stack *alloc()

    {

    return (stack*)malloc(sizeof(stack));}

    void push(stack**, int);

    int pop(stack**);

    void display(stack*);

    void main()

    {int ch,item,delitem;

    stack *top;

    top=NULL;

    do

    {printf("\n1.Push");

    printf("\n2.Pop");

    printf("\n3.Display");

    printf("\n4.Exit");

    printf("\nEnter your choice:");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    printf("\nEnter element to be inserted:");

    scanf("%d",&item);

    push(&top,item);

    break;

    case 2: delitem=pop(&top);

    if(delitem!=-1)

    printf("\nThe deleted element is %d",delitem);

    break;

  • 7/24/2019 Data Structures File

    12/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 12

    case 3:

    printf("\nThe elements of the stack are\n\n");

    printf("\t");

    display(top);

    break;

    case 4:printf(\nGOOD BYE!!);getch();

    exit(0);

    break;

    default: printf("\nWrong Choice!!");

    }

    }while(ch>=1&&chdata=d;

    (*T)->next=NULL;

    }

    else

    {

    temp=alloc();

    temp->data=d;temp->next=(*T);

    (*T)=temp;

    }

    }

    int pop(stack**T)

    {

    stack*temp;

    int d;

    if(*T==NULL)

    {printf("\nStack is empty\n");

    return -1;

    }

    d=(*T)->data;

    temp=*T;

    (*T)=(*T)->next;

    free(temp);

  • 7/24/2019 Data Structures File

    13/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 13

    return d;

    }

    void display(stack *S)

    {

    while(S!=NULL)

    {printf("%d->",S->data);

    S=S->next;

    }

    printf("\n");

    }

    OUTPUT1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:1

    Enter element to be inserted:16

    1.Push2.Pop

    3.Display

    4.Exit

    Enter your choice:1

    Enter element to be inserted:08

    1.Push

    2.Pop3.Display

    4.Exit

    Enter your choice:3

    The elements of the stack are

    16->08->

  • 7/24/2019 Data Structures File

    14/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 14

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:2

    The deleted element is 16

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:3

    The elements of the stack are

    08->

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:2

    The deleted element is 08

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter your choice:2

    Stack is empty

    1.Push

    2.Pop

    3.Display

    4.Exit

  • 7/24/2019 Data Structures File

    15/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 15

    Enter your choice:4

    GOOD BYE!!

  • 7/24/2019 Data Structures File

    16/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 16

    PROGRAM 8:

    WAP to evaluate postfix expression.

    #include

    #include

    #include

    #include

    #include

    #define MAX 30

    int stack[MAX/2];

    int top=-1;

    void push(int*,int*,int);

    int pop(int*,int*);

    void eval(char*);void main()

    {

    char expression[20];

    printf("\nEnter the expression:");

    scanf("%s",&expression);

    eval(expression);

    printf("\nThe result is %d",stack[0]);

    getch();

    }

    void eval(char*exp){

    int val1,val2,ans;

    while(*exp)

    {

    if(*exp==' ' || *exp=='\t')

    {

    exp++;

    continue;

    }

    else if(isdigit(*exp))

    push(stack,&top,(*exp)-48);

    else

    {

    val1=pop(stack,&top);

    val2=pop(stack,&top);

    switch(*exp)

    {

  • 7/24/2019 Data Structures File

    17/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 17

    case '+':

    ans=val2+val1;

    break;

    case '-':

    ans=val2-val1;

    break;case '/':

    ans=val2/val1;

    break;

    case '*':

    ans=val2*val1;

    break;

    case '%':

    ans=val2%val1;

    break;

    case '^':

    ans=pow(val2,val1);

    break;

    default:

    printf("\nUnknown Operator");

    exit(0);

    }

    push(stack,&top,ans);

    }

    exp++;

    }}

    void push(int *st,int *sp,int item){

    if(*sp==MAX-1)

    printf("\nStack is full");

    else{

    (*sp)++;

    st[*sp]=item;}}

    int pop(int *st, int *sp)

    {int item;

    if(*sp==-1)

    {printf("\nStack is empty");return -1;}

    else

    {item=st[*sp];

    (*sp)--;

    return item;

    }}

  • 7/24/2019 Data Structures File

    18/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 18

    OUTPUT

    Enter the expression: 234*+52^-

    The result is -11

  • 7/24/2019 Data Structures File

    19/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 19

    PROGRAM 9:

    WAP to convert infix expression to postfix.

    #include

    #include

    #include

    #include

    class expression

    {

    char infix[100];

    char stack[200];

    int top;

    int r;

    char postfix[100];public:

    void convert();

    int input_p(char);

    int stack_p(char);

    int rank(char);

    };

    int expression::input_p(char c)

    {

    if(c== || c==-')

    return 1;else if(c== || c==/')

    return 3;

    else if(c==^')

    return 6;

    else if(isalpha(c)!=0)

    return 7;

    else if(c==()

    return 9;

    else if(c==)')

    return 0;

    else

    {

    cout

  • 7/24/2019 Data Structures File

    20/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 20

    if(c== || c==-')

    return 2;

    else if(c== || c==/')

    return 4;

    else if(c==^')

    return 5;else if(isalpha(c)!=0)

    return 8;

    else if(c==()

    return 0;

    else

    {

    cout

  • 7/24/2019 Data Structures File

    21/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 21

    while(next!=)

    {

    //Pop all the elements to output in stack which have higher precedence

    while( input_p(next) < stack_p(stack[top]) )

    {

    if(top

  • 7/24/2019 Data Structures File

    22/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 22

    OUTPUT

    Enter an infix expression ::

    (a+b^c^d)*(c+d)

    The corresponding postfix expression is ::abcd^^+cd+*

  • 7/24/2019 Data Structures File

    23/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 23

    PROGRAM 10:

    Write a program for checking balanced parenthesis.

    #include

    #include

    #include

    const int maxstack = 100;

    struct stack

    {

    char data[maxstack] ; // array where data is stored

    int stacktop ; // when it is -1, stack is empty

    }s;

    void initstack(stack & s) // empty the stack

    {s.stacktop = -1; // set stacktop to be empty

    }

    bool isEmpty(const stack& s) // returns true or false

    {

    return s.stacktop==-1; // function is true if stacktop has value of -1

    }

    void pop ( stack & s, char & x) // removes the top item

    {

    if (isEmpty(s)) // if stack is empty, cannot remove data

    {cout

  • 7/24/2019 Data Structures File

    24/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 24

    }

    else

    {

    s.stacktop++; // move the top of the stack up

    s.data[s.stacktop] = x; // add new item,x

    }}

    // ************ FUNCTION TOP ****************

    char top(const stack& s)

    //returns the top item from the stack in parameter x

    {

    if (isEmpty(s))

    {

    cout

  • 7/24/2019 Data Structures File

    25/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 25

    if (isEmpty(s))

    error = true;

    else

    pop(s,ch);

    cin.get(ch);

    }if ((!error) && isEmpty(s))

    cout

  • 7/24/2019 Data Structures File

    26/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 26

    PROGRAM 11:

    WAP to implement queue using linked list.

    #include

    #include

    #include

    class cqueue

    {

    int q[5],front,rare;

    public:

    cqueue()

    {

    front=-1;

    rare=-1;}

    void push(int x)

    {

    if(front ==-1 && rare == -1)

    {

    q[++rare]=x;

    front=rare;

    return;

    }

    else if(front == (rare+1)%5 ){

    cout

  • 7/24/2019 Data Structures File

    27/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 27

    return;

    }

    front= (front+1)%5;

    }

    void display()

    {int i;

    if( front

  • 7/24/2019 Data Structures File

    28/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 28

    exit(0);

    }}

    getch();

    return(0);}

    OUTPUT

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter your choice1

    enter element4

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter your choice1

    enter element5

    1.INSERT 2.DELETE 3.DISPLAY 4.EXITEnter your choice1

    enter element3

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter your choice3

    4 5 3

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter your choice2

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter your choice3

    5 3

    1.INSERT 2.DELETE 3.DISPLAY 4.EXIT

    Enter your choice4

  • 7/24/2019 Data Structures File

    29/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 29

    PROGRAM 12:

    WAP to implement queue using array.

    #include

    #include

    #include

    #define MAXSIZE 5

    int cq[MAXSIZE];

    int front,rear;

    void main()

    {

    void add(int,int);

    void del(int);

    int will=1,i,num;front = -1;

    rear = -1;

    clrscr();

    printf("\nProgram for Circular Queue demonstration through array");

    while(1)

    {

    printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");

    printf("\n\nENTER YOUR CHOICE : ");

    scanf("%d",&will);

    switch(will){

    case 1:

    printf("\n\nENTER THE QUEUE ELEMENT : ");

    scanf("%d",&num);

    add(num,MAXSIZE);

    break;

    case 2:

    del(MAXSIZE);

    break;

    case 3:

    exit(0);

    default: printf("\n\nInvalid Choice . ");

    }

    } //end of outer while

    } //end of main

    void add(int item,int MAX)

  • 7/24/2019 Data Structures File

    30/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 30

    {

    //rear++;

    //rear= (rear%MAX);

    if(front ==(rear+1)%MAX)

    {

    printf("\n\nCIRCULAR QUEUE IS OVERFLOW");}

    else

    {

    if(front==-1)

    front=rear=0;

    else

    rear=(rear+1)%MAX;

    cq[rear]=item;

    printf("\n\nRear = %d Front = %d ",rear,front);

    }

    }

    void del(int MAX)

    {

    int a;

    if(front == -1)

    {

    printf("\n\nCIRCULAR QUEUE IS UNDERFLOW");

    }

    else

    {

    a=cq[front];if(front==rear)

    front=rear=-1;

    else

    front = (front+1)%MAX;

    printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",a);

    printf("\n\nRear = %d Front = %d ",rear,front);

    }}

    OUTPUT

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

  • 7/24/2019 Data Structures File

    31/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 31

    ENTER YOUR CHOICE : 1

    ENTER THE QUEUE ELEMENT : 10

    Rear=0 Front=0

    MAIN MENU

    1. INSERTION

    2.DELETION3.EXIT

    ENTER YOUR CHOICE : 1

    ENTER THE QUEUE ELEMENT : 20

    Rear=1 Front=0

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 1

    ENTER THE QUEUE ELEMENT : 30

    Rear=2 Front=0

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 1

    ENTER THE QUEUE ELEMENT : 40

    Rear=3 Front=0

    MAIN MENU

    1. INSERTION

    2.DELETION3.EXIT

    ENTER YOUR CHOICE : 1

    ENTER THE QUEUE ELEMENT : 50

    Rear=4 Front=0

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 1

    ENTER THE QUEUE ELEMENT : 60CIRCULAR QUEUE IS OVERFLOW.

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 2

    DELETED ELEMENT FROM QUEUE IS : 10

  • 7/24/2019 Data Structures File

    32/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 32

    Rear =4 Front=1

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 2DELETED ELEMENT FROM QUEUE IS : 20

    Rear =4 Front=2

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 2

    DELETED ELEMENT FROM QUEUE IS : 30

    Rear =4 Front=3

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 2

    DELETED ELEMENT FROM QUEUE IS : 40

    Rear =4 Front=4

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 2DELETED ELEMENT FROM QUEUE IS : 50

    Rear =-1 Front=-1

    MAIN MENU

    1. INSERTION

    2.DELETION

    3.EXIT

    ENTER YOUR CHOICE : 2

    CIRCULAR QUEUE IS UNDERFLOW

  • 7/24/2019 Data Structures File

    33/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 33

    PROGRAM 13:

    WAP to implement binary search tree.

    #include

    #include

    #include

    void insert(int,int );

    void delte(int);

    void display(int);

    int search(int);

    int search1(int,int);

    int tree[40],t=1,s,x,i;

    main()

    {int ch,y;

    for(i=1;i

  • 7/24/2019 Data Structures File

    34/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 34

    break;

    case 4:

    cout x;

    y=search(1);

    if(y == -1) cout

  • 7/24/2019 Data Structures File

    35/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 35

    delte(2*x);

    }

    t--;

    }

    int search(int s){

    if(t==1)

    {

    cout

  • 7/24/2019 Data Structures File

    36/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 36

    search1(2*s,ch);

    else search1(2*s+1,ch);

    }

    OUTPUT

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXIT

    Enter your choice:3

    no element in tree:

    01234567890111213141516171819202122232425262728293031321.INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXIT

    Enter your choice:1

    Enter the element to insert 10

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXIT

    Enter your choice:4

    Enter the element to search: 10

    10 is in 1 position

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXIT

    Enter your choice:5

  • 7/24/2019 Data Structures File

    37/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 37

    PROGRAM 14:

    WAP to implement linear search array.

    #include < stdio.h>

    #include < conio.h>

    void main()

    {

    int a[10],n,i,item,loc;

    clrscr();

    printf("Enter the size of array: ");

    scanf("%d",&n);

    printf("\nEnter the elements of array:\n");

    for(i=0;i

  • 7/24/2019 Data Structures File

    38/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 38

    67

    23

    12

    45

    4

    6Enter the item to be searched: 12

    Item 12 is present at 7 location in the array

  • 7/24/2019 Data Structures File

    39/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 39

    PROGRAM 15:

    WAP to implement binary search in an array.

    #include

    main()

    {

    int c, first, last, middle, n, search, array[100];

    printf("Enter number of elements\n");

    scanf("%d",&n);

    printf("Enter %d integers\n", n);

    for ( c = 0 ; c < n ; c++ )

    scanf("%d",&array[c]);

    printf("Enter value to find\n");

    scanf("%d",&search);first = 0;

    last = n - 1;

    middle = (first+last)/2;

    while( first last )

    printf("Not found! %d is not present in the list.\n", search);

    return 0; }

    OUTPUT

    Enter number of elements

    10

    Enter 10 integers

    1

    23

  • 7/24/2019 Data Structures File

    40/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 40

    45

    6

    7

    89

    67

    5666

    77

    9

    Enter value to find

    23

    23 found at location 2

  • 7/24/2019 Data Structures File

    41/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 41

    PROGRAM 16:

    WAP to implement linked list.

    #include

    #include

    #include

    void create();

    void insert();

    void delete();

    void display();

    struct node

    {

    int data;

    struct node *link;};

    struct node *first=NULL,*last=NULL,*next,*prev,*cur;

    void create()

    {

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

    printf("\nENTER THE DATA: ");

    scanf("%d",&cur->data);

    cur->link=NULL;

    first=cur;

    last=cur;}

    void insert()

    {

    int pos,c=1;

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

    printf("\nENTER THE DATA: ");

    scanf("%d",&cur->data);

    printf("\nENTER THE POSITION: ");

    scanf("%d",&pos);

    if((pos==1) &&(first!=NULL))

    {

    cur->link = first;

    first=cur;

    }

    else

    {

    next=first;

  • 7/24/2019 Data Structures File

    42/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 42

    while(clink;

    c++;

    }if(prev==NULL)

    {

    printf("\nINVALID POSITION\n");

    }

    else

    {

    cur->link=prev->link;

    prev->link=cur;

    }

    }

    }

    void delete()

    {

    int pos,c=1;

    printf("\nENTER THE POSITION : ");

    scanf("%d",&pos);

    if(first==NULL)

    {

    printf("\nLIST IS EMPTY\n");

    }

    else if(pos==1 && first->link==NULL){

    printf("\n DELETED ELEMENT IS %d\n",first->data);

    free(first);

    first=NULL;

    }

    else if(pos==1 && first->link!=NULL)

    {

    cur=first;

    first=first->link;

    cur->link=NULL;printf("\n DELETED ELEMENT IS %d\n",cur->data);

    free(cur);

    }

    else

    {

    next=first;

    while(c

  • 7/24/2019 Data Structures File

    43/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 43

    {

    cur=next;

    next=next->link;

    c++;

    }

    cur->link=next->link;next->link=NULL;

    if(next==NULL)

    {

    printf("\nINVALID POSITION\n");

    }

    else

    {

    printf("\n DELETED ELEMENT IS %d\n",next->data);

    free(next);

    }

    }

    }

    void display()

    {

    cur=first;

    while(cur!=NULL)

    {

    printf("\n %d",cur->data);

    cur=cur->link;

    }

    }void main()

    {

    int ch;

    clrscr();

    printf("\n\nSINGLY LINKED LIST");

    do

    {

    printf("\n\n1.CREATE\n2.INSERT\n3.DELETE\n4.EXIT");

    printf("\n\nENTER YOUR CHOICE : ");

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

    {

    case 1:

    create();

    display();

    break;

    case 2:

  • 7/24/2019 Data Structures File

    44/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 44

    insert();

    display();

    break;

    case 3:

    delete();

    display();break;

    case 4:

    exit(0);

    default:

    printf("Invalid choice...");

    }

    }while(1);

    getch();

    }

    OUTPUT

    SINGLY LINKED LIST

    1.CREATE

    2.INSERT

    3.DELETE

    4.EXIT

    ENTER YOUR CHOICE : 1

    ENTER THE DATA: 10

    10

    1.CREATE

    2.INSERT

    3.DELETE

    4.EXIT

    ENTER YOUR CHOICE : 2

    ENTER THE DATA: 30

    ENTER THE POSITION: 1

    30

    10

    1.CREATE2.INSERT

    3.DELETE

    4.EXIT

    ENTER YOUR CHOICE : 3

    ENTER THE POSITION : 2

    LIST IS EMPTY

  • 7/24/2019 Data Structures File

    45/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 45

    PROGRAM 17:

    WAP to implement bubble sort.

    #include

    #include

    void bubbleSort(int *ar,int length)//Bubble sort function

    {

    int i,j;

    for(i=0;i

  • 7/24/2019 Data Structures File

    46/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 46

    OUTPUT

    Enter number of elements of an array:

    5

    Enter elements of array:14

    4

    2

    45

    1

    Sorted array is:

    1 2 4 14 45

  • 7/24/2019 Data Structures File

    47/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 47

    PROGRAM 18:

    WAP to implement selection sort.

    #include

    void selsort(int*a,int s)

    {

    Int i,temp,j;

    for(i=0;i

  • 7/24/2019 Data Structures File

    48/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 48

    0

    21

    7

    After sorting is:

    0 4 5 7 21

  • 7/24/2019 Data Structures File

    49/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 49

    PROGRAM 19:

    WAP to implement insertion sort.

    #include

    void insort(int*a,int s)

    {

    Int i,j,temp;

    for(i=1;i

  • 7/24/2019 Data Structures File

    50/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 50

    7

    9

    0

    2

    After sorting:

    0 2 3 7 9

  • 7/24/2019 Data Structures File

    51/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 51

    PROGRAM 20:

    WAP to implement merge sort.

    #include

    #include

    const int size=50;

    void merge(int*a,int*b,int*c,int m,int n)

    {

    int i,j,k;

    for(i=0,j=0;i

  • 7/24/2019 Data Structures File

    52/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 52

    for(i=0;i>b[i];

    }

    k=m+n;

    merge(a,b,c,m,n);cout

  • 7/24/2019 Data Structures File

    53/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 53

    PROGRAM 21:

    WAP to implement shell sort.

    #include

    #include

    #define MAX 10

    void shellsort(int*arr,int n){

    int i,j,temp,increment;

    for(increment=n/2; increment>0; increment /= 2){

    for(i=increment; i=increment; j -= increment){

    if(temp < arr[j-increment])

    arr[j] = arr[j-increment];elsebreak;

    }

    arr[j] = temp;

    }

    }

    }

    void main(){

    clrscr();

    int arr[MAX],n;

    cout

  • 7/24/2019 Data Structures File

    54/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 54

    35

    45

    32

    23

    --Display

    10 23 32 35 45

  • 7/24/2019 Data Structures File

    55/56

    www.mcdtu.wordpress.com

    www.mcdtu.wordpress.com 55

    PROGRAM 22:

    WAP to implement quick sort.

    #include

    void quicksort(int [10],int,int);

    int main(){

    int x[20],size,i;

    printf("Enter size of the array: ");

    scanf("%d",&size);

    printf("Enter %d elements: ",size);

    for(i=0;i

  • 7/24/2019 Data Structures File

    56/56

    www.mcdtu.wordpress.com

    OUTPUT

    Enter size of the array: 5

    Enter 5 elements: 3 8 0 1 2

    Sorted elements: 0 1 2 3 8