linked lists, stacks and queues

This is a discussion on linked lists, stacks and queues within the C Programming forums, part of the General Programming Boards category; Hello! I don't know if you can help me but I've got an assignment that I can't solve. Suppose we ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    9

    linked lists, stacks and queues

    Hello!
    I don't know if you can help me but I've got an assignment that I can't solve.

    Suppose we have 2 ferry boats, S and Q (stack and queue implementation). We have to build a program with the following conditions:
    there's a file to read data (carplates)

    car0.txt
    NBE1016 S<new line>
    XKZ2016 Q<new line>
    IPN1017 S<new line>
    PP21017 Q<new line>
    NNB2018 Q<new line>

    then suppose that at first, ALL cars from dock fill boat S (which ships when boat is full or there's no car left in the dock)

    and travell until the middle dock where ALL cars leave S,and a new file is written with the new order.

    Then, cars for Q fill that boat (which ships when boat is full or there's no car for Q left in the dock) until the final destination.


    We MUST use the following structures:
    Code:
    /* Node structure */
    typedef struct dnode {
        char *Name; /* CAR ID */
        struct dnode *Next; /* Pointer to next node in the linked-list */
    } DNODE;
    
    /* Stack structure type */
    typedef struct Stack {
        int Capacity; /* the max elements stack is permitted to hold */
        int NumElements; /* current number of elements in stack */
        DNODE *Top; /* pointer to top node */
    } STACK;
    
    /* Queue structure type */
    typedef struct Queue {
        int Capacity; /* the max elements in queue */
        int NumElements; /* num of elements in queue */
        DNODE *First; /* pointer to first node */
        DNODE *Last;  /* pointer to last node */
    } QUEUE;

    We run the program with the following arguments:
    ./as.exe <capacity of S> <capacity of Q> cars0.txt out0_s out0_q

    eg
    ./as.exe 2 1 cars0.txt out0_s out0_q

    and the output must be
    out0_s0.txt
    XKZ2016<new line>
    NBE1016<new line>

    out0_q0.txt
    XKZ2016<new line>

    out0_q1.txt
    NBE1016<new line>

    out0_s1.txt
    PP21017<new line>
    IPN1017<new line>

    out0_q2.txt
    PP21017<new line>

    out0_q3.txt
    IPN1017<new line>

    out0_s2.txt
    NNB2018<new line>

    out0_q4.txt
    NNB2018<new line>

    BUT I MESSED UP everything ....

    I attach my code so far.................

    Can anyone help me PLEASE PLEASE PLEASE!
    Thanks!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    IMHO figure out the difference between C source and header files.
    Are you sure that stack.h and queue.h are indeed headers or do they need to be source files?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Yes, that's how we've been told to build them...
    So, we must follow the rules!

    Thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by aniramg69
    BUT I MESSED UP everything ....
    How does it not work? If you really did mess up everything, then restart from scratch, but this time do one part, test, and then do another part, test, etc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    I would heed laserlight and start from scratch as the whole thing is messed up.
    This time take care to separate the statements that should go into the source file (as3b.c)
    from the ones that should go into the header files (stack.h and queue.h).

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    1
    Did u find the solution finally? i have the same problem in my exercise.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    For the sake of everyone could you post the problem. It's not completely clear to me what the OP has in mind.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    I've made some improvements to the code
    BUT the result is not what I want...

    here it is

    I'd appreciate some real help...
    thanks
    Attached Files Attached Files

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think things would be easier for everyone, especially yourself, if you describe more clearly what you are supposed to do AND how it is, specifically in relation to what you wrote, that you intend to get it done.

    For example, in your original post you mention "a new file is written with the new order". What new order? All you've said is that a line of cars drive onto one ferry, go somewhere, drive off, and get onto another ferry. Why would their order change? You also mention other details that may be important, but maybe not, such as "when boat is full or there's no car for Q left". Are any cars left seperate at some point? Are the ferries the same size? Since you claim your program doesn't work the way you want it too, there is not much point in trying to figure out what you wanted by looking at that, is there?

    The first thing I noticed when I looked at as3b.c was this:
    Code:
    fpin=openFile(argv[3], "r");
    [...]
    FILE *openFile(char *filename, char *mode)
    {
       FILE *fp;
    
       fp= fopen(filename, mode);
    
       if (fp==NULL){
          printf("Cannot open file: %s.\nExiting...\n", filename);
          exit(1);
          return NULL;
       }
       else{
          return fp;
       }
    }
    Why would you call exit, and then return??? What's suppose to happen here:
    1) The program ends.
    2) The function ends.
    3) The function returns a NULL pointer.
    It certainly won't be the last case, anyway. Do you see now how trying to deduce intentions from code you already know is flawed might be a waste of time?

    This would be better, perhaps, as this (I'm guessing as to your intentions):
    Code:
    if ((fpin=openFile(argv[3], "r"))==NULL) {
        printf("Cannot open file: %s.\nExiting...\n", argv[3]);
        exit (1);
    }
    [...]
    FILE *openFile(char *filename, char *mode)
    {
       FILE *fp;
       fp= fopen(filename, mode);
    }
    Hmmm, looks like "openFile" is really just fopen! Maybe this would have been fine then:
    Code:
    if ((fpin=fopen(argv[3], "r"))==NULL) {
        printf("Cannot open file: %s.\nExiting...\n", argv[3]);
        exit (1);
    }
    My confidence in your ability to think, much less program a computer, is fading fast. How many other superfluous functions are in here? Before I go any further, you will have to answer my questions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Suppose

    Port 1: there are cars waiting for the ferry: A, B, C, D, E

    the ferry "STACK" comes in and lets say its capacity is 2 - so car A gets in and then car B. The ferry sails and reaches the next port 2. Cars must leave the ferry in the NEW ORDER of the stack: first car B and then car A.

    Now there is another ferry "QUEUE" waiting for cars with capacity lets say 1. Car B gets in, ferry QUEUE sails reaches a third port, car B leaves QUEUE. QUEUE goes back to port 2, car A gets in, QUEUE sails to port 3, car A gets out and QUEUE goes back to port 2.

    A new cycle begins at port 1. the ferry STACK (with capacity 2) sails with car C and car D
    etc etc...

    So, the files created have to look like this:

    out0_s0.txt
    carB
    carA

    out0_q0.txt
    carB

    out0_q1.txt
    carA


    out0_s1.txt
    carD
    carC

    out0_q2.txt
    carD

    out0_q3.txt
    carC



    out0_s2.txt
    carE

    out0_q4.txt
    carE

    the concept is to implement the idea of stacks and queues
    of course the number of cars is bigger, so is capacity of STACK and QUEUE

    thanks

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Now, how had you meant to do this using the code you have? Try going through all the functions you wrote and -- referring to them by name -- explain the intended purpose of the function, how it is called and fits into the flow of the program, and whether you think it accomplishes this purpose. Then we can get closer to helping you solve all your problems.

    I'm going out now but I will probably be back in the next 24hrs.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stacks and queues info
    By Emeighty in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2008, 01:41 AM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. need help understanding Linked lists
    By Brain Cell in forum C Programming
    Replies: 6
    Last Post: 07-16-2004, 01:29 PM
  4. stacks and queues
    By j0hnb in forum C Programming
    Replies: 4
    Last Post: 04-16-2003, 09:44 PM
  5. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21