facebook like button

30 July, 2011

program 68: stack implementation of array

The need: 
   This is a program to reverse the string using stack. Though for reversing a string usually not done using stack, still this is a basic program to implement stack on an array.


The code:
----------------------------------------------------------

#define max 20
#include<stdio.h>
#include<string.h>
struct STACK
{
    int top;
    int str[max];
};
typedef struct STACK stack;
void push(stack *p,int x);
int pop(stack *p);
main()
{
    int i,j,s1[10],s2[10];
    stack st;
    st.top=-1;
    printf("Enter 10 numbers .");
    for(i=0;i<10;i++)
        scanf("%d",&s1[i]);
    printf("The string is :\n");
    for(i=0;i<10;i++)
    printf("%d ",s1[i]);
    for(i=0;i<10;i++)
        push(&st,s1[i]);
    for(i=0;i<10;i++)
        s2[i]=pop(&st);
    printf("\nThe reversed string is :\n");
    for(i=0;i<10;i++)
        printf("%d ",s2[i]);
}

void push(stack *p,int x)
{
    (p->top)++;
    p->str[p->top]=x;
}

int pop(stack *p)
{
    if(p->top=='\0')
    return (0);
    else
    return(p->str[p->top--]);
}

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

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply