🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Zeynep HABER / VERİYAPILARI / YIĞIN

1-) VERİYAPILARI - YIĞIN

 

Kural: son giren ilk çıkar

 

KOD:

# include<stdio.h>

typedef struct yigin

{

int deger;

yigin* sonraki;

}

ygn;

ygn* top,*temp;

// YIĞININ DOLULUK KONTROLÜ

bool bosMu()

{

if (top == NULL) return true;

else return false;

}

// EKLEME

void ekle(int sayi)

{

if (bosMu())

{

top = temp = new ygn;

temp->deger = sayi;

temp->sonraki = NULL;

}

else

{

temp = new ygn;

temp->deger = sayi;

temp->sonraki = top;

top = temp;

}

}

// SON GİREN ELEMANI ÇIKARMA

int getir()

{

int deger;

temp = top;

if (!bosMu())

{

deger = temp->deger;

top = top->sonraki;

delete temp;

return deger;

}

return -1;

}

int main()

{

top = NULL;

for (int i = 0; i < 10; i++)

{

ekle(i);

}

while (1 == 1)

{

int deger = getir();

if (deger == -1) break;

else

{

printf("%d /", deger);

}

}

}

 

ÖRNEK 1: Desimal’den Binary’e Dönüşüm

#include<stdio.h>

#include<stdlib.h>

#define MAX 50

 

int isEmpty(int top, int stack_arr[]);

void push(int x, int* top, int stack_arr[]);

int pop(int* top, int stack_arr[]);

void DecToBin(int num);

 

int main()

{

    int num;

    printf("Enter an integer : ");

    scanf("%d", &num);

    printf("Binary Equivalent is : ");

    DecToBin(num);

 

    return 0;

 

}/*End of main()*/

 

void DecToBin(int num)

{

    int stack[MAX], top = -1, rem;

    while (num != 0)

    {

        rem = num % 2;

        push(rem, &top, stack);

        num /= 2;

    }

    while (top != -1)

        printf("%d", pop(&top, stack));

    printf("\n");

}

 

 

void push(int x, int* top, int stack_arr[])

{

    if (*top == (MAX - 1))

        printf("Stack Overflow\n");

    else

    {

        *top = *top + 1;

        stack_arr[*top] = x;

    }

}/*End of push()*/

 

int pop(int* top, int stack_arr[])

{

    int x;

    if (*top == -1)

    {

        printf("Stack Underflow\n");

        exit(1);

    }

    else

    {

        x = stack_arr[*top];

        *top = *top - 1;

    }

    return x;

}/*End of pop()*/

 2022 Mart 08 Salı
 346