stack

php pop/push/shift/unshift, which to use for queues and which for stacks

半城伤御伤魂 提交于 2019-12-11 06:07:15
问题 In PHP there are two ways to use an array as a stack (LIFO) and two ways to use them as a queue (FIFO). One could implement a stack with push & pop , but the same can be done with unshift & shift . Similarly one could implement a queue with push & shift , but the same can be done with unshift & pop . To demonstrate: echo "stack push & pop approach:\n"; $s = []; array_push($s, 'first'); array_push($s, 'second'); array_push($s, 'third'); echo array_pop($s) . '-' . array_pop($s) . '-' . array

Does a stack allocate more space if needed or does it overflow?

会有一股神秘感。 提交于 2019-12-11 06:00:57
问题 For x86 assembly, let's say we have a stack like so The stack has 2 words allocated for the 2 local variables it has. But what if you forcibly push a third local variable to the stack. Does the ESP move upwards to make room for the variable or does the variable override the ESP? 回答1: With x86, instructions that “push” data to the stack also modify the stack pointer ( %esp in this case) to mark the new top of stack. Instructions that “pop” data modify the stack pointer in the opposite

gcc x64 stack manipulation

丶灬走出姿态 提交于 2019-12-11 05:07:45
问题 I try to understand the way gcc x64 organize the stack, a small program generate this asm (gdb) disassemble *main Dump of assembler code for function main: 0x0000000000400534 <main+0>: push rbp 0x0000000000400535 <main+1>: mov rbp,rsp 0x0000000000400538 <main+4>: sub rsp,0x30 0x000000000040053c <main+8>: mov DWORD PTR [rbp-0x14],edi 0x000000000040053f <main+11>: mov QWORD PTR [rbp-0x20],rsi 0x0000000000400543 <main+15>: mov DWORD PTR [rsp],0x7 0x000000000040054a <main+22>: mov r9d,0x6

Getting the name of the calling function in C (without using the preprocessor)

流过昼夜 提交于 2019-12-11 05:04:09
问题 I was wondering if there is a way of finding which function called the current function (at runtime) in C. I know you could use __FUNCTION__ in gcc, but is there a way without using the C preprocessor? Probably not. Cheers 回答1: No, there isn't. C isn't a particularly introspective language - things like the name of a function (or pieces of your call stack) simply aren't available at runtime in any sane fashion. If, for some reason, you are looking for a lot of work for very little benefit,

Largest Item in Heap

℡╲_俬逩灬. 提交于 2019-12-11 04:48:09
问题 The largest item in a heap must appear in position 1, and the second largest must be in position 2 or position 3. Give the list of positions in a heap of size 31 where the kth largest (i) can appear, and (ii) cannot appear, for k=2, 3, 4 (assuming the values to be distinct). I am trying to study this for my midterm but its 3AM and I am stuck on this problem in the book as it does not provide a solution. Any help would be appreciated. 回答1: If you look at the Heap Implementation example on

Heap vs Stack in regard to read/write speed

筅森魡賤 提交于 2019-12-11 04:07:50
问题 I just went through a bunch of stack vs heap threads here on Stack Overflow (and other random sites found through Google), and yet I can't find an answer that provides much (if any) depth to the issue of read/write speed (most answers and articles focus on allocation speed). If I'm writing a C program (in my case, a game) where I'm storing object data in a buffer that's a few kilobytes in size, will it make a difference (in terms of access speed) if that buffer is allocated on the stack or on

implementing stack with linked list in C

情到浓时终转凉″ 提交于 2019-12-11 03:46:05
问题 I'm having trouble implementing a Stack using a linked list with struct. The program compiles fine but when I run it, it prints the first element but then reads the next node as a NULL. I think it might be an error with my passing of the stack to the push method but I am not sure and I have not been successful in fixing it so I'm asking for your help: #include <stdio.h> #include <stdlib.h> struct stackNode{ char data; struct stackNode *nextPtr; }; typedef struct stackNode StackNode; typedef

Removing a specific element in a stack

落爺英雄遲暮 提交于 2019-12-11 03:36:09
问题 I am trying to remove a specific element in a stack, but having some trouble. My thought would be to pop the elements on to a temporary stack, pop the index I am looking for and then pop the elements in my temporary stack back onto the main stack. I am having trouble conjuring up how I can get the temp stack back on top. Any help would be greatly appreciated. public E remove(int index) { Stack<E> tmpStack = new Stack<E>(); if (size() == 0) { return null; } else { for (int i = 0; i < index; i+

C++ stack array limit?

爷,独闯天下 提交于 2019-12-11 03:33:12
问题 I'm running some code which may be pointing out I don't understand the difference between the heap and stack that well. Below I have some example code, where I either declare an array on the stack or the heap of 1234567 elements. Both work. int main(int argc, char** argv){ int N = 1234567; int A[N]; //int* A = new int[N]; } But if we take N to be 12345678, I get a seg fault with int A[N], whereas the heap declaration still works fine. (I'm using g++ O3 -std=c++0x if that matters). What

Implementing queue using stack

与世无争的帅哥 提交于 2019-12-11 03:10:01
问题 This is question from homework: Implement a FIFO queue using two stacks. The total running time of Enqueue and Dequeue functions should be O(n) in the worst case scenario. Also, analyze the running time of the algorithm. What I did: void Enqueue(T *value) { s1.Push(value); } T *Dequeue() { if (s2.size > 0) return s2.Pop(); else if (s1.size > 0) { for (int i = 0; i < s1.size; i++) s2.Push(s1.Pop()); return s2.Pop(); } else return NULL; } Analysis of the algorithm: Running time of one Enqueue