#include
#include
#define SIZE 5
struct tag
{
int a[SIZE];
int top;
}; //Structure definition for stack
typedef struct tag stack;
struct tagg
{
int items[SIZE];
int rear;
int front;
}; //Structure definition for queue
typedef struct tagg QUEUE;
void push();
int pop();
void display();
void rev(); //Function declarations(parameters not required at declaration)
int main()
{
int ch, element, dnum;
stack st;
st.top= -1;
do
{ //Displaying menu for operations on stack
printf("\n1.) PUSH");
printf("\n2.) POP");
printf("\n3.) DISPLAY");
printf("\n4.) REVERSE");
printf("\n5.) EXIT");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter element to push: ");
scanf("%d", &element);
push(&st, element);
break;
case 2: dnum = pop(&st);
if(dnum != -1)
printf("\nDeleted element: %d", dnum);
break;
case 3: display(st);
break;
case 4: rev(&st);
break;
case 5: break;
default: printf("\nWrong choice!! Enter choice again.");
}
}while(ch!=5);
}
void push(stack *s, int ele)
{
if(s->top -1)
{
printf("\nStack is empty");
}
else
{
for(i=s.top; i>=0; i--)
{
printf("\n%d", s.a[i]);
}
printf("\n");
}
}
//Function to reverse a stack using queue
void rev(stack *s)
{
QUEUE q;
q.rear = -1;
q.front = 0;
if(s->top==-1)
{
printf("\nStack empty");
return;
}
while((s->top) > -1)
{
q.items[++(q.rear)] = s->a[(s->top)--];
}
while(q.front <= q.rear)
{
s->a[++(s->top)] = q.items[(q.front)++];
}
} Use the two queues to simulate a stack.
Reversing a Stack using Queue.
There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements. However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack. You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue. Your queue will have the exact same elements as in the beginning, but in reverse order. The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers. Following is pseudocode: Queue<Item> reverse (Queue<Item> queue) { Stack<Item> stack; Item item; while (queue.remove(&item)) { stack.push(item); } while(stack.pop(&item)) { queue.add(item); } return queue; }
// stack to contain content Stack sourceStack = new Stack(); // ... fill sourceStack with content // stack to contain reversed content Stack targetStack = new Stack(); while (!sourceStack.empty()) { targetStack.push(sourceStack.pop()); } // targetStack contains the reversed content of sourceStack
A stack is generally First In, Last Out, and a queue is First In First Out.Item can be added or removed only at one end in stack and in a queue insertion at the rear and deletion from the front.The basic operation of stack are 'push' and 'pop', on other hand of queue are 'enque' and 'dequeue'.
LIFO and stack are synonyms, so are FIFO and queue.
Design an algorithm to show the different operations on a stack?
There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements. However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack. You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue. Your queue will have the exact same elements as in the beginning, but in reverse order. The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers. Following is pseudocode: Queue<Item> reverse (Queue<Item> queue) { Stack<Item> stack; Item item; while (queue.remove(&item)) { stack.push(item); } while(stack.pop(&item)) { queue.add(item); } return queue; }
// stack to contain content Stack sourceStack = new Stack(); // ... fill sourceStack with content // stack to contain reversed content Stack targetStack = new Stack(); while (!sourceStack.empty()) { targetStack.push(sourceStack.pop()); } // targetStack contains the reversed content of sourceStack
A stack is generally First In, Last Out, and a queue is First In First Out.Item can be added or removed only at one end in stack and in a queue insertion at the rear and deletion from the front.The basic operation of stack are 'push' and 'pop', on other hand of queue are 'enque' and 'dequeue'.
A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.
A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.
A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.
LIFO and stack are synonyms, so are FIFO and queue.
Design an algorithm to show the different operations on a stack?
In stack , the object which is last in will be first out (LIFO), whereas in queue the object which is first in will be first out (FIFO).
A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.
yes,cursor implementation possible in priority queue.
A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Some applications of stack are : Polish notation, reversing string, backtracking , quick sort algorithm etc. The queue is a linear data structure where operations od insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. Whenever a new item is added to queue, rear pointer is used. and the front pointer is used when an item is deleted from the queue.