[go: up one dir, main page]

0% found this document useful (0 votes)
217 views9 pages

Dsa Lab 07

The document describes a laboratory manual for implementing stacks and queues using Python. It defines stacks and queues, explaining that stacks follow LIFO (last in, first out) while queues follow FIFO (first in, first out). It provides procedures for performing push, pop, and display operations on stacks and enqueue, dequeue, and display operations on queues. The lab tasks involve writing programs to implement stacks and queues using arrays and to perform operations like reversing a list and calculating factorials using stacks.

Uploaded by

Shahood Mustafa
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)
217 views9 pages

Dsa Lab 07

The document describes a laboratory manual for implementing stacks and queues using Python. It defines stacks and queues, explaining that stacks follow LIFO (last in, first out) while queues follow FIFO (first in, first out). It provides procedures for performing push, pop, and display operations on stacks and enqueue, dequeue, and display operations on queues. The lab tasks involve writing programs to implement stacks and queues using arrays and to perform operations like reversing a list and calculating factorials using stacks.

Uploaded by

Shahood Mustafa
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
You are on page 1/ 9

LABORATORY MANUAL  Fall 2019  Engr.

Umair Naeem Khan

CL-210 DATA STRUCTURE & ALGORITHM LAB


LAB 07
IMPLEMENTATION OF STACK
AND QUEUE

________________________________________ __________ ___


STUDENT NAME ROLL NO SEC

______________________________________
LAB ENGINEER'S SIGNATURE & DATE

MARKS AWARDED: /10


___________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES
(NUCES), KARACHI

Prepared by: Engr. Umair Naeem Khan Version: 2.0


Verified by: Dr. Burhan Khan Date: 4th Oct 2019
[IMPLEMENTATION OF STACK AND QUEUE] [Fall-2019 DSA Lab] LAB: 07

Lab Session 07: IMPLEMENTATION OF STACK AND QUEUE


OBJECTIVES:
1. Write a Python program to implement Stack and its operations using list.
2. Write a Python program to implement Queue and its operations using list.

STACK:
Stack is a linear data structure which works under the principle of last in first out, an
array is a random access data structure, where each element can be accessed directly and in
constant time. A typical illustration of random access is a book - each page of the book can be
open independently of others. Random access is critical to many algorithms, for example
binary search.

A linked list is a sequential access data structure, where each element can be accesed only in
particular order.

A typical illustration of sequential access is a roll of paper or tape - all prior material must be
unrolled in order to get to data you want.

In this note we consider a subcase of sequential data structures, so-called limited access data
structure.

What is a stack?
A stack is a container of objects that are inserted and removed according to the last-in
first-out (LIFO) principle. In the pushdown stacks only two operations are allowed:
Push the item into the stack, and pop the item out of the stack.
A stack is a limited access data structure - elements can be added and removed from the stack
only at the top. Push adds an item to the top of the stack, pop removes the item from the top.

Instructor: Umair Naeem Khan National University of Computer & Emerging Sciences, Karachi Page 1 of 8
LAB: 07 [Fall 2019 - DSA Lab] [Introduction to Data Structure and Algorithm]
A helpful analogy is to think of a stack of books; you can remove only the top book,
also you can add a new book on the top.
A stack is a recursive data structure. Here is a structural definition of a Stack: a stack is either
empty or it consists of a top and the rest which is a stack.

Applications
The simplest application of a stack are:
1. Reverse a word. You push a given word to stack - letter by letter - and then pop
letters from the stack.
2. Another application is an "undo" mechanism in text editors; this operation is
accomplished by keeping all text changes in a stack.
Backtracking:
This is a process when you need to access the most recent data element in a series of
elements. Think of a labyrinth or maze - how do you find a way from an entrance to an exit?
Once you reach a dead end, you must backtrack. But backtrack to where? To the previous
choice point. Therefore, at each choice point you store on a stack all possible choices. Then
backtracking simply means popping a next choice from the stack.
In the standard library of classes, the data type stack is an adapter class, meaning that a
stack is built on top of other data structures. The underlying structure for a stack could be an
array, a vector, an ArrayList, a linked list, or any other collection. Regardless of the type of the
underlying data structure, a Stack must implement the same functionality.
Another implementation requirement (in addition to the above interface) is that all stack
operations must run in constant time O(1). Constant time means that there is some constant k
such that an operation takes k nanoseconds of computational time regardless of the stack size.

Procedure for Stack:

if (top==MAX), display Stack overflow. Otherwise reading the data


PUSH and making stack

TOP data and incrementing the top value by doing top++


if (top==0), display Stack underflow. Otherwise printing the element
POP
at the top of the stack and decrementing the top value by doing the top.
If (top==0), display Stack is empty. Otherwise printing the elements in
DISPLAY
the stack from stack [0] to stack [top].

Page 2 of 8 National University of Computer & Emerging Sciences, Karachi Instructor: Umair Naeem Khan
[IMPLEMENTATION OF STACK AND QUEUE] [Fall-2019 DSA Lab] LAB: 07
Example:
Consider a stack with 5 elements capacity. When an element is added to a stack, the
operation is performed by Push().

When an element is taken off from the stack, the operation is performed by Pop().

QUEUE:
Queue is a linear data structure which works under the principle of first in first out. A
queue is a container of objects (a linear collection) that are inserted and removed
according to the first-in first-out (FIFO) principle. An excellent example of a queue is a
line of students in the food court of the UC. New additions to a line made to the back of
the queue, while removal (or serving) happens in the front. In the queue only two
operations are allowed enqueue and dequeue.
1. Enqueue means to insert an item into the back of the queue.
2. Dequeue means removing the front item. The picture demonstrates the FIFO
access.
The difference between stacks and queues is in removing. In a stack we remove
the item the most recently added; in a queue, we remove the item the least recently
added.

Instructor: Umair Naeem Khan National University of Computer & Emerging Sciences, Karachi Page 3 of 8
LAB: 07 [Fall 2019 - DSA Lab] [Introduction to Data Structure and Algorithm]
Procedure for Queue using List
if (rear==MAX), display Queue is full. Else reading data and inserting
Insertion at queue [rear], and doing rear++.

if (front==rear), display Queue is empty .Else printing element at queue


Deletion
[front] and doing front++.
if (front==rear) ,display No elements in the queue .Else printing the
Display
elements from queue[front] to queue[rear].

LAB TASKS
1. Write a program to implement stack and its operations using arrays.
2. Formulate a program to reverse a list of numbers using stack.
3. Write a program to find the factorial of a number using stack.
4. Compose a program to implement Queue operations using arrays.
Questions
1. List out the applications of stack?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

2. Define a stack?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

3. Stack data structure uses which principle?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

4. Stack belongs to which type of data structure?

Page 4 of 8 National University of Computer & Emerging Sciences, Karachi Instructor: Umair Naeem Khan
[IMPLEMENTATION OF STACK AND QUEUE] [Fall-2019 DSA Lab] LAB: 07
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

5. What do you mean by stack underflow?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

6. What do you mean by stack overflow?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

7. Define a circular queue?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

8. Which principle is followed in queue?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

9. List out the applications of queue?

_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
____________________________________________________________________

Instructor: Umair Naeem Khan National University of Computer & Emerging Sciences, Karachi Page 5 of 8
LAB: 07 [Fall 2019 - DSA Lab] [Introduction to Data Structure and Algorithm]
Program for implementing stack
top=0
mymax=eval(input("enter maximum size of stack:"))
…………………………………………………………………………………………………………………………………………………………………
def createStack():
stack=[ ]
return stack
…………………………………………………………………………………………………………………………………………………………………
def isEmpty(stack):
return len(stack)==0
…………………………………………………………………………………………………………………………………………………………………
def push(stack,item):
stack.append(item)
print("pushed to stack",item)
…………………………………………………………………………………………………………………………………………………………………
def pop(stack):
if isEmpty(stack):
return"stack underflow"
return stack.pop()
stack=createStack()
…………………………………………………………………………………………………………………………………………………………………
while True:
print("\n MAIN MENU \n")
print("1.push")
print("2.pop")
print("3.display")
print("4.quit")
ch=int(input("Enter your choice:"))
if ch==1:
if top<mymax:
item=input("enter any element:")
push(stack,item)
top+=1
else:
print("stack overflow")
elif ch==2:
print(pop(stack))
elif ch==3:
print("CURRENT STACK IS -->>",stack)
else:
print("exit")
break

Page 6 of 8 National University of Computer & Emerging Sciences, Karachi Instructor: Umair Naeem Khan
[IMPLEMENTATION OF STACK AND QUEUE] [Fall-2019 DSA Lab] LAB: 07
Program for implementing Linear Queue using list
front=0
rear=0
mymax=eval(input("Enter maximum size of queue :"))
…………………………………………………………………………………………………………………………………………………………………
def createQueue():
queue =[]
return queue
…………………………………………………………………………………………………………………………………………………………………
def isEmpty(queue):
return len(queue)==0
…………………………………………………………………………………………………………………………………………………………………
def enqueue(queue, item):
queue.append(item)
print("Enquened to queue", item)
…………………………………………………………………………………………………………………………………………………………………
def dequeue(queue):
if isEmpty(queue):
return "Queue is empty"
item = queue[0]
del queue[0]
return item
…………………………………………………………………………………………………………………………………………………………………
queue=createQueue()
while True:
print("\n MAIN MENU \n")
print("1.Enqueue")
print("2.Dequeue")
print("3.Display")
print("4.Quit")

ch=int(input("Enter your choice [1-4]: "))


if ch==1:
if rear<mymax:
item=input("Enter any elements:")
enqueue(queue,item)
rear+=1
else:
print("Queue is full")
elif ch==2:
print(dequeue(queue))
elif ch==3:
print("Current Queue is ->> ",queue)
else:
print("Exit")
break

Instructor: Umair Naeem Khan National University of Computer & Emerging Sciences, Karachi Page 7 of 8
LAB: 07 [Fall 2019 - DSA Lab] [Introduction to Data Structure and Algorithm]
Lab Report:
(It is recommended to write Lab Report in bullet Form)
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________
________________________________________________________________________________________

Page 8 of 8 National University of Computer & Emerging Sciences, Karachi Instructor: Umair Naeem Khan

You might also like