Thread: Stack & Queue

  1. #1
    Registered User UkOi YOku's Avatar
    Join Date
    Sep 2013
    Posts
    3

    Stack & Queue

    ok i have this activity which i dont know if it's really the correct output on the input ones.

    i have this problem making two stacks and two queues.
    A.Input stack: used to store all user input
    B.input queue: used to store all user input
    C.Output stack: used to store data deleted from input queue
    D. Output queue: used to store data deleted from input stack

    I only have this menu on selecting insert or delete.

    i'll just post my main.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include"teststack.h"
    
    void main(){
        StackList s1=create();
        int choice,item;
        
        do{
            do{
                printf("Press 1-Insert\n");
                printf("Press 2-Delete\n");
                printf("Press 3-Exit\n");
                printf("Enter choice: ");
                scanf("%d", &choice);
    
        if(choice<1 || choice>3)
            printf("Invalid input");
            
            }while (choice<1 || choice>3);
    
    
    switch(choice)
            {
            case 1:
            printf ("Enter number to push: ");
            scanf  ("%d", &item);
            printf("\n====================");
            printf("\nInput Stack: \n");
            push (s1, item);
            display(s1);
            printf("\n");
            printf("\nInput Queue: \n");
            enqueue(s1, item);
            display(s1);
            printf("====================\n");
            
            
                break;
            case 2:
            pop(s1);
            push (s1, item);
            printf("\n====================");
            printf("\nOutput Stack: \n");
            display(s1);
            printf("\n");
            printf("\nOutput Queue: \n");
            dequeue(s1);
            enqueue(s1, item);
            display(s1);
            printf("\n");
            printf("====================\n");
            
            
        
            
                break;
            default:
                printf("End.");
    }
    }while(choice!=3);
        
        }
    and this is the output of Input Stacks & Queues.

    Stack &amp; Queue-qq-png

    is this correct?
    Last edited by UkOi YOku; 09-02-2013 at 06:21 AM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Usually stack places items at one end and deletes "pops" them off from the same end. Example:

    stack: [ ]

    push 1

    stack: [1]

    push 50

    stack: [1 50]

    push 12

    stack: [1 50 12]

    pop() returns 12

    stack: [1 50]

    pop() returns 50

    stack: [1]

    Stack is called LIFO "last in first out" for this reason. Queue in concept is similar but deletion hapens from opposite end ("first in first out" FIFO). Your queue program seems to operating at both ends based on reading the output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stubborn Stack and Queue
    By federico in forum C++ Programming
    Replies: 7
    Last Post: 10-10-2010, 12:26 PM
  2. Stack and Queue
    By audinue in forum C Programming
    Replies: 7
    Last Post: 07-04-2008, 12:28 PM
  3. Queue Stack Palindrome
    By silicon in forum C++ Programming
    Replies: 20
    Last Post: 07-15-2004, 03:38 PM
  4. queue / stack
    By smd in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 01:30 PM
  5. change stack to a queue
    By sballew in forum C Programming
    Replies: 12
    Last Post: 12-03-2001, 11:16 PM