STACK/FIFO as a linked list

This is a discussion on STACK/FIFO as a linked list within the C Programming forums, part of the General Programming Boards category; just was experimenting.. here is the code: Code: #include <stdlib.h> #include <stdio.h> #define snapd(arg) printf(#arg " = %d\n", arg); #define ...

  1. #1
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78

    Post STACK/FIFO as a linked list

    just was experimenting.. here is the code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define snapd(arg) printf(#arg " = %d\n", arg);
    #define snapf(arg) printf(#arg " = %f\n", arg);
    
    struct fifo 
    {
        int data;
        struct fifo *next;
    };
    
    struct fifo *push(struct fifo *st, int value)
    {
        struct fifo *newnode;
    
        if (st == NULL)
            {
            newnode = (struct fifo *)malloc(sizeof(struct fifo));
            newnode->data = value;
            newnode->next = NULL;
            return newnode;
            }
    
        newnode = (struct fifo *)malloc(sizeof(struct fifo));
        newnode->data = value;
        newnode->next = st;
        return newnode;
    
    }
    
    struct fifo *pop(struct fifo *st)
    {
        struct fifo *top;
    
        if (st == NULL)
            {
            printf("The stack is empty!\n");
            return NULL;
            }
    
        top = st->next;
        free(st);
        return top;
    
    }
    
    void printstack(struct fifo *st)
    {
        if (st == NULL)
            {
            printf("The stack is empty!\n");
            return;
            }
    
        do
            {
            printf("%d\n", st->data);
            st = st->next;
            }
        while (st);
    }
    
    
    
    int main (int argc, char *argv[])
    {
    
        struct fifo *myfifo = NULL;
        int command, value;
    
        printf("FIFO/Stack implementation with linked list.\n");
        printf("1 - push value.\n");
        printf("2 - pop value.\n");
        printf("3 - print stack.\n");
        printf("4 - exit.\n");
        printf("Enter your command: ");
    
    
        for (;;)
            {
    
            scanf("%d", &command);
    
            switch (command)
                {
                case 1:
                    {
                    printf("Enter the value to push on to the stack: ");
                    scanf("%d",&value);
                    myfifo = push(myfifo, value);
                    printf("Enter the next command: ");
                    break;
                    }
    
                case 2:
                    {
                    myfifo = pop(myfifo);
                    if (myfifo != NULL) printf("The value is popped out of the stack\n");
                    printf("Enter the next command: ");
                    break;
                    }
    
                case 3:
                    {
                    printf("The contains of the stack is:\n");
                    printstack(myfifo);
                    printf("====================================\n");
                    printf("Enter the next command: ");
                    break;
                    }
    
                case 4:
                    return 0;
    
                default:
                    {
                    printf("Wrong command!\n");
                    printf("1 - push value.\n");
                    printf("2 - pop value.\n");
                    printf("3 - print stack.\n");
                    printf("4 - exit.\n");
                    printf("Enter your command: ");
                    break;
                    }
    
                }
            }

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Your heading is confusing. Stacks and recursion calls are LIFO. ADTs like queues or FIFO.

  3. #3
    Registered User BlackOps's Avatar
    Join Date
    Jul 2009
    Location
    AZERBAIJAN
    Posts
    78
    ah..yeah i know its wrong... i mean Stack/LIFO... true...queues are FIFO yeah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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