Stack: Q1:-WAP To Implement Operations of Stack Without Using Built-In Functions
Stack: Q1:-WAP To Implement Operations of Stack Without Using Built-In Functions
1. Stack is an ordered list in which, insertion and deletion can be performed only at one
end that is called top.
2. Stack is a recursive data structure having pointer to its top element.
3. Stacks are sometimes called as Last-In-First-Out (LIFO) lists i.e. the element which is
inserted first in the stack, will be deleted last from the stack.
4. Operations of Stack are push and pop.
def getChoice():
print("Menu\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 4.EXIT")
choice = int(input("Enter your choice"));
return choice;
def pushitem(item):
stack.append(item);
def popitem():
global stack;
item=stack[-1];
del stack[-1];
return item;
def display():
print 'Elements of stack are',stack;
def getChoice():
print("Menu\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 4.EXIT")
choice = int(input("Enter your choice"));
return choice;
Q3- WAP to implement the operations of Stack using class and object.
class Stack:
def __init__(self):
self.stackarr=[];
def push(self,item):
self.stackarr.append(item);
def pop(self):
item=self.stackarr[-1];
del self.stackarr[-1];
return item;
def display(self):
print('Values of stack are ',self.stackarr);
def getChoice():
print("Menu\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 4.Exit")
choice = int(input("Enter your choice"));
return choice
#Main function starts from here
print("Program starts")
choice = getChoice();
ob = Stack();
while choice!=4:
if choice==1:
item=int(input("Enter value to push"));
ob.push(item);
elif choice==2:
if(len(ob.stackarr) != 0):
item=ob.pop();
print("Popped item is ", item);
else:
print('Stack Underflow');
elif choice==3:
if(len(ob.stackarr) != 0):
ob.display();
else:
print('Stack Underflow');
else:
print("Invalid choice, please choose again")
print("\n")
choice = getChoice();
Queue
1. A queue can be defined as an ordered list which enables insert operations to be performed
at one end called REAR and delete operations to be performed at another end
called FRONT.
3. For example, people waiting in line for a rail ticket form a queue.
Q4:- WAP to implement operations of Linear Queue without using built-in functions.
Queue=[];
def getChoice():
print("Menu\n 1.INSERT\n 2.DELETE\n 3.DISPLAY\n 4.EXIT")
choice = int(input("Enter your choice"));
return choice;
def insertitem(item):
Queue.append(item);
def deleteitem():
global Queue;
item=Queue[0];
Queue = Queue[1:len(Queue)];
return item;
def display():
print 'Elements of Queue are',Queue;
Q5- WAP to implement the operations of Linear Queue using built-in functions.
Queue=[];
def getChoice():
print("Menu\n 1.INSERT\n 2.DELETE\n 3.DISPLAY\n 4.EXIT")
choice = int(input("Enter your choice"));
return choice;
Q6:- WAP to implement operations of Linear Queue using class and object.
class Queue:
def __init__(self):
self.queuearr = [];
def insert(self,item):
self.queuearr.append(item);
def deletequeue(self):
item = self.queuearr[0];
del self.queuearr[0];
return item;
def display(self):
print 'Elements of Queue are',self.queuearr;
def getChoice():
print("Menu\n 1.INSERT\n 2.DELETE\n 3.DISPLAY\n 4.EXIT")
choice = int(input("Enter your choice"));
return choice;
#Main function starts from here
print("Program starts")
choice = getChoice();
ob = Queue();
while choice!=4:
if choice==1:
item=int(input("Enter value to insert"));
ob.insert(item);
elif choice==2:
if(len(ob.queuearr) != 0):
item=ob.deletequeue();
print("Deleted item= ",item);
else:
print("Queue Underflow");
elif choice==3:
if(len(ob.queuearr) != 0):
ob.display();
else:
print("Queue Underflow");
else:
print("Wrong Choice");
choice=getChoice();
print("Queue Operations are Over");
Linked List
o Linked List can be defined as collection of objects called nodes that are randomly
stored in the memory.
o A node contains two fields i.e. data stored at that particular address and the pointer
which contains the address of the next node in the memory.
o The last node of the list contains pointer to the null.
However, Array has several advantages and disadvantages which must be known in order to
decide the data structure which will be used throughout the program.
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand
the size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory. Inserting
any element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array. Using
linked list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously
stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available
memory space.
#Menu driven
def getChoice():
print("Menu\n 1.Insert at Beginning\n 2.Delete from Beginning\n 3.Traversal\n 4.Total
Number Elements\n 12.Exit")
choice = int(input("Enter your choice"));
return choice
#Main function starts from here
print("Program starts")
choice = getChoice();
ob = Linkedlist();
while choice!=12:
if choice==1:
item=int(input("Enter value to insert"));
ob.insertbegin(item);
elif choice==2:
ob.deleteBegin();
elif choice==3:
ob.traverse();
elif choice==4:
ob.totalnode();
else:
print("Invalid choice, please choose again")
print("\n")
choice = getChoice();
With Regards
Samaleswari Prasad Nayak
Assistant professor
Department of CSE
Silicon Institute of Technology
Bhubaneswar
9658663103