0% found this document useful (0 votes)
522 views

Stack: Q1:-WAP To Implement Operations of Stack Without Using Built-In Functions

The document discusses stacks and queues. It defines a stack as an ordered list where insertion and deletion can only occur at one end, called the top. Stacks follow the LIFO principle. Operations on a stack are push and pop. A queue is defined as an ordered list where insertion occurs at the rear and deletion at the front, following the FIFO principle. Common queue operations are insert and delete. Several questions provide code to implement stack and queue operations using lists, built-in functions, and classes. Linked lists are also introduced, noting their advantages over arrays for dynamic memory allocation and size.

Uploaded by

Mahesh
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)
522 views

Stack: Q1:-WAP To Implement Operations of Stack Without Using Built-In Functions

The document discusses stacks and queues. It defines a stack as an ordered list where insertion and deletion can only occur at one end, called the top. Stacks follow the LIFO principle. Operations on a stack are push and pop. A queue is defined as an ordered list where insertion occurs at the rear and deletion at the front, following the FIFO principle. Common queue operations are insert and delete. Several questions provide code to implement stack and queue operations using lists, built-in functions, and classes. Linked lists are also introduced, noting their advantages over arrays for dynamic memory allocation and size.

Uploaded by

Mahesh
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/ 8

Stack

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.

Q1:- WAP to implement operations of Stack without using built-in functions.


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;

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;

#Main function starts from here


print("Program starts")
choice = getChoice();
while choice!=4:
if choice==1:
item=int(input("Enter value to push"));
pushitem(item);
elif choice==2:
if(len(stack)!=0):
item=popitem();
print("Popped item= ",item);
else:
print("Stack Underflow");
elif choice==3:
if(len(stack)!=0):
display();
else:
print("Stack Underflow");
else:
print("Wrong Choice");
choice=getChoice();
print("Stack Operations are Over");

Q2- WAP to implement the operations of Stack using built-in functions.


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;

#Main function starts from here


print("Program starts")
choice = getChoice();
while choice!=4:
if choice==1:
item=int(input("Enter value to push"));
stack.append(item);
elif choice==2:
if(len(stack)!=0):
item=stack.pop();
print("Popped item= ",item);
else:
print("Stack Underflow");
elif choice==3:
if(len(stack)!=0):
print 'Elements of stack are',stack;
else:
print("Stack Underflow");
else:
print("Wrong Choice");
choice=getChoice();
print("Stack Operations are Over");

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.

2. Queue is referred to be as First In First Out list.

3. For example, people waiting in line for a rail ticket form a queue.

4. Operations of Queue are insert and delete.

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;

#Main function starts from here


print("Program starts")
choice = getChoice();
while choice!=4:
if choice==1:
item=int(input("Enter value to insert"));
insertitem(item);
elif choice==2:
if(len(Queue) != 0):
item=deleteitem();
print("Deleted item= ",item);
else:
print("Queue Underflow");
elif choice==3:
if(len(Queue) != 0):
display();
else:
print("Queue Underflow");
else:
print("Wrong Choice");
choice=getChoice();
print("Queue Operations are Over");

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;

#Main function starts from here


print("Program starts")
choice = getChoice();
while choice!=4:
if choice==1:
item=int(input("Enter value to insert"));
Queue.append(item);
elif choice==2:
if(len(Queue) != 0):
#item=Queue.get();
item=Queue.pop(0);
print("Deleted item= ",item);
else:
print("Queue Underflow");
elif choice==3:
if(len(Queue) != 0):
print 'Elements of Queue are',Queue;
else:
print("Queue Underflow");
else:
print("Wrong Choice");
choice=getChoice();
print("Queue Operations are Over");

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.

Why use linked list over array?

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.

Array contains following limitations:

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.

Q7:- WAP to implement all operations of Single Linked List.


#Independent Node creation
class Node:
def __init__(self,data):
self.data = data;
self.next = None;
#Linked list operation starts
class Linkedlist(object):
def __init__(self):
self.start=None;
self.size=0;#To count total nodes in the list
def insertbegin(self,data):
newnode = Node(data);#New node is created
self.size = self.size + 1;
if(self.start == None):
self.start = newnode;
else:
newnode.next = self.start;
self.start = newnode;
def deleteBegin(self):
if(self.start == None):
print('The list is empty');
else:
self.size = self.size - 1;
print 'The deleted value= ', self.start.data;
if(self.start.next == None):
self.start = None;
else:
temp = self.start;
self.start = temp.next;
del temp;
def traverse(self):
if(self.start == None):
print('The list is empty');
else:
print('The values of the list are');
temp=self.start;
while(temp != None):
print(temp.data);
temp = temp.next;
def totalnode(self):
if(self.start == None):
print('The list is empty');
else:
print 'The total number of elements are ',self.size;

#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();

The following operations can be implemented using Single linked list.


#Insert end, insert after given node, insert at specific location
#Delete from end, delete after a given node, delete from a specific location

With Regards
Samaleswari Prasad Nayak
Assistant professor
Department of CSE
Silicon Institute of Technology
Bhubaneswar
9658663103

You might also like