Sequential and Linked Representation Sequential Representation of an Integer Stack

J.E.D.I. been the first person in the line. This person gets to leave the line first. Relate this to how dequeue works. Here is a simple illustration of how a queue looks like. 1 2 3 4 5 6 7 8 9 ... n-1 Eve Jayz KC Jojo Toto Kyla DMX front end ← Insert → Delete Table 4: Queue illustration The queue is empty if end is less than front. Meanwhile, it is full when the end is equal to n-1.

3.3.4 Sequential and Linked Representation

ADTs can generally be represented using sequential and linked representation. It is easier to create the sequential representation with the use of arrays. However, the problem with an array is its limited size, making it inflexible. Memory space is either wasted or not enough with use of arrays. Consider this scenario. You’ve created an array and declared it to be able to store a maximum of 50 elements. If the user only inserts exactly 5 elements, take note that 45 spaces would have been wasted. On the other hand, if the user wants to insert 51 elements, the spaces provided in the array would not have been enough. Compared to sequential representation, the linked representation may be a little more difficult to implement but it is more flexible. It adapts to the memory need of the user. A more detailed explanation on linked representation are discussed in a later section. Introduction to Programming II Page 46 J.E.D.I.

3.3.5 Sequential Representation of an Integer Stack

class SeqStack { int top = -1; initially, the stack is empty int memSpace[]; storage for integers int limit; size of memSpace SeqStack { memSpace = new int[10]; limit = 10; } SeqStackint size { memSpace = new int[size]; limit = size; } boolean pushint value { top++; check if the stack is full if top limit { memSpace[top] = value; } else { top--; return false; } return true; } int pop { int temp = -1; check if the stack is empty if top = 0 { temp = memSpace[top]; top--; } else { return -1; } return temp; } public static void mainString args[] { SeqStack myStack = new SeqStack3; myStack.push1; myStack.push2; myStack.push3; myStack.push4; System.out.printlnmyStack.pop; System.out.printlnmyStack.pop; System.out.printlnmyStack.pop; System.out.printlnmyStack.pop; } } Introduction to Programming II Page 47 J.E.D.I. The following figure gives a trace of SeqStacks main method. Figure 3.2:Trace of SeqStack Introduction to Programming II Page 48 J.E.D.I.

3.3.6 Linked Lists