943,714 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 723
  • C RSS
Feb 26th, 2009
0

problem with stacks -in C

Expand Post »
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. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
abby2589 is offline Offline
33 posts
since Jan 2009
Feb 26th, 2009
0

Re: problem with stacks -in C

Perhaps be more specific... what exactly is it that you want to know?
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Feb 26th, 2009
0

Re: problem with stacks -in C

i want to know how to use stacks in this program
Reputation Points: 8
Solved Threads: 0
Light Poster
abby2589 is offline Offline
33 posts
since Jan 2009
Feb 26th, 2009
0

Re: problem with stacks -in C

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Feb 27th, 2009
0

Re: problem with stacks -in C

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.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Mapping Sonar Data with C
Next Thread in C Forum Timeline: C Compiler Creation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC