C language queue (full version) with two stacks

A queue is a FIFO (first in - first out, FIFO ) data structure, the queue elements from the rear end (REAR) enqueued (Push), from the front end (Front) dequeued (pop).
The most straightforward way to achieve queue is a linked list, but in this article I will introduce another method - using the stack.
Stack is a LIFO (last in - first out, LIFO ) data structure, the stack elements from the top of the stack (Top) pressed (Push), also pops up (pop) from the stack.
In order to meet the characteristics of the FIFO queue, we need to use two stacks, with one of their team to reverse the order of the elements, with the other elements to store the final sequence.

A method (using two stacks enqueued - O (n) O (n ), the team - O (1) O (1 ))
algorithm

The team (push)

It is a FIFO queue, but a stack is a LIFO. This means that the latest press elements have to be placed into the bottom of the stack. To accomplish this, we first need to move all the elements s1 and s2, then the new element is pushed s2. Finally s2 pop all the elements, then the pop-up element is pressed into s1.

 

Dequeue (pop)

S1 from the pop-up directly on it, because the top element s1 is the team's first queue element. At the same time we pop the top element s1 after assignment to the team's first front variable elements.

The above pictures and text from https://leetcode-cn.com/problems/implement-queue-using-stacks/solution/yong-zhan-shi-xian-dui-lie-by-leetcode/

 

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
// create a stack
struct Stack {
int Data [MAXSIZE];
int Top;
};
typedef struct Stack MyStack;
// queue definition dual stack
typedef struct {
MyStack S1; // Sl main stack
MyStack s2; // S2 is inverted to stack
} MyQueue;


/** Initialize your data structure here. */

MyQueue* myQueueCreate() {
MyQueue * tempQueue =(MyQueue *)malloc(sizeof(MyQueue));
tempQueue->s1.top = -1 ;
tempQueue->s2.top = -1 ;
return tempQueue ;

}

/ ** The Back to the Push Element of X Queue * /.
Void myQueuePush (obj MyQueue *, int X) {
IF (obj-> s1.top <MAXSIZE)
{
the while (obj-> s1.top = -!. 1) // stack-full
{
obj-> s2.data [++ (obj-> s2.top)] = obj-> s1.data [(obj-> s1.top) -]; //// the S1 elements in the stack is pressed into S2 for reversing
}
obj-> s1.data [++ (obj-> s1.top)] = X; //// push element is pressed into the stack S1 (S1 is empty case stack , because it has all the elements of the matter to S2)
the while (obj-> s2.top = -!. 1)
{
obj-> s1.data [++ (obj-> s1.top)] = obj-> s2.data [(obj-> s2.top) -] ; // then all elements in the stack S2 is pressed into reverse Sl
}
}
}
void show_myQueue (MyQueue * obj)
{
int TEMP = obj-> s1.top;
the while (TEMP = -. 1!)
{
the printf ( "% D", obj-> s1.data [temp--]);
}
the printf ( "\n");
}

/** Removes the element from in front of queue and returns that element. */
int myQueuePop(MyQueue* obj) {
if(obj->s1.top!=-1)
{
return obj->s1.data[obj->s1.top--] ;
}
// return NULL ;

}

/** Get the front element. */
int myQueuePeek(MyQueue* obj) {
if(obj->s1.top!=-1)
{
return obj->s1.data[obj->s1.top] ;
}
// return NULL ;
}

/** Returns whether the queue is empty. */
bool myQueueEmpty(MyQueue* obj) {
if(obj->s1.top == -1)
return true ;
return false ;

}

void myQueueFree(MyQueue* obj) {
free(obj);
}

 

int main ()
{
int I;
MyQueue * myQueue = NULL;
myQueue = myQueueCreate ();
for (I = 0; I <. 3; I ++)
{
myQueuePush (myQueue, I);
}
show_myQueue (myQueue);
the printf ( "the Force elements are: D% \ n-", myQueuePop (myQueue));
the printf (" in this case the first element in the queue is:% D \ n-", myQueuePeek (myQueue));
}

/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);

* int param_2 = myQueuePop(obj);

* int param_3 = myQueuePeek(obj);

* bool param_4 = myQueueEmpty(obj);

* MyQueueFree (obj);
* /

 

Guess you like

Origin www.cnblogs.com/cocobear9/p/12343816.html