problem with stacks -in C

Reply

Join Date: Jan 2009
Posts: 29
Reputation: abby2589 is an unknown quantity at this point 
Solved Threads: 0
abby2589's Avatar
abby2589 abby2589 is offline Offline
Light Poster

problem with stacks -in C

 
0
  #1
Feb 26th, 2009
how to use push and pop here???

thanks!

  
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void push(char palindrome[20], int *top, int value)
  5. {
  6. if(*top<=10)
  7. {
  8. palindrome[*top]=value;
  9. *top++;
  10. }
  11. else
  12. {
  13. printf("Stack is full!!");
  14. exit(0);
  15. }
  16.  
  17. }
  18.  
  19. int value;
  20. void pop(char palindrome[10], int *top)
  21. {
  22. if(*top>=0)
  23. {
  24. value=palindrome[*top];
  25. *top=*top-1;
  26. }
  27. else
  28. {
  29. printf("The stack is empty!!\n");
  30. }
  31. }
  32.  
  33. char palindrome[10];
  34. int top = -1;
  35. int value;
  36. int x;
  37.  
  38. void main()
  39. {
  40. clrscr();
  41. printf("Enter word: ");
  42. scanf("%s",&value);
  43.  
  44. for(top=0;palindrome[top]!=x;top++)
  45. {
  46. }
  47. top--;
  48.  
  49. for(value=0;palindrome[value]!=x;value++,top--)
  50. {
  51. if(palindrome[top]!=palindrome[value])
  52. x=1;
  53. }
  54. if(x==0)
  55. {
  56. printf("The word you've entered is a palindrome!!\n");
  57. }
  58. else
  59. {
  60. printf("The word you've entered is not a palindrome!!\n");
  61. }
  62. getch();
  63. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,421
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: problem with stacks -in C

 
0
  #2
Feb 26th, 2009
Perhaps be more specific... what exactly is it that you want to know?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 29
Reputation: abby2589 is an unknown quantity at this point 
Solved Threads: 0
abby2589's Avatar
abby2589 abby2589 is offline Offline
Light Poster

Re: problem with stacks -in C

 
0
  #3
Feb 26th, 2009
i want to know how to use stacks in this program
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: problem with stacks -in C

 
0
  #4
Feb 26th, 2009
You never initialized the value of x. When is the loop supposed to stop? Stopping at the null character '\0' might be a good idea... Also, you can't have both the loop and and function keeping track of top. You can use a while loop to go through the string and push will modify top for you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: problem with stacks -in C

 
0
  #5
Feb 27th, 2009
Look abby2589 ,

I didn't read all your code but from the general idea of the code i assume that you want to know how to use the stack to check if the word is palindrome or not.

- After you enter the string you will push all of its element from the first character till the end of the string (Using a Pointer ofcourse till the Pointer Reaches NULL).

-Then Pop all the elements from the stack and recieve them in another string.

-And compare the newly created string to the original string that been entered at the first of your program.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC