Reverse a word using stack


 #include<conio.h>
#include<stdio.h>



typedef struct list
{char data ;
struct list *next;
}node;


                                                                                        INPUT:      abcd
                                                                                       
                                                                                                    OUTPUT: dcba
      node*top=NULL;
      node*push(node*,char);
      node*display(node*);

void main()
{

  int option ;
  char val;
  clrscr();
  do{
  printf("\n main menu");
  printf("\n1:PUSH");
  printf("\n2:DISPLAY");
  printf("\n3:EXIT");


  printf("\nenter your option:   ");
  scanf("%d",&option);


  switch(option)
  {
      case 1: printf("\n enter value to insert in stack\n");
      scanf("%s",&val);
       top=push(top,val);
     break;

     case 2:top=display(top);
     break;
    case 3:exit(0);
    default :
    printf("not a correct option") ;
}
}
while(option!=3);
 getch();

 }




    node*push(node* top,char val)
    {
    node *ptr;

    ptr=(node*)malloc(sizeof(node));
    ptr->data=val;

    if(top==NULL)

    {
    ptr->next=NULL;
    top=ptr;

    }

       else
    {
    ptr->next=top;
    top=ptr;
    }
    return(top) ;
    }




    node *display(node*top)

    {
    node*ptr;
    ptr=top;

    if(top==NULL)
    {
    printf("stack is empty");
    exit(0);
    }

     else
     {
     while(ptr!=NULL)
     {
     printf("%c",ptr->data);
     ptr=ptr->next;
     }
     }
     return(top);

        }

      




Post a Comment

Previous Post Next Post