Program File C

Embed Size (px)

Citation preview

  • 7/24/2019 Program File C

    1/32

    1. Write a program to search an element using Linear Search.

    /* Linear search */

    #include

    int main()

    {

    int n , ar[10] , i , s , z=0; printf("enter the size of list \n");

    scanf("%d",&n);

    printf("enter in list \n");

    for(i=0;i

  • 7/24/2019 Program File C

    2/32

    2. Write a program to search an element using Binary Search.

    /* Binary search */

    #includeint main()

    {

    int ar[10] , mid , first , last , i , n , s ;

    printf("enter the size of list [MAX 9] \n");

    printf("assuming give input is in ascending order \n");

    scanf("%d",&n);

    printf("enter in list \n");

    for(i=0;i

  • 7/24/2019 Program File C

    3/32

    3. Write a program to sort the given array using Bubble Sort.

    /* Bubble sort */

    #include

    int main()

    {

    int ar[10] , i , n , temp , j ;

    printf("enter the size of the array [MAX 9] \n");

    scanf("%d",&n);

    printf("enter in array \n");

    for(i=0;i

  • 7/24/2019 Program File C

    4/32

    4. Write a program to sort the given array using Selection Sort.

    /* Selection sort */#include int main(){ int array[10], n, c, d, osition, s!a"

    rint($%nter num&er o elements'n$)" scan($d$, n)" rint($%nter in array 'n$)"

    or ( c 0 " c < n " c++ ) scan($d$, array[c])"

    or ( c 0 " c < ( n 1 ) " c++ ) { osition c"

    or ( d c + 1 " d < n " d++ ) { i ( array[osition] > array[d] ) osition d" - i ( osition c ) { s!a array[c]"

    array[c] array[osition]" array[osition] s!a" - -

    rint($Sorted list in ascendin order'n$)"

    or ( c 0 " c < n " c++ ) rint($d'n$, array[c])"

    return 0"

    -

  • 7/24/2019 Program File C

    5/32

    5. Write a program to sort the given array using Insertion Sort.

    /* insertion sort */#include

    int main(){ int n, array[10], c, d, t"

    rint($%nter num&er o elements'n$)" scan($d$, n)"

    rint($%nter in array 'n$)"

    or (c 0" c < n" c++) { scan($d$, array[c])" -

    or (c 1 " c 0 array[d] < array[d1]) { t array[d]" array[d] array[d1]" array[d1] t"

    d" - -

    rint($Sorted list in ascendin order'n$)"

    or (c 0" c

  • 7/24/2019 Program File C

    6/32

    6. Write a program to calculate the factorial of a number usingrecursion.

    /* 5actorial*/#include int sum 1 "int ac(int 6){ i(60) return sum" else { sum sum * 6 " 6 61" ac(6)" --int main(){ int n , , sum1" rint($enter the num&er to ind it7s actorial 'n$)" scan($d$,n)"

    ac(n)" rint($actorial o num&er is d 'n$,)"

    return 0 "-

    23423

  • 7/24/2019 Program File C

    7/32

    7. Write a program to print the Fibonacci Series of n terms usingrecursion.

    /* 5i&onacci series */#include8oid i&(int n){

    static lon int irst0,second1,sum"

    i(n>0) { sum irst + second" irst second" second sum" rint($ld 'n$,sum)" i&(n1)" --int main(){

    int 9,n" lon int i0,:1,"

    rint($%nter the rane o the 5i&onacci series 'n $)" scan($d$,n)"

    rint($5i&onacci Series 'n$)" rint($d d $,0,1)" i&(n)" return 0"-

    23423

  • 7/24/2019 Program File C

    8/32

    8. Write a program to sort the given array using QuickSort.

    /* ;uic9sort */#include8oid uic9sort(int 6[10],int irst,int last){ int i8ot,:,tem,i"

    i(irst6[i8ot]) :" i(i

  • 7/24/2019 Program File C

    9/32

    rint($%nter sie o the array $)" scan($d$,sie)"

    rint($%nter d elements $,sie)" or(i0"i

  • 7/24/2019 Program File C

    10/32

    artition(arr,mid+1,hih)" mereSort(arr,lo!,mid,hih)" --

    8oid mereSort(int arr[],int lo!,int mid,int hih){

    int i,m,9,l,tem[@0]"

    llo!" ilo!" mmid+1"

    !hile((l

  • 7/24/2019 Program File C

    11/32

    "#. Write a program to insert a ne$ element in the given unsorte%array at kth position.

    /* insert element at unsorted osition */#include

    8oid insertEunsorted()"int a[@0], n, loc, item"

    main(){

    int i"

    rint($ %nter sie o an array $)"scan($d$, n)"

    rint($ %nter elements o an array $)"

    or(i0" i

  • 7/24/2019 Program File C

    12/32

    insertEunsorted()"

    rint($ Ater insertion $)"

    or(i0" i

  • 7/24/2019 Program File C

    13/32

    array[c] array[c+1]"

    rint($Hesultant array is'n$)"

    or( c 0 " c < n 1 " c++ ) rint($d'n$, array[c])" -

    return 0"-

    23423

    "&. Write a program to merge to given sorte% arrays.

    /* merer i8en sorted arrays */#include 8oid mere(int [], int, int [], int, int [])"int main() { int a[10], &[10], m, n, c, sorted[=0]"

    rint($Fnut num&er o elements in irst array'n$)" scan($d$, m)"

    rint($enter in array'n$)" or (c 0" c < m" c++) { scan($d$, a[c])" -

    rint($Fnut num&er o elements in second array'n$)" scan($d$, n)"

    rint($enter in =nd array 'n$)" or (c 0" c < n" c++) { scan($d$, &[c])"

  • 7/24/2019 Program File C

    14/32

    -

    mere(a, m, &, n, sorted)"

    rint($mered array'n$)"

    or (c 0" c < m + n" c++) {

    rint($d'n$, sorted[c])" -

    return 0"-8oid mere(int a[], int m, int &[], int n, int sorted[]) { int i, :, 9"

    : 9 0"

    or (i 0" i < m + n") {

    i (: < m 9 < n) { i (a[:] < &[9]) { sorted[i] a[:]" :++" - else { sorted[i] &[9]" 9++" - i++" - else i (: m) { or (" i < m + n") { sorted[i] &[9]" 9++" i++" - - else { or (" i < m + n") { sorted[i] a[:]" :++" i++"

    - - --

    23423

  • 7/24/2019 Program File C

    15/32

    "'. Write a program to implement Stack using array( also sho$overflo$ an% un%erflo$ in respective push an% pop operations.

    /* stac9 imlementation usin arrays */#include #includeint to, status"8oid ush (int stac9[], int item){ i (to (101))

    status 0" else { status 1"

    ++to"stac9 [to] item"

    -

  • 7/24/2019 Program File C

    16/32

    -

    int o (int stac9[]){ int ret" i (to 1) { ret 0"

    status 0" - else { status 1"

    ret stac9 [to]"to"

    -return ret"-

    8oid dislay (int stac9[])

    { int i" rint ($'n3he Stac9 is $)" i (to 1)

    rint ($emty$)" else { or (ito" i>0" i)

    rint ($'n'nIJd I'n$,stac9[i])" - rint ($'n$)"-

    int main()

    { int stac9 [10], item" int ch" to 1"

    do { do { rint($'n K?AFK ?%K2$)"

    rint ($'n1.42SL (Fnsert) in the Stac9$)" rint ($'n=.44 (Gelete) rom the Stac9$)" rint ($'nJ.%6it (%nd the %6ecution)$)"

    rint ($'n%nter Mour Nhoice $)" scan ($d$, ch)" i (chJ) rint ($'nFn8alid Nhoice, 4lease try aain$)"-!hile (chJ)"

    s!itch (ch) {case 1

    rint ($'n%nter the %lement to &e ushed $)"scan ($d$, item)"rint ($ d$, item)"ush (stac9, item)"i (status)

    { rint ($'nAter 4ushin $)" dislay (stac9)" i (to (101))

  • 7/24/2019 Program File C

    17/32

    rint ($'n3he Stac9 is 5ull$)"-else rint ($'nStac9 o8erlo! on 4ush$)"&rea9"

    case =item o (stac9)"i (status){ rint($'n3he 4oed item is d. Ater 4oin $,item)" dislay (stac9)"-else rint ($'nStac9 underlo! on 4o$)"&rea9"

    deaultrint ($'n%KG 5 %B%N23FK$)"

    - -!hile (ch J)"

    -23423

    "). Write a program to implement Queue using array( $hich sho$sinsertion an% %eletion operations.

    /* imlement ueue usin arrays */#includeint ueue[10],ront1,rear1"8oid insertEelement()"8oid deleteEelement()"8oid dislayEueue()"int main()

    { int otion"

  • 7/24/2019 Program File C

    18/32

    do { rint($'n 1.Fnsert an element$)" rint($'n =.Gelete an element$)" rint($'n J.Gislay ueue$)" rint($'n C.%6it$)" rint($'n %nter your choice $)" scan($d$,otion)" s!itch(otion) { case 1 insertEelement()" &rea9" case = deleteEelement()" &rea9" case J dislayEueue()" &rea9" case C return 0" -

    -!hile(otionC)"-8oid insertEelement(){ int num" rint($'n %nter the num&er to &e inserted $)" scan($d$,num)" i(ront0 rear101) rint($'n ;ueue 8er5lo! ccured$)" else i(ront1rear1) {

    rontrear0" ueue[rear]num"

    - else i(rear101 ront0) { rear0" ueue[rear]num" - else {

    rear++" ueue[rear]num" --8oid deleteEelement(){ int element" i(ront1) { rint($'n 2nderlo!$)" - elementueue[ront]"

    i(rontrear) rontrear1" else

  • 7/24/2019 Program File C

    19/32

    { i(ront101) ront0" else ront++" rint($'n 3he deleted element is d$,element)" -

    -8oid dislayEueue(){ int i" i(ront1) rint($'n Ko elements to dislay$)" else { rint($'n 3he ueue elements are'n $)" or(iront"i

  • 7/24/2019 Program File C

    20/32

    int ch" 8oid insert()" 8oid delet()" 8oid dislay()" rint($'nNircular ;ueue oerations'n$)" rint($1.insert'n=.delete'nJ.dislay'nC.e6it'n$)" !hile(1) { rint($%nter your choice$)" scan($d$,ch)" s!itch(ch) { case 1 insert()" &rea9" case = delet()" &rea9" case Jdislay()" &rea9"

    case C &rea9" deaultrint($Fn8alid otion'n$)" - --

    8oid insert(){ int 6" i((ront0rearma61)II(ront>0rearront1)) rint($;ueue is o8erlo!'n$)" else

    { rint($%nter element to &e insert$)" scan($d$,6)" i(rearma61ront>0) { rear0" [rear]6" - else { i((ront0rear1)II(rearront1))

    [++rear]6" - --8oid delet(){ int a" i((ront0)(rear1)) { rint($;ueue is underlo!'n$)" 8oid e6it()" -

    i(rontrear) { a[ront]"

  • 7/24/2019 Program File C

    21/32

    rear1" ront0" - else i(rontma61) { a[ront]" ront0" - else a[ront++]" rint($Geleted element isd'n$,a)"-

    8oid dislay(){ int i,:" i(ront0rear1) {

    rint($;ueue is underlo!'n$)"

    8oid e6it()" - i(ront>rear) { or(i0"i

  • 7/24/2019 Program File C

    22/32

    #include#includestruct node { int data " struct node * ne6t"

    -"struct node* head "int count 1"8oid insert(int data , int n ){

    struct node * tem1 (struct node *)malloc(sieo(struct node))" tem1>data data" tem1>ne6t K2OO" i(n1) {

    tem1>ne6t head" head tem1" return" - else i(n>count II n1) { ++count" struct node * tem= " int i" or(i0"ine6t tem=>ne6t" tem=>ne6t tem1" - else{ rint($FKPAOFG 4S 'n$)" --8oid rint(){ struct node * tem head"

    !hile(temK2OO) { rint($'t > d 'n$, tem>data)" tem tem>ne6t" - return"-8oid del(int del){ struct node * tem1 head " int i " or(i0"i

  • 7/24/2019 Program File C

    23/32

    struct node* tem= tem1>ne6t" tem1>ne6t tem=>ne6t" ree(tem=)"

    return"-int re8erse(){ struct node *current , * re8 , *ne6t" current head" re8 K2OO " !hile(currentK2OO) { ne6t current>ne6t" current>ne6t re8" re8 current" current ne6t" head re8"

    return head" --int main(){ head K2OO" int choice 0 , data , os " !hile(choice@) {

  • 7/24/2019 Program File C

    24/32

    rint($ 't?AFK ?%K2 'n $)" rint($ 't1. FKS%H3 'n$)" rint($ 't=. 4HFK3 'n$)" rint($ 'tJ. G%O%3% 'n$)" rint($ 'tC. H%P%HS% 'n$)" rint($ 't@. %BF3 'n$)" scan($d$, choice)" s!itch(choice){ case 1 rint($enter the data and os'n$)" scan($d d$ , data , os)" insert(data,os)" &rea9" case = rint()" &rea9" case J rint($enter the os you !ant to

    delete 'n$)" scan($d$,os)" del(os)" &rea9" case C head re8erse()" &rea9" case @ &rea9" deault rint($FKPAOFG NLFN% 'n$)" - - return 0 "-

    "7. Write a program to implement Stack(using +inke% +ist. Implement ,ush( ,opan% %isplay operations.

    /* Stac9 imlementation usin lin9ed list */#include #include struct Kode{ int Gata" struct Kode *ne6t"

    -*to"

    8oid oStac9(){ struct Kode *tem, *8arto" i(8arto) { to to>ne6t" ree(8ar)" - else rint($'nStac9 %mty$)"

    -

    8oid ush(int 8alue)

  • 7/24/2019 Program File C

    25/32

  • 7/24/2019 Program File C

    26/32

    case = { oStac9()" dislay()" &rea9" - case J { dislay()" &rea9" - case C { struct Kode *tem" !hile(toK2OO) { tem to>ne6t" ree(to)"

    totem" - e6it(0)" - deault { rint($'n!ron choice oroeration$)" - - --

    23423

    "8. Write a program to implementQueue( using +inke% +ist. Implement Insertion( %eletion an%%isplay operations.

    /* ;ueue imlementation usin lin9ed list */#include

    #includestruct Kode{ int Gata" struct Kode* ne6t"-*rear, *ront"

    8oid del;ueue(){ struct Kode *tem, *8arrear" i(8arrear) {

    rear rear>ne6t" ree(8ar)" -

  • 7/24/2019 Program File C

    27/32

    else rint($'n;ueue %mty$)"-

    8oid ush(int 8alue){ struct Kode *tem" tem(struct Kode *)malloc(sieo(struct Kode))" tem>Gata8alue" i (ront K2OO) { ronttem" ront>ne6tK2OO" rearront" - else { ront>ne6ttem"

    ronttem" ront>ne6tK2OO" --

    8oid dislay(){ struct Kode *8arrear" i(8arK2OO) { rint($'n%lements are as $)" !hile(8arK2OO)

    { rint($'td$,8ar>Gata)" 8ar8ar>ne6t" - rint($'n$)" - else rint($'n;ueue is %mty$)"-

    int main()

    { int i0" rontK2OO" rint($ 'n1. 4ush to ;ueue$)" rint($ 'n=. 4o rom ;ueue$)" rint($ 'nJ. Gislay Gata o ;ueue$)" rint($ 'nC. %6it'n$)" !hile(1) { rint($ 'nNhoose tion $)" scan($d$,i)" s!itch(i)

    { case 1 {

  • 7/24/2019 Program File C

    28/32

    int 8alue" rint($'n%nter a 8alue&er to ush into ;ueue $)" scan($d$,8alue)" ush(8alue)" dislay()" &rea9" - case = { del;ueue()" dislay()" &rea9" - case J { dislay()" &rea9" -

    case C { e6it(0)" - deault { rint($'n!ronchoice or oeration$)" - - --

    23423

    ". Write a program to count the number of times an item ispresent in a linke% list.

    /* count occurrences in a lin9ed list */#include#includestruct node{ int data" struct node* ne6t"-"

    8oid ush(struct node** headEre, int ne!Edata){

    struct node* ne!Enode (struct node*) malloc(sieo(struct node))"

    ne!Enode>data ne!Edata"

  • 7/24/2019 Program File C

    29/32

    ne!Enode>ne6t (*headEre)"

    (*headEre) ne!Enode"-

    int count(struct node* head, int searchEor){ struct node* current head" int count 0" !hile (current K2OO) { i (current>data searchEor) count++" current current>ne6t" - return count"-

    int main(){ struct node* head K2OO"

    ush(head, 1)" ush(head, J)" ush(head, 1)" ush(head, =)" ush(head, 1)"

    rint($count o 1 is d$, count(head, 1))"

    return 0"-

    23423

    . Write a program to increment the %ata part of every no%epresent in a linke% list by "#.

    /* Fncrement data &y 10 */#include#includestruct node { int data " struct node * ne6t"

    -"struct node* head "

    int count 1"8oid insert(int data , int n ){

  • 7/24/2019 Program File C

    30/32

    struct node * tem1 (struct node *)malloc(sieo(struct node))" tem1>data data" tem1>ne6t K2OO" i(n1) { tem1>ne6t head" head tem1" return" - else i(n>count II n1) { ++count" struct node * tem= " int i" or(i0"ine6t tem=>ne6t" tem=>ne6t tem1" - else{ rint($FKPAOFG 4S 'n$)" --8oid rint(){ struct node * tem head" !hile(temK2OO)

    { rint($'t > d 'n$, tem>data)" tem tem>ne6t" - return"-8oid del(int del){ struct node * tem1 head " int i " or(i0"ine6t" tem1>ne6t tem=>ne6t" ree(tem=)"

    return"-

    int increment(){

    struct node * tem head" !hile(temK2OO) {

  • 7/24/2019 Program File C

    31/32

  • 7/24/2019 Program File C

    32/32

    -"

    8oid ush(struct node** headEre, int ne!Edata){ struct node* ne!Enode (struct node*) malloc(sieo(struct node))"

    ne!Enode>data ne!Edata"

    ne!Enode>ne6t (*headEre)"

    (*headEre) ne!Enode"-

    int count(struct node* head, int searchEor){ struct node* current head" int count 0" !hile (current K2OO)

    { i (current>data searchEor) count++" current current>ne6t" - return count"-

    int main(){ struct node* head K2OO"

    ush(head, 1)" ush(head, J)" ush(head, 1)" ush(head, =)" ush(head, 1)"

    rint($count o 1 is d$, count(head, 1))" return 0"-

    23423