You are on page 1of 19

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

BELAGAVI

Report on:
CONTENT BEYOND SYLLABUS
SUBJECT:
APPLICATION DEVELOPMENT USING PYTHON (18CS55)
Mini Project Title
PYTHON STACK IMPLEMENTATION

Computer Science & Engineering


By
SINDHU B N 3VC20CS158
TEJASHWINI MORE 3VC20CS173

Faculty In-Charge

Vinutha Prashanth
Assistant Professor,
CSE Department, RYMEC

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


ACCREDITED BY NATIONAL BOARD OF ACCREDITATION
RAO BAHADUR Y MAHABALESHWARAPPA ENGINEERING COLLEGE
CANTONMENT, BALLARI-583104, KARNATAKA
2022-23
VEERASHAIVA VIDYAVARDAHKA SANGHA’S

RAO BAHADUR Y MAHABALESHWARAPPA


ENGINEERING COLLEGE
(AFFILIATED TO VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELGAUM &
APPROVED BY AICTE, NEWDELHI)
BALLARI – 583104, KARNATAKA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
ACCREDITED BY NATIONAL BOARD OF ACCREDITATION

CERTIFICATE

This is to that project work entitled “PYTHON STACK


certify
IMPLEMENTATION” is a bonafied Work carried out by SINDHUN B N
(3VC20CS158) , TEJASHWINI MORE(3VC20CS173) of 5th Semester, as
CONTENT BEYOND SYLLABUS for the subject: APPLICATION
DEVELOPMENT USING PYTHON (18CS55) during the year 2022-23.

Signature of Staff - Incharge


Mrs. VINUTHA PRASHANTH
Assistant Professor,
Dept. of CSE, RYMEC

Name of Students: Signature with Date


1. SINDHU B N
2. TEJASHWINI MORE
ABSTRACT:
This page shows the animation of the working of Stack. Stack in Data Structure is a
linear type of data structure that follows the LIFO (Last-In-First-Out) principle and allows
insertion and deletion operations from one end of the stack data structure, that is top.
Implementation of the stack can be done by contiguous memory which is an array, and non-
contiguous memory which is a linked list. Stack plays a vital role in many applications.

INTRODUCTION:
The Stack in data structure is a linear data structure that accompanies a principle
known as LIFO (Last In First Out) or FILO ( First In Last Out). Real-life examples of a stack are
a deck of cards, piles of books, piles of money, and Many more.

This example allows you to perform operations from one end only, like when you insert and
remove new books from the top of the stack. It means insertion and deletion in the stack
data structure can be done only from the top of the stack. You can access only the top of the
stack at any given point in time.

• Inserting a new element in the stack is termed a push operation.


• Removing or deleting elements from the stack is termed pop operation.

Stack, Representation in Data Structure:


Working of Stack in Data Structure:
Now, assume that we have a stack of books.

We can only see the top, i.e., the top-most book, namely 40, which is kept top of the stack.

If we want to insert a new book first, namely 50, we must update the top and then insert a
new text.

And if we want to access any other book other than the topmost book that is 40, you first
remove the topmost book from the stack, and then the top will point to the next topmost
book.

Basic Operations on Stack in Data Structure:


There following are some operations that are implemented on the stack.

Push Operation:
Push operation involves inserting new elements in the stack. Since you have only one end to
insert a unique element on top of the stack, it inserts the new element at the top of the
stack.

Push operation includes various steps, which are as follows :


Step 1: First, check whether or not the stack is full

Step 2: If the stack is complete, then exit


Step 3: If not, increment the top by one

Step 4: Insert a new element where the top is pointing

Step 5: Success

Pop Operation:
Pop operation refers to removing the element from the stack again since you have only one
end to do all top of the stack. So removing an element from the top of the stack is termed
pop operation.

Step 1: First, check whether or not the stack is empty

Step 2: If the stack is empty, then exit

Step 3: If not, access the topmost data element

Step 4: Decrement the top by one

Step 5: Success

Peek Operation:
Peek operation refers to retrieving the topmost element in the stack without removing it
from the collections of data elements.

isFull():
isFull function is used to check whether or not a stack is empty.

isEmpty():
isEmpty function is used to check whether or not a stack is empty.

ANIMATION OF WORKING OF STACK:


Python provides various options for developing graphical user interfaces (GUIs). Most
important are listed below.

• Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python.
We would look this option in this chapter.
• wxPython − This is an open-source Python interface for
wxWindows http://wxpython.org.
• JPython − JPython is a Python port for Java which gives Python scripts seamless
access to Java class libraries on the local machine http://www.jython.org.
In this, animation of the working of Stack is achieved by using Tkinter module .

Tkinter Programming:
Tkinter is the standard GUI library for Python. Python when combined with Tkinter
provides a fast and easy way to create GUI applications. Tkinter provides a powerful
object-oriented interface to the Tk GUI toolkit.

Creating a GUI application using Tkinter is an easy task. All you need to do is perform the
following steps −

• Import the Tkinter module.

• Create the GUI application main window.

• Add one or more of the above-mentioned widgets to the GUI application.

• Enter the main event loop to take action against each event triggered by the user.

Example

#!/usr/bin/python

import Tkinter
top = Tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
This would create a following window −

DESIGN AND IMPLEMENTATION

➢ Source code:
#Stack animation
from tkinter import *

from tkinter import messagebox

import time

class StackVisualizer:

def __init__(self, root):

self.window = root

self.window.config(bg="gray")

self.last_label_value_keep = []

self.block_value_counter = []

self.block_make = 0

self.final_set_block = 0

self.extra_decrease = 0

#Label,Button,Entry

self.heading_name = None

self.sub_heading = None

self.stack_indicator = None

self.push_btn = None

self.pop_btn = None

self.element_take_entry = None

self.entry_number = None

self.element_take_label = None

self.add_btn = None

self.top_index = None

self.index_neg = None

self.index_0 = None

self.index_1 = None

self.index_2 = None

self.index_3 = None
self.index_4 = None

self.index_5 = None

self.number = IntVar()

self.value_entry = IntVar()

self.value_entry.set(" ")

#Default

self.canvas_width = 600

self.canvas_height = 500

self.number_set_x = 40

self.number_set_y = 105

self.block_left = 26

self.block_right = 72

self.block_up = 100

self.block_down = 130

self.down_achieve = 400

self.top_y = 400

self.stack_canvas = Canvas(self.window, width=self.canvas_width,


height=self.canvas_height,bg="purple", relief=RAISED, bd=10)

self.stack_canvas.pack(fill=BOTH)

self.make_stack_container()

self.make_buttons()

self.heading_and_sub_heading()

self.set_index()

#Main heading, sub heading and indicator label set

def heading_and_sub_heading(self):

self.heading_name = Label(self.stack_canvas, text="STACK VISUALIZER", bg="purple",


fg="black", font=("Helvetica",30,"bold","italic"))

self.heading_name.place(x=140,y=30)
self.sub_heading = Label(self.stack_canvas, text="Index number", bg="purple",
fg="#99ff33", font=("Helvetica",20,"bold","italic"))

self.sub_heading.place(x=20,y=300)

self.stack_indicator = Label(self.stack_canvas, text="Stack Container", bg="purple",


fg="#00FF00", font=("Helvetica",20,"bold","italic"))

self.stack_indicator.place(x=180,y=450)

#Make Buttons to access and make top with arrow

def make_buttons(self):

self.push_btn =
Button(self.window,text="PUSH",fg="red",bg="black",font=("Arial",15,"bold"),

relief=RAISED,bd=7,command=self.push_element)

self.push_btn.place(x=30,y=535)

self.pop_btn = Button(self.window, text="POP", fg="red", bg="black", font=("Arial",


15, "bold"),relief=RAISED,bd=7,command=self.pop_data)

self.pop_btn.place(x=500, y=535)

self.top_index = Label(self.window, text="<---- top", fg="red", bg="purple",


font=("Arial", 20, "bold"))

self.top_index.place(x=310, y=self.top_y)

#stack container

def make_stack_container(self):

self.stack_canvas.create_line(250,198,250,400,fill="yellow",width=4)

self.stack_canvas.create_line(250,400,300,400,fill="yellow",width=4)

self.stack_canvas.create_line(300, 198, 300, 400, fill="yellow", width=4)

#Index

def set_index(self):

self.index_neg = Label(self.stack_canvas,text="-
1",fg="blue",bg="purple",font=("Arial",15,"bold"))

self.index_neg.place(x= 215, y=403)

self.index_0 = Label(self.stack_canvas, text="0", fg="blue", bg="purple", font=("Arial",


15, "bold"))

self.index_0.place(x=220, y=365)
self.index_1 = Label(self.stack_canvas, text="1", fg="blue", bg="purple", font=("Arial",
15, "bold"))

self.index_1.place(x=220, y=332)

self.index_2 = Label(self.stack_canvas, text="2", fg="blue", bg="purple", font=("Arial",


15, "bold"))

self.index_2.place(x=220, y=299)

self.index_3 = Label(self.stack_canvas, text="3", fg="blue", bg="purple", font=("Arial",


15, "bold"))

self.index_3.place(x=220, y=266)

self.index_4 = Label(self.stack_canvas, text="4", fg="blue", bg="purple", font=("Arial",


15, "bold"))

self.index_4.place(x=220, y=232)

self.index_5 = Label(self.stack_canvas, text="5", fg="blue", bg="purple", font=("Arial",


15, "bold"))

self.index_5.place(x=220, y=199)

#Push button

def push_element(self):

if len(self.last_label_value_keep) == 6:

messagebox.showerror("Overflow","Stack is full")

else:

self.pop_btn.config(state=DISABLED)

self.push_btn.config(state=DISABLED)

self.element_take_label = Label(self.window,text="Enter the element value",

bg="orange",fg="brown",font=("Arial",12,"bold"))

self.element_take_label.place(x=170,y=536)

self.element_take_entry = Entry(self.window,font=("Arial",13,"bold"),bg="white",

fg="blue",relief=SUNKEN,bd=5, textvar=self.value_entry)

self.element_take_entry.place(x=167,y=560)

self.element_take_entry.focus()
self.add_btn = Button(self.window, text="Add", font=("Arial", 10, "bold"), bg="blue",
fg="red", relief=RAISED, bd=3, padx=3, pady=3, command=lambda:
self.make_block('<Return>'))

self.add_btn.place(x=400, y=560)

self.window.bind('<Return>',self.make_block)

def make_block(self,e):

try:

self.element_take_label.place_forget()

self.element_take_entry.place_forget()

self.add_btn.place_forget()

self.block_make = self.stack_canvas.create_rectangle(self.block_left, self.block_up,


self.block_right, self.block_down, fill="black", width=2, outline="blue")

self.entry_number = Label(self.stack_canvas, textvar=self.number, bg="black",


fg="red", font=("Arial",11,"bold"))

self.entry_number.place(x=self.number_set_x, y=self.number_set_y)

if type(self.value_entry.get()) == int:

self.number.set(self.value_entry.get())

self.push_data()

except:

self.stack_canvas.delete(self.block_make)

self.entry_number.destroy()

self.value_entry.set(" ")

messagebox.showerror("Wrong happen", "Something wrong here....Correctly do


it(Only integer value allowed)")

self.pop_btn.config(state=NORMAL)

self.push_btn.config(state=NORMAL)

pass

def push_data(self):

try:

self.down_achieve -= 28 + self.extra_decrease
self.top_y -= 35

self.top_index.place_forget()

self.top_index.place(x=310, y=self.top_y)

while self.number_set_x<265:

self.stack_canvas.delete(self.block_make)

self.entry_number.place_forget()

self.number_set_x+=2

self.block_left+=2

self.block_right+= 2

self.block_make = self.stack_canvas.create_rectangle(self.block_left,
self.block_up, self.block_right, self.block_down, fill="black", width=2, outline="blue")

self.entry_number.place(x=self.number_set_x, y=self.number_set_y)

self.window.update()

while self.number_set_y < self.down_achieve:

self.stack_canvas.delete(self.block_make)

self.entry_number.place_forget()

self.number_set_y += 2

self.block_up+= 2

self.block_down+= 2

self.block_make = self.stack_canvas.create_rectangle(self.block_left,
self.block_up, self.block_right, self.block_down, fill="black", width=2, outline="blue")

self.entry_number.place(x=self.number_set_x, y=self.number_set_y)

time.sleep(0.01)

self.window.update()

self.reset_with_position_set()

except:

pass

def reset_with_position_set(self):
self.final_set_block = Label(self.window, text=self.value_entry.get(), bg="black",
fg="red",font=("Arial",11,"bold"))

self.final_set_block.place(x=self.number_set_x, y=self.number_set_y)

self.last_label_value_keep.append(self.final_set_block)

self.block_value_counter.append(self.block_make)

self.value_entry.set(" ")

self.entry_number.place_forget()

self.push_btn.config(state=NORMAL)

self.pop_btn.config(state=NORMAL)

self.block_left = 26

self.block_up = 100

self.block_right = 72

self.block_down = 130

self.number_set_x = 40

self.number_set_y = 105

self.extra_decrease =6

def pop_data(self):

if len(self.last_label_value_keep) ==0:

messagebox.showerror("Underflow","Stack is empty")

else:

self.last_label_value_keep.pop().destroy()

self.stack_canvas.delete(self.block_value_counter.pop())

self.top_y += 35

self.top_index.place_forget()

self.top_index.place(x=310, y=self.top_y)

self.down_achieve += 28 + self.extra_decrease

if len(self.last_label_value_keep) == 5:

self.push_btn.config(state=NORMAL)
if __name__ == '__main__':

window= Tk()

window.title("Stack Visualization")

window.geometry("600x600")

window.maxsize(900,900)

window.minsize(900,900)

StackVisualizer(window)

window.mainloop()

SNAPSHOTS:

➢ STACK IMPLEMENTATION

➢ PUSH
➢ SHOWING ERROR, WHEN TRY TO INSERT OTHER THAN INTEGER
➢ STACK FULL
➢ POP
➢ STACK EMPTY

CONCLUSION:
We have learned the implementation, importance, and application of stacks. This is
one of the most important data structures to know and it is extensively asked in the
computer science industry. It is important to have strong knowledge on this topic as it
would give you an edge.

In this, we learn about using of Tkinter module. Python Tkinter is the standard Graphical
User Interface (GUI) that is supported in Python. When Tkinter is used alongside Python, it
results in the creation of GUI very conveniently and quite fast. The main advantage of using
Tkinter is that it has an object-oriented interface. The success of any application or website
depends on how well it interacts with the user, i.e. an attractive graphical interface helps to
create a good application

Reference:
• geekforgeeks: https://www.geeksforgeeks.org/python-gui-tkinter/
• Simplelearn: https://www.simplilearn.com/tutorials/data-structure-tutorial/stacks-
in-data-structures#introduction_to_stack_in_data_structures

THANK YOU

You might also like