Data Structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ch13f
    New Member
    • Jul 2007
    • 5

    #1

    Data Structure

    Kindly help me with these problems ..
    *sigh* I'm having a hard time for this ..

    C Programming please ..

    Problem 1 : Create a program to test the package for the ADT stack
    INPUT : Commands entered by the user
    OUTPUT : List of commands and messages describing the effects of each command.

    SAMPLE OUTPUT :
    Use the ff. commands to test the ADT Stack .
    C - Create a stack
    D - Dump the stack
    E - Test whether the stack is empty
    F - Check if operation failed ( show StackError )
    H - Prints the list of commands
    PO - Pop an element of the stack
    PU - Push an item onto the stack
    Q - Quit Testing

    Command : C
    -> Stack Created

    Command : E
    -> Stack Empty? True

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

    Problem 2 : Create a program that will output a base-ten representation to a base two representation using stack data structure.

    I need your help people .. T_T
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What do you have so far?

    I can help you if you are stuck in your code but I cannot provide a complete solution.

    Comment

    • ch13f
      New Member
      • Jul 2007
      • 5

      #3
      Originally posted by weaknessforcats
      What do you have so far?

      I can help you if you are stuck in your code but I cannot provide a complete solution.
      #include<stdio. h>
      main()
      {
      int x,y,num,i;
      int ans[500];
      clrscr();
      printf("Enter number:");
      scanf("%d",&num );
      x=num%2;
      y=num/2;
      i=0;
      while(y){
      if(!x)
      ans[i]=x;
      x=x%2;
      y=y/2;
      i++;
      }
      for(i;i>=0;i--) printf("%d", ans[i]);
      getch();
      }

      that's for the #2 ..
      but It seems that it doesn't give the correct output .. :)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        An array is not a good choice for a stack.

        You need a linked list.

        You "push" a number onto the top of the stack by adding it to the end of the list.
        You "pop" a number by removing ti from the end of the list.
        You dump the stack by reading the list from bback to front
        etc...

        OR

        You "push" a number onto the top of the stack by adding as the first element of the list.
        You "pop" a number by removing itfrom the front of the list.
        You dump the stack by reading the list from front to back
        etc...

        It doesn't matter which way you choose as long as the last number "pushed" is the next one "popped".

        Comment

        • seforo
          New Member
          • Nov 2006
          • 60

          #5
          Search google on how to create a stack data structure, that will be a very good start

          Comment

          Working...