You are here:

C/Stack Operations

Advertisement


Question
QUESTION: Write a program that reverses the string contents of a stack (the top and bottom elements exchange positions, the second last element exchange positions, and so forth until the entire stack is reversed). Hint: You may use a second stack. The following diagram illustrates the exchange process.

A   I
B   H
C   G
D   F
E   E
F   D
G   C
H   B
I   A

this id the code i write to read an integer and put in the stack. then reverce the stack.
can you tell me how to read a string and store the string in a stack and then reverce it.
thank you.




#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
/* self-referential structure */
struct stackNode {
  int data;          /* define data as an int */
  struct stackNode *nextPtr; /* stackNode pointer */
}; /* end structure stackNode */

struct stackNode1 {
  int data;          /* define dat1a as an int */
  struct stackNode1 *nextPtr; /* stackNode1 pointer */
}; /* end structure stackNode1 */

typedef struct stackNode StackNode; /* synonym for struct stackNode */
typedef StackNode *StackNodePtr; /* synonym for StackNode* */

typedef struct stackNode1 StackNode1; /* synonym for struct stackNode */
typedef StackNode1 *StackNode1Ptr; /* synonym for StackNode* */

/* prototypes */
void push( StackNodePtr *topPtr, int info );
void push1( StackNode1Ptr *topPtr, int info );
int pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr currentPtr );
void printStack1( StackNode1Ptr current1Ptr );

int i,counter=0;
/* function main begins program execution */
int main( void )
{



  StackNodePtr stackPtr = NULL; /* points to stack top */
  StackNode1Ptr stack1Ptr = NULL; /* points to stack top */

  //StackNodePtr stackPtr = NULL; /* points to stack top */

  char value[15];  /* int input by user */

  clrscr();  /*Clear screen*/

  for (i=0;i<10;i++)
   {
      printf( "Enter [%2d] integer: " ,i);
      scanf("%d",&value);
      push( &stackPtr, value );
      printStack( stackPtr );
      counter+=1;
      printf("counter : %d\n",counter);
    }

  clrscr();
  for(i=0;i<counter;i++)
  {
     printStack( stackPtr );
  }
  getch();

  clrscr();
  for(i=0;i<counter;i++)
  {
  push1( &stack1Ptr, pop( &stackPtr ) );
     printStack1( stack1Ptr );
  }
  getch();
  return 0; /* indicates successful termination */

} /* end main */

/* Insert a node at the stack top */
void push( StackNodePtr *topPtr, int info )
{
  StackNodePtr newPtr; /* pointer to new node */

  newPtr = malloc( sizeof( StackNode ) );

  /* insert the node at stack top */
  if ( newPtr != NULL ) {
     newPtr->data = info;
     newPtr->nextPtr = *topPtr;
     *topPtr = newPtr;
  } /* end if */
  else { /* no space available */
     printf( "%d not inserted. No memory available.\n", info );
  } /* end else */

} /* end function push */





void push1( StackNode1Ptr *topPtr, int info )
{
  StackNode1Ptr newPtr; /* pointer to new node */

  newPtr = malloc( sizeof( StackNode1 ) );

  /* insert the node at stack top */
  if ( newPtr != NULL ) {
     newPtr->data = info;
     newPtr->nextPtr = *topPtr;
     *topPtr = newPtr;
  } /* end if */
  else { /* no space available */
     printf( "%s not inserted. No memory available.\n", info );
  } /* end else */

} /* end function push */

/* Remove a node from the stack top */
int pop( StackNodePtr *topPtr )
{
  StackNodePtr tempPtr; /* temporary node pointer */
  int popValue; /* node value */

  tempPtr = *topPtr;
  popValue = ( *topPtr )->data;
  *topPtr = ( *topPtr )->nextPtr;
  free( tempPtr );

  return popValue;

} /* end function pop */

/* Print the stack */
void printStack( StackNodePtr currentPtr )
{

  /* if stack is empty */
  if ( currentPtr == NULL ) {
     printf( "The stack is empty.\n\n" );
  } /* end if */
  else {
     printf( "The stack is:\n" );

     /* while not the end of the stack */
     while ( currentPtr != NULL ) {
   printf( "%s --> ", currentPtr->data );
   currentPtr = currentPtr->nextPtr;
     } /* end while */

     printf( "NULL\n" );
  } /* end else */

} /* end function printList */


void printStack1( StackNode1Ptr current1Ptr )
{

  /* if stack is empty */
  if ( current1Ptr == NULL ) {
     printf( "The stack is empty.\n\n" );
  } /* end if */
  else {
     printf( "The reversed stack is:\n" );

     /* while not the end of the stack */
     while ( current1Ptr != NULL ) {
   printf( "%s --> ", current1Ptr->data );
   current1Ptr = current1Ptr->nextPtr;
     } /* end while */

     printf( "NULL\n" );
  } /* end else */

} /* end function printList */




/* Return 1 if the stack is empty, 0 otherwise */
int isEmpty( StackNodePtr topPtr )
{
  return topPtr == NULL;

} /* end function isEmpty */




ANSWER: Hi dear Canno!

Need not to write code this much complex ,earlier  ive answered  a question similar to this please refer this URL http://en.allexperts.com/q/C-1587/2009/10/Reverses-string-contents-using.htm

but as you mentioned if you require push and pop operation as we do in stack refer the following code ( simple version ) ive written for you , modify this simple prototype as per your need

#include<stdio.h>
#define MAX_STK 20 /* define the maximum stack size */

int iStkPtr = -1;
char cStack[MAX_STK];
void Push(char ch)
{
  if(iStkPtr == MAX_STK)
  {
     printf("Error: Stack Full ..\n");
     return;
  }
  cStack[++iStkPtr] = ch;
}

char Pop(char ch)
{
  if(iStkPtr == -1)
  {
     printf("Error Stack is empty\n");
     return -1;
  }
  return cStack[iStkPtr--];
}

void Disp()
{
  int i;
  if(iStkPtr == -1)
  {
     printf("Error Stack is empty\n");
     return;
  }
  for( i = iStkPtr ; i >= 0  ; i--)
     printf("%c\n",cStack[i]);
}

void main()
{
  Push('a');
  Push('b');
  Push('c');
  Push('d');
  Push('e');

  printf("Stack contents\n");
  Disp();
  getch();
}


Note: i haven't tested this code , please get back to me in case of bugs and further assistance.

All the best
Prince M. Premnath

---------- FOLLOW-UP ----------

QUESTION: Dear Prince M. Premnath,

thanks a lot for answering my question.

i done a programme with using stack.
now the problem i can use the function pop or the pop function is not working or retuen what i want.
can you please check the code for me?
thank.
Hope you can answer me asap.
Thank You so much.

the code is:

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
/* self-referential structure */
struct stackNode {
  char* data;          /* define data as a string */
  struct stackNode *nextPtr; /* stackNode pointer */
}; /* end structure stackNode */



typedef struct stackNode StackNode; /* synonym for struct stackNode */
typedef StackNode *StackNodePtr; /* synonym for StackNode* */


/* prototypes */
void push( StackNodePtr *topPtr, char* info );

char* pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr currentPtr );

int inputString(char* dest, int space);
int i,counter=0;
/* function main begins program execution */
int main( void )
{



  StackNodePtr stackPtr = NULL; /* points to stack top */



 char value[16][15];

  clrscr();  /*Clear screen*/
  for(i = 0; i < 10;i++)
  {

      printf( "Enter [%2d] string: " ,i+1);
      gets(value[i]);
      push( &stackPtr, value[i] );
      printStack( stackPtr );
  }


  getch();
  clrscr();

  printStack( stackPtr );
  printf("\n %s ",pop(stackPtr));
  getch();
  return 0; /* indicates successful termination */

} /* end main */

/* Insert a node at the stack top */
void push( StackNodePtr *topPtr, char* info )
{
  StackNodePtr newPtr; /* pointer to new node */

  newPtr = malloc( sizeof( StackNode ) );

  /* insert the node at stack top */
  if ( newPtr != NULL ) {
     newPtr->data = info;
     newPtr->nextPtr = *topPtr;
     *topPtr = newPtr;
  } /* end if */
  else { /* no space available */
     printf( "%s not inserted. No memory available.\n", info );
  } /* end else */

} /* end function push */


/* Remove a node from the stack top */
char* pop( StackNodePtr *topPtr )
{
  StackNodePtr tempPtr; /* temporary node pointer */
  char* popValue; /* node value */

  tempPtr = *topPtr;
  popValue = ( *topPtr )->data;
  *topPtr = ( *topPtr )->nextPtr;
  free( tempPtr );

  return (popValue);

} /* end function pop */

/* Print the stack */
void printStack( StackNodePtr currentPtr )
{

  /* if stack is empty */
  if ( currentPtr == NULL ) {
     printf( "The stack is empty.\n\n" );
  } /* end if */
  else {
     printf( "The stack is:\n" );

     /* while not the end of the stack */
     while ( currentPtr != NULL ) {
    printf( "%s --> ", currentPtr->data );
    currentPtr = currentPtr->nextPtr;
     } /* end while */

     printf( "NULL\n" );
  } /* end else */

} /* end function printList */


/* Return 1 if the stack is empty, 0 otherwise */
int isEmpty( StackNodePtr topPtr )
{
  return topPtr == NULL;

} /* end function isEmpty */


Answer
Hi dear canno !

I really really sorry for the delay in responding you :( just got committed with some project delivery , well here is your solution

replace the code of your pop() function using this

char* POP(StackNodePtr ptr)
{
  StackNodePtr temp;
  char* popValue;
  temp = ptr;
  popValue = temp->data;
  ptr = ptr->nextPtr;
  free(temp);
  return popValue;
}

and the rest going to be the same , while calling the function  printf("%s",POP(stackPtr)) will return the top element of the stack and update the stack pointer as well

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.