# Array simulation stack and queue

Posted by wtf on Thu, 04 Nov 2021 22:53:13 +0100

# Array simulation stack and queue

## Array simulation stack

Stack features: last in first out

### Template

```// tt indicates the top of the stack
int stk[N], tt = 0;

// Insert a number to the top of the stack
stk[ ++ tt] = x;

// Pop a number from the top of the stack
tt -- ;

// Stack top value
stk[tt];

// Determine whether the stack is empty
if (tt > 0)
{

}
```

### The stack top pointer points to the stack top element (* *)

T tt he stack top pointer points to the stack top element
stk[0] will not be assigned
First the stack top pointer + +, and then assign stk[tt++]=x
Output stack top element cout < < STK [TT]

Pop up stack top element t--

[reference code]

```#include<iostream>

using namespace std;
const int N = 100000+10;
int stk[N],tt = 0;
int main()
{
int m;
cin>>m;
while(m --)
{
int x;
string opt;
cin>>opt;
if(opt == "push")
{   cin>>x;
// Insert element to top of stack
stk[++tt] = x;
}
else if(opt == "pop")
{   // Pop up stack top element
tt--;
}
else if(opt == "empty")
{   // Determine whether the stack is empty
if(tt == 0) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else if(opt == "query")
{   // Get (query) stack top element
cout<<stk[tt]<<endl;
}
}

return 0;
}
```

[change to function]

```#include<iostream>

using namespace std;
const int N = 100000+10;
int stk[N],tt = 0;
// Insert element to top of stack
void push(int x)
{
stk[++tt] = x;
}

void pop()
{
// Pop up stack top element
tt --;
}
// Determine whether the stack is empty
int is__empty()
{
// Returns tt == 0 instead of assigning it a value
return tt == 0;
}
// Get (query) stack top element
int get_top()
{
return stk[tt];
}
int main()
{
int m;
cin>>m;
while(m --)
{
int x;
string opt;
cin>>opt;
if(opt == "push")
{   cin>>x;
// Insert element to top of stack
push(x);
}
else if(opt == "pop")
{   // Pop up stack top element
pop();
}
else if(opt == "empty")
{   // Determine whether the stack is empty
if(is__empty()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else if(opt == "query")
{   // Get (query) stack top element
cout<<get_top()<<endl;
}
}

return 0;
}
```

### The stack top pointer points to the next position of the stack top element

T tt he stack top pointer points to the next position of the stack top element
stk[0] will assign a value
Assign stk[k++] = x at the top of the stack first, and then the pointer at the top of the stack++
Output stack top element cout < < STK [TT - 1]

Pop up stack top element t--

[reference code]

```#include<iostream>

using namespace std;
const int N = 100000+10;
int stk[N],tt = 0;
int main()
{
int m;
cin>>m;
while(m --)
{
int x;
string opt;
cin>>opt;
if(opt == "push")
{   cin>>x;
// Insert element to top of stack
stk[tt++] = x;
}
else if(opt == "pop")
{   // Pop up stack top element
tt--;
}
else if(opt == "empty")
{   // Determine whether the stack is empty
if(tt == 0) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else if(opt == "query")
{   // Get (query) stack top element
cout<<stk[tt - 1]<<endl;
}
}

return 0;
}
```

### Summary:

Use one of the above two implementation methods!

#### Determine palindrome string

```#include<iostream>

using namespace std;
const int N = 100000+10;
int stk[N],tt = 0;
// Insert element to top of stack
void push(int x)
{
stk[++tt] = x;
}

void pop()
{
// Pop up stack top element
tt --;
}
// Determine whether the stack is empty
int is__empty()
{
// Returns tt == 0 instead of assigning it a value
return tt == 0;
}
// Get (query) stack top element
int get_top()
{
return stk[tt];
}
int main()
{
string str;
cin>>str;

for(int i = 0; i < str.size(); i++) push(str[i]);

string s;
for(int i = 0; i < str.size(); i++)
{
s += get_top();
pop();// Get the stack top element and tt-- (move back) get the next stack top element
}

// cout<<s;
if(str == s) cout<<"It's a palindrome string"<<endl;
else cout<<"NO"<<endl;

return 0;
}
```

#### Binary conversion

```#include<iostream>

using namespace std;
const int N = 100000+10;
int stk[N],tt = 0;
// Insert element to top of stack
void push(int x)
{
stk[++tt] = x;
}

void pop()
{
// Pop up stack top element
tt --;
}
// Determine whether the stack is empty
int is__empty()
{
// Returns tt == 0 instead of assigning it a value
return tt == 0;
}
// Get (query) stack top element

int main()
{
int x;
cin>>x;
while(x)
{
push(x % 2);
x /= 2;
}
while(tt != 0)
{
cout<<get_top();
pop();
}

return 0;
}
```

## Array emulation queue

Queue features: first in first out

### 1. General queue

```// hh stands for the head of the team and tt stands for the tail of the team
int q[N], hh = 0, tt = -1; // Because hh starts from 0 and + + tt starts from - 1 when joining the team (q[0] assignment)

// Insert a number to the end of the queue
q[ ++ tt] = x;

// Pop up a number from the head of the team
hh ++ ;

q[hh];

// Determine whether the queue is empty
if (hh <= tt)
{

}

```

Implement a queue. The queue is initially empty. Four operations are supported:

1. push x – insert a number xx to the end of the team;
2. Pop – pop a number from the team head;
3. Empty – judge whether the queue is empty;
4. Query – query the queue header element.

Now, M operations will be performed on the queue, and each operation 3 and operation 4 will output the corresponding results.

Input format

The first line contains the integer M, indicating the number of operations.

The next M lines contain an operation command, which is one of push x, pop, empty and query.

Output format

For each empty and query operation, a query result should be output, and each result occupies one row.

Where, the query result of empty operation is YES or NO, and the query result of query operation is an integer, representing the value of the queue head element.

Data range

1≤M≤100000,
1≤x≤109,
All operations shall be legal.

Input example:

10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6

Output example:

NO
6
YES
4

```#include<iostream>

using namespace std;
const int N = 100000+10;
int q[N], tt = -1, hh; // Because hh starts from 0 and + + tt starts from - 1 when joining the team (q[0] assignment)
// Insert an x at the end of the queue
void push(int x)
{
q[++ tt] = x;
}
void pop()
{
hh ++;
}
// Judge whether the queue is empty and return 1
int is__empty()
{
if(tt >= hh ) return 0;
else return 1;
}
{
return q[hh];
}
int main()
{
int m;
cin>>m;

while(m --)
{
int x;
string opt;
cin>>opt;
if(opt == "push")
{
cin>>x;
push(x);
}
else if(opt == "pop")
{
pop();
}
else if(opt == "empty")
{
if(is__empty() == 1) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
else if(opt == "query")
{
}
}

return 0;
}
```

### 2. Circular queue

```// hh indicates the head of the team and tt indicates the last position at the end of the team
int q[N], hh = 0, tt = 0;

// Insert a number to the end of the queue
q[tt ++ ] = x;
if (tt == N) tt = 0;

// Pop up a number from the head of the team
hh ++ ;
if (hh == N) hh = 0;