top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a C program to reverse a stack using recursion?

+1 vote
732 views
Write a C program to reverse a stack using recursion?
posted May 16, 2017 by Pooja Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
#include<stdio.h>
#include<stdlib.h>

// gets  value and returns it that is recursion,functions that do only that (i think)
int recursion(int br)
{
    return br;
}

int main(void)
{
    int *p;
    int size,size_cpy;
    int i=0;
    int temp;

    printf("haw much stack would you like to allocate");
    scanf("%d",&size);
    size_cpy = size;

    //acllocate block of memory 
    p = (int*)malloc(size*sizeof(int));

    //fill array you can use scanf i am  yust using i+1 variable
    while(i != size)
    {
        i[p] = (i+1);
        i++;
    }

    i = 0;size-=1;     //set i to 0 and size-1 becose you are imitating indexs
    while(i != size)    ////se loop to true(infinite loop)
    {
        //make sure bouth pointers get same block at one time if the number of bloks is odd
        //becose :Access violation memory
        if(i != size && i <= (size_cpy/2))
        {
            temp = size[p];
            size[p] = recursion(i[p]);
            i[p] = recursion(temp);
        }
        else
        {
            break;
        }
        i++;
        size--;
    }

    // set i to 0 and print elemenets 
    i=0;
    while(i != size_cpy)
    {
        printf("%d\n",i[p]);
        i++;
    }

    //this will relase stack
    free(p);

    // this is for stoping program
    getchar();
    getchar();
    return 0;
}
answer May 18, 2017 by Leon Martinović
...