0% found this document useful (0 votes)
2 views8 pages

Stack

The document describes various implementations of a stack data structure using arrays and linked lists. It includes procedures for push, pop, and top operations, along with checks for stack fullness and emptiness. Sample code in different programming languages illustrates these implementations, including static and dynamic linked lists.

Uploaded by

maweretna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Stack

The document describes various implementations of a stack data structure using arrays and linked lists. It includes procedures for push, pop, and top operations, along with checks for stack fullness and emptiness. Sample code in different programming languages illustrates these implementations, including static and dynamic linked lists.

Uploaded by

maweretna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STACK USING ARRAY

//Declaration and initialisation of global variables


DECLARE TheStack: ARRAY [1:10] OF INTEGER
DECLARE TopOfStack: INTEGER
TopOfStack ← 0 //Points at the element added to the stack

PROCEDURE Push (Value: INTEGER)


IF TopOfStack = LENGTH(TheStack) THEN
OUTPUT “Stack is full”
ELSE
TopOfStack ← TopOfStack + 1
TheStack [TopOfStack] ← Value
ENDIF
ENDPROCEDURE

PROCEDURE Pop ()
IF TopOfStack = 0 THEN
OUTPUT “Stack is empty”
ELSE
OUTPUT TheStack [TopOfStack]
TopOfStack ← TopOfStack - 1
ENDIF
ENDPROCEDURE

FUNCTION Top RETURNS INTEGER

IF TopOfStack = 0 THEN

OUTPUT “Stack is empty”

RETURN -1

ELSE

TopMostElement ← TheStack [TopOfStack]

RETURN TopMostElement

ENDIF

ENDFUNCTION

# Other stack operations include IsEmpty and isFull.


STACK USING ARRAY (ALTERNATIVELY)
//Declaration and initialisation of global variables
DECLARE TheStack: ARRAY [1:10] OF INTEGER
DECLARE TopOfStack: INTEGER
TopOfStack ← 1 //Points at the index of the element to be added to the stack

PROCEDURE Push (Value: INTEGER)


IF TopOfStack > LENGTH(TheStack) THEN
OUTPUT “Stack is full”
ELSE
TheStack [TopOfStack] ← Value
TopOfStack ← TopOfStack + 1
ENDIF
ENDPROCEDURE

PROCEDURE Pop ()
IF TopOfStack = 1 THEN
OUTPUT “Stack is empty”
ELSE
TopOfStack ← TopOfStack - 1
OUTPUT TheStack [TopOfStack]
ENDIF
ENDPROCEDURE

FUNCTION Top RETURNS INTEGER

IF TopOfStack = 1 THEN

OUTPUT “Stack is empty”

RETURN -1

ELSE

TopMostElement ← TheStack [TopOfStack - 1]

RETURN TopMostElement

ENDIF

ENDFUNCTION
STACK USING STATIC LINKED LIST
//Declaration and initialisation of global variables
TYPE Node
DECLARE Data: STRING
DECLARE Ptr: INTEGER
ENDTYPE

DECLARE TheStack: ARRAY [1:10] OF Node


DECLARE FreePtr, TopOfStackPtr, TempPtr, Index: INTEGER
TopOfStackPtr ← -1
FreePtr ← 1

FOR Index ← 1 TO 10
TheStack [Index].Ptr ← Index + 1
NEXT Index
TheStack [10].Ptr ← -1

PROCEDURE Push (Value: INTEGER)


IF FreePtr = -1 THEN
OUTPUT “Stack is full”
ELSE
TheStack [FreePtr].Data ← Value
TempPtr ← FreePtr
FreePtr ← TheStack [FreePtr].Ptr
TheStack [TempPtr].Ptr ← TopOfStackPtr
TopOfStackPtr ← TempPtr
ENDIF
ENDPROCEDURE
PROCEDURE Pop
IF TopOfStackPtr = -1 THEN
OUTPUT “Stack is empty”
ELSE
OUTPUT TheStack [TopOfStackPtr].Data
TempPtr ← TopOfStackPtr
TopOfStackPtr ← TheStack [TopOfStackPtr].Ptr
TheStack [TempPtr].Ptr ← FreePtr
FreePtr ← TempPtr
ENDIF
ENDPROCEDURE
Sample code 1: USING ARRAY

size = 4
stack = [-1 for _ in range(size)]
topOfStack = -1

def push(item):
global topOfStack
if topOfStack == len(stack) - 1:
print("Stack is full")
else:
topOfStack = topOfStack + 1
stack[topOfStack] = item

def pop():
global topOfStack
if topOfStack == -1:
print("Stack is empty")
else:
print(stack[topOfStack])
stack[topOfStack] = -1
topOfStack = topOfStack – 1

def top():
if topOfStack == -1:
print("Stack is empty")
else:
print(stack[topOfStack])
Sample code 2:

class Stack:
def __init__(self, size):
[Link] = size
[Link] = ["" for _ in range([Link])]
[Link] = -1

def push(self, theName):


if self.__isFull():
print("Stack is full.")
return
[Link] = [Link] + 1
[Link][[Link]] = theName

def pop(self):
if self.__isEmpty():
print("Stack is empty.")
return
thisName = [Link][[Link]]
[Link][[Link]] = ""
[Link] = [Link] - 1
return thisName

def peek(self):
if self.__isEmpty():
print("Stack is empty.")
return
return [Link][[Link]]

def __isEmpty(self):
if [Link] == -1:
return True

def __isFull(self):
return [Link] == len([Link]) - 1
Sample code 3: USING STATIC LINKED LIST

size = 10
stackList = [[-1 for _ in range(2)] for _ in range(size)]
topOfStack = -1
freeList = 0

for index in range(size - 1):


stackList[index][1] = index + 1

def isEmpty():
if topOfStack == -1:
return True

def isFull():
if freeList == -1:
return True

def push(item):
global freeList, topOfStack
if isFull():
print("Stack is full")
else:
stackList[freeList][0] = item
nextFree = freeList
freeList = stackList[freeList][1]
stackList[nextFree][1] = topOfStack
topOfStack = nextFree

def pop():
global topOfStack, freeList
if isEmpty():
print("Stack is empty")
else:
print(stackList[topOfStack][0])

temp = topOfStack
topOfStack = stackList[topOfStack][1]
stackList[temp][1] = freeList
freeList = temp

def top():
if not isEmpty():
print(stackList[topOfStack][0])
else:
print("Stack is empty")
Sample code 4: USING DYNAMIC LINKED LIST: (You can ignore this)

class Node:
def __init__(self, data):
[Link] = data
[Link] = None

class Stack:
def __init__(self):
[Link] = None

def isEmpty(self):
return [Link] is None

def Push(self, item):


newNode = Node(item)
[Link] = [Link]
[Link] = newNode

def Pop(self):
if [Link]():
return None
temp = [Link]
[Link] = [Link]
return [Link]

def Peek(self):
if [Link]():
return None
return [Link]

def Display(self):
temp = [Link]
while temp is not None:
print([Link], end=' -> ')
temp = [Link]
print("None")

You might also like