Ques. Simulate a stack using a one dimensional array as storage element. The program should implement the basic addition, deletion operations.
Answer :
Stack : Stack is a linear data structure that uses LIFO (last in first out) or FILO(First In Last Out) as its functionality. Here the last element is first poped out and the first element will be poped out at last .
Stack is a list of elements in which an element may be inserted or deleted only at one place called as top of the stack(peek). This means that the elements are removed from a stack in the reverse order of that in which they were inserted into stack .
Example: Plates stacked over one another in the Kitechen, Conversion of infix to prefix / infix to postfix etc.
There are Four basic operations of stack :
- PUSH - Push is used to insert an element into a stack .
- POP - Pop is used to delete a element from a stack .
- Peek - To find top of stack.
- Isempty - check stack is empty or not.
Top here contains the location of top element of stack i.e the last element of stack . Maxstack gives the maximum number of element that can be held by the stack . there are two conditions of stack :
- If top=0 or top=null then it means that the stack is empty or we can say underflow.
- If top=maxstack then stack is full or overflow condition
Program-1 Stack implementation using Arrays :
s = [] #stack creation
# append is to push element into stack
s.append('goeduhub')
s.append('eat')
s.append('sleep')
s.append('code')
print(s)
#pop out of last element first
s.pop()
print(s)
s.pop()
print(s)
#want to access stack element without deleting
print(s[-1])
s.pop()
print(s)
s.pop()
print(s)
Output :
['goeduhub', 'eat', 'sleep', 'code']
['goeduhub', 'eat', 'sleep']
['goeduhub', 'eat']
['goeduhub']
[]
Program-2 Stack Implementation using Array :
# import maxsize from sys module
# Used to return -infinite when stack is empty
from sys import maxsize
# function to create a empty stack
# function to create a empty stack
def make_a_Stack():
stack = []
return stack
# Stack is empty when stack size is 0
def stack_is_Empty(stack):
return len(stack) == 0
# Puch function is to add an item to stack
def push(stack, item):
stack.append(item)
print(item + " is pushed into the stack ")
# Popp function to remove the last item from stack
def popp(stack):
if (stack_is_Empty(stack)):
return str(-maxsize -1) # return minus infinite
return stack.pop()
# Function to return the top from stack without removing it
def last_element(stack):
if (stack_is_Empty(stack)):
return str(-maxsize -1)# return minus infinite
return stack[len(stack) - 1]
stack = make_a_Stack()
push(stack, str(10))
push(stack, str(20))
push(stack, str(30))
push(stack, str(40))
push(stack, str(50))
print(popp(stack) + " is popped out from the stack")
print(popp(stack) + " is popped out from the stack")
print(popp(stack) + " is popped out from the stack")
print(popp(stack) + " is popped out from the stack")
print(popp(stack) + " is popped out from the stack")
print(stack)
print(last_element(stack))
Output :
10 is pushed into the stack
20 is pushed into the stack
30 is pushed into the stack
40 is pushed into the stack
50 is pushed into the stack
50 is popped out from the stack
40 is popped out from the stack
30 is popped out from the stack
20 is popped out from the stack
10 is popped out from the stack
[]
-9223372036854775808
Program 3 Stack Implementation using Linked List
# Python program for linked list implementation of stack
# Class to represent a node
class StackNode:
# Constructor to initialize a node
def __init__(self, data):
self.data = data
self.next = None
class Stack:
# Constructor to initialize the head of linked list
def __init__(self):
self.head = None
def isEmpty(self):
return True if self.head is None else False
def push(self, data):
newNode = StackNode(data)
newNode.next = self.head
self.head = newNode
print("pushed to stack",data)
def pop(self):
if (self.isEmpty()):
return float("-inf")
temp = self.head
self.head = self.head.next
popped = temp.data
return popped
def peek(self):
if self.isEmpty():
return float("-inf")
return self.head.data
# Driver code
stack = Stack()
stack.push(100)
stack.push(200)
stack.push(300)
print("popped from stack",stack.pop())
print("Top element is ", stack.peek())
Output:
pushed to stack 100
pushed to stack 200
pushed to stack 300
popped from stack 300
Top element is 200
For more Rajasthan Technical University CSE-III Sem DSA Lab Experiments CLICK HERE