Gadgets 4 Students Career Guide Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
707 views
in RTU/BTU B.Tech(CSE-III Sem) DSA LAB by Goeduhub's Expert (7.6k points)
edited by

Simulate a stack  using a one dimensional array as storage element. The program should implement the basic addition, deletion  operations.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

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 : 

  1. PUSH  - Push is used to insert an element into a stack .
  2. POP - Pop is used to delete a element from a stack .
  3. Peek - To find top of stack.
  4. 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 :

  1. If top=0 or top=null then it means that the stack is empty or we can say underflow.
  2. 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(stackitem): 

    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__(selfdata):

        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(selfdata):

        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


Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...