+ All Categories
Home > Documents > gnanavelmeblog.files.wordpress.com  · Web viewA double-ended queue is a special type of queue...

gnanavelmeblog.files.wordpress.com  · Web viewA double-ended queue is a special type of queue...

Date post: 30-Nov-2018
Category:
Upload: lamliem
View: 216 times
Download: 0 times
Share this document with a friend
17
RAJALAKSHMI ENGINEERING COLLEGE, CHENNAI 602 105 CONTINUOUS ASSESMENT TEST I III Semester – B.E. Electronics and Communication Engineering CS17201 – Data Strutures Using Python ANSWER KEY Date: Time: 90 min Max Marks: 50 Answer ALL Questions [Part A - 6x2=12 Marks] 1. 1 Write the routines to insert and delete elements from stack. Routine to Insert: def push(self, ele): if self.isFull(): print("Stack Overflow...!") else: self.stack.append(ele) Routine to Delete: def pop(self): if self.isEmpty(): print("Stack Underflow...!") else: print(self.stack.pop()) 1. 2 Convert the infix expression to postfix expression: a+b*c+ (d*e) Postfix expression: abc*+de*+ 1. 3 List out the operations on queue along with the exception conditions. The basic operations on a queue are: Enqueue - which inserts an element at the end of the list (called rear). Dequeue - which deletes (and returns) the element at the start of the list (known as front). The exceptional conditions are: Overflow - Attempt to insert an element when the queue is full Underflow - Attempt to delete an element, when the queue is empty 1. 4 Define circular queue. What are the advantages of circular queue? A circular queue is a queue whose start and end locations are logically connected with each other. The start location comes after the end location. If we continue to
Transcript

RAJALAKSHMI ENGINEERING COLLEGE, CHENNAI 602 105 CONTINUOUS ASSESMENT TEST I

III Semester – B.E. Electronics and Communication EngineeringCS17201 – Data Strutures Using Python

ANSWER KEY

Date: Time: 90 min Max Marks: 50

Answer ALL Questions [Part A - 6x2=12 Marks]

1.1 Write the routines to insert and delete elements from stack.Routine to Insert:def push(self, ele):

if self.isFull(): print("Stack Overflow...!") else: self.stack.append(ele)Routine to Delete:def pop(self): if self.isEmpty(): print("Stack Underflow...!") else: print(self.stack.pop())

1.2 Convert the infix expression to postfix expression: a+b*c+(d*e)

Postfix expression: abc*+de*+

1.3 List out the operations on queue along with the exception conditions.The basic operations on a queue are:Enqueue - which inserts an element at the end of the list (called rear).Dequeue - which deletes (and returns) the element at the start of the list (known as front).The exceptional conditions are:Overflow - Attempt to insert an element when the queue is full Underflow - Attempt to delete an element, when the queue is empty

1.4 Define circular queue. What are the advantages of circular queue?A circular queue is a queue whose start and end locations are logically connected with each other. The start location comes after the end location. If we continue to add elements in a circular queue till its end location, then after the end location has been filled, the next element will be added at the beginning of the queue.Advantages:Circular queues remove one of the main disadvantages of array implemented queues in which a lot of memory space is wasted due to inefficient utilization.

1.5 List out the differences between stack and queue data structures.

BASIS FOR COMPARISON STACK QUEUE

Working principle LIFO (Last in First out)

FIFO (First in First out)

Structure Same end is used to One end is used for insertion, i.e.,

insert and delete elements.

rear end and another end is used for deletion of elements, i.e., front end.

Number of pointers used

One Two (In simple queue case)

Operations performed Push and Pop Enqueue and dequeue

Examination of empty condition

Top == -1 Front == -1 || Front == Rear + 1

Examination of full condition

Top == Max - 1 Rear == Max - 1

Variants It does not have variants.

It has variants like circular queue, priority queue, doubly ended queue.

Implementation Simpler Comparatively complex

1.6 List five applications of stack.Balancing symbolsInfix to postfix conversionEvaluating postfix expressionFunction callsTowers of Hanoi8 queens problemPage-visited history in a Web browser (Back Buttons)Undo sequence in a text editor

Answer ALL Questions [Part B – 2x10=20 Marks]

1.7 a How do you push and pop elements into array stack? Explain with an example. (10)Stack: stack is a list with the restriction that insertions and deletions can be performed in only one position, namely, the end of the list, called the top. It follows Last-In-First-Out (LIFO) principle.Push: To push an element X onto the stack, top pointer is incremented by 1 and then set stack.append(X).Pop: To pop an element from the stack, the stack.pop() value is returned and the top pointer is decremented by 1.Program:class Stack: def __init__(self): self.stack = [] self.limit = 5 def isFull(self): if len(self.stack) == self.limit: return True else: return False def isEmpty(self):

if len(self.stack) == 0: return True else: return False def push(self, ele): if self.isFull(): print("Stack Overflow...!") else: self.stack.append(ele) def pop(self): if self.isEmpty(): print("Stack Underflow...!") else: print(self.stack.pop())s = Stack()while True: print("1. PUSH 2. POP 3. DISPLAY 4. EXIT") ch = int(input("Enter your choice : ")) if ch == 1: ele = int(input("Enter the element : ")) s.push(ele) elif ch == 2: s.pop() elif ch == 3: s.display() else: breakOutput1. PUSH 2. POP 3. DISPLAY 4. EXITEnter your choice : 1Enter the element : 101. PUSH 2. POP 3. DISPLAY 4.EXITEnter your choice : 1Enter the element : 201. PUSH 2. POP 3. DISPLAY 4. EXITEnter your choice : 2201. PUSH 2. POP 3. DISPLAY 4. EXITEnter your choice : 3101. PUSH 2. POP 3. DISPLAY 4. EXITEnter your choice : 2101. PUSH 2. POP 3. DISPLAY 4. EXITEnter your choice : 2Stack Underflow...!1. PUSH 2. POP 3. DISPLAY 4. EXITEnter your choice : 4

[Or]

b Write a function to insert and delete an element in a queue, in which the queue is implemented as an array. (10)A queue is list with the restrictions that insertion is done at one end (rear), whereas deletion is performed at the other end (front). It follows First-In-First-

Out (FIFO) principle.Example: Waiting line in a reservation counter.Program:class Queue: def __init__(self): self.queue = [] self.limit = 5 self.front = None self.rear = None def isFull(self): if self.rear == self.limit - 1: return True else: return False def isEmpty(self): if self.front == None: return True else: return False def enqueue(self, ele): if self.isFull(): print("Queue Overflow...!") else: if self.front == None and self.rear == None: self.front = self.rear = 0 else: self.rear = self.rear + 1 self.queue.append(ele) def dequeue(self): if self.isEmpty(): print("Queue Underflow...!") else: print(self.queue.pop(0)) if self.front == self.rear: self.front = self.rear = None else: self.front = self.front + 1 def display(self): if self.isEmpty(): print("Queue Underflow...!") else: for i in self.queue: print(i, end = " ") print()q = Queue()while True: print("1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXIT") ch = int(input("Enter your choice : ")) if ch == 1: ele = int(input("Enter the element : ")) q.enqueue(ele) elif ch == 2: q.dequeue() elif ch == 3:

q.display() else: break

OUTPUT:1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXITEnter your choice : 1Enter the element : 101. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXITEnter your choice : 1Enter the element : 201. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXITEnter your choice : 310 20 1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXITEnter your choice : 2101. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXITEnter your choice : 2201. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXITEnter your choice : 2Queue Underflow...!1. ENQUEUE 2. DEQUEUE 3. DISPLAY 4. EXITEnter your choice : 4

1.8 aHow will you evaluate a postfix expression? Convert the infix expression a+b*c+(d*e+f)*g to its equivalent postfix expression (a=3,b=2,c=1,d=4,e=5,f=8,g=7). (10)

To evaluate an arithmetic expression, first convert the given infix expression to postfix expression and then evaluate the postfix expression using stack.Equivalent postfix expression: abc*+de*f+g*+

Program:class Stack: def __init__(self): self.stack = [] def push(self, ele): self.stack.append(ele) def pop(self): return self.stack.pop()epe = Stack()exp = input("Enter the postfix expression : ")for token in exp: if token in "+-*/": op2 = epe.pop() op1 = epe.pop() if token == "+": epe.push(op1 + op2) elif token == "-": epe.push(op1 - op2) elif token == "*": epe.push(op1 * op2)

elif token == "/": epe.push(op1 / op2) else: print("Enter the value for", token, ":")val = int(input())epe.push(val)print("The result is :", epe.pop())

Evaluated PostFix Expression result: 61

[Or]b What is a deque? Write a program to implement deque operations. (10)

A double-ended queue is a special type of queue that allows insertion and deletion of elements at both ends, i.e., front and rear. A double-ended queue can be referred as a linear list of elements in which insertion and deletion of elements takes place at its two ends but not in the middle. This is the reason why it is termed as double-ended queue or deque.Operations in Deque:1. EnqueueFront : Insertion at front end of the queue.2. DequeueFront : Deletion from front end of the queue.3. EnqueueRear : Insertion at rear end of the queue.4. DequeueRear : Deletion from rear end of the queue.

Program:class DoubleEndedQueue: def __init__(self): self.doubleendedqueue = [] self.limit = 5 self.front = None self.rear = None def enqueueRear(self, ele): if self.rear == self.limit - 1: print("Queue Overflow...!") else: if self.front == None and self.rear == None: self.front = self.rear = 0 else: self.rear = self.rear + 1 self.doubleendedqueue.append(ele) def enqueueFront(self, ele): if self.front == 0: print("Cannot Insert at Front...!") else: if self.front == None and self.rear == None: self.front = self.rear = 0 else: self.front = self.front - 1 self.doubleendedqueue.insert(0, ele) def dequeueFront(self): if self.front == None: print("Queue Underflow...!") else: print(self.doubleendedqueue.pop(0)) if self.front == self.rear:

self.front = self.rear = None else: self.front = self.front + 1 def dequeueRear(self): if self.rear == None: print("Queue Underflow...!") else: print(self.doubleendedqueue.pop()) if self.front == self.rear: self.front = self.rear = None else: self.rear = self.rear – 1 def display(self): if len(self.doubleendedqueue) == 0: print("Queue Underflow...!") else: for i in self.doubleendedqueue: print(i, end = " ") print()dq = DoubleEndedQueue()while True: print("1. ENQUEUE-REAR 2. ENQUEUE-FRONT") print("3. DEQUEUE-FRONT 4. DEQUEUE-REAR") print("5. DISPLAY 6. EXIT") ch = int(input("Enter your choice : ")) if ch == 1: ele = int(input("Enter the element : ")) dq.enqueueRear(ele) elif ch == 2: ele = int(input("Enter the element : ")) dq.enqueueFront(ele) elif ch == 3: dq.dequeueFront() elif ch == 4: dq.dequeueRear() elif ch == 5: dq.display() else: break

Answer ALL Questions [Part A – 4x2=8 Marks]

2.1 Define a list. Mention the three operations performed on a list.List is an ordered set of elements. For any list except the empty list, we say that Ai+1 follows (or succeeds) Ai(i<N) and that Ai-1 precedes Ai (i>1). The first element of the list is A1, and the last element is AN.Operations:printList – contents of the list is displayedmakeEmpty – makes the list emptyfind – returns the position of the first occurrence of an iteminsert – insert some element from some position in the listremove – remove some element from some position in the listfindKth – returns the element in some position (specified as an argument)

next – return the position of the successorprevious – return the position of the predecessor

2.2 List down the difference between arrays and linked lists.Arrays are index based data structure where each element associated with an index. On the other hand, Linked list relies on references where each node consists of the data and the references to the previous and next element.

2.3 Explain the two ways of implementing a list.Lists are implemented either using:Array:  data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.Linked list: The linked list consists of a series of structures, which are not necessarily adjacent in memory. Each structure contains the element and a pointer to a structure containing its successor. This is the next pointer. The last cell's next pointer points to NULL.

2.4 What are the advantages and disadvantages of linked lists over arrays?ADVANTAGES OF LINKED LISTS1. Linked lists facilitate dynamic memory management by allowing elements to be addedor deleted at any time during program execution.2. The use of linked lists ensures efficient utilization of memory space as only that muchamount of memory space is reserved as is required for storing the list elements.3. It is easy to insert or delete elements in a linked list, unlike arrays, which requireshuffling of other elements with each insert and delete operation.DISADVANTAGES OF LINKED LISTS1. A linked list element requires more memory space in comparison to an array elementbecause it has to also store the address of the next element in the list.2. Accessing an element is a little more difficult in linked lists than arrays because unlikearrays, there is no index identifier associated with each list element. Thus, to access alinked list element, it is mandatory to traverse all the preceding elements.

Answer ALL Questions [Part B – 1x10=10 Marks]

2.5 a What are singly linked lists? Explain with example the operations performed on singly linked lists. (10)A singly linked list is a linked list in which each node contains only one link field pointing to the next node in the list.

Fig. 1 Linked List

Fig. 2 Linked List With HeaderOperations - Algorithms:traverse()Step 1 : Start.Step 2 : If head = None, then goto Step 3 else goto Step 4.Step 3 : Display “List is empty” and goto Step 8.Step 4 : Set position = head.Step 5 : Repeat the Steps 6-7 until position != None.Step 6 : Display position.element.

Step 7 : Set position = position.next.Step 8 : Stop.

insertBeg(ele)Step 1 : Start.Step 2 : Set newnode = Node(ele).Step 3 : If head = None, then goto Step 4 else goto Step 5.Step 4 : Set head = newnode and goto Step 7.Step 5 : Set newnode.next = head.Step 6 : Set head = newnode.Step 7: Stop.

insertEnd(ele)Step 1 : Start.Step 2 : Set newnode = Node(ele).Step 3 : If head = None, then goto Step 4 else goto Step 5.Step 4 : Set head = newnode and goto Step 9.Step 5 : Set position = head.Step 6 : Repeat the Step 7 until position.next != None.Step 7 : Set position = position.next.Step 8 : Set position.next = newnode.Step 9 : Stop.

insertMid(pos, ele)Step 1 : Start.Step 2 : Set newnode = Node(ele).Step 3 : Set position = head.Step 4 : Repeat Step 5 to until position.next != None and position.element != pos.Step 5 : Set position = position.next.Step 6 : Set newnode.next = position.next.Step 7 : Set position.next = newnode.Step 8 : Stop.

deleteBeg()Step 1 : Start.Step 2 : If head = None, then goto Step 3 else goto Step 4.Step 3 : Display “List is Empty” and g to Step 6.Step 4 : Display “The deleted item is”, head.element.Step 5 : Set head = head.next.Step 6 : Stop.

deleteEnd(List)Step 1 : Start.Step 2 : If head = None, then goto Step 3 else goto Step 4.Step 3 : Display “List is Empty” and go to Step 9.Step 4 : Set position = head.Step 5 : Repeat the Step 6 until position.next.next != None.Step 6 : Set position = position.next.Step 7 : Display “The deleted item is”, position.next.element.Step 8 : Set position.next = None.Step 9 : Stop.

deleteMid(ele)Step 1 : Start.

Step 2 : If head = None, then goto Step 3 else goto Step 4.Step 3 : Display “List is Empty” and goto Step 9.Step 4 : Set position = head.Step 5 : Repeat Step 6 to until position.next != None and position.next.element != ele.Step 6 : Set position = position.next.Step 7 : Display “The deleted item is”, position.next.element.Step 8 : Set position.next = position.next.next.Step 9 : Stop.

Program:class Node: def __init__(self, ele): self.element = ele self.next = Noneclass SLL: def __init__(self): self.head = None def insertBeg(self, ele): newnode = Node(ele) if self.head == None: self.head = newnode else: newnode.next = self.head self.head = newnode def insertMid(self, pos, ele): newnode = Node(ele) position = self.head while position.next != None and position.element != pos: position = position.next newnode.next = position.next position.next = newnode def insertEnd(self, ele): newnode = Node(ele) if self.head == None: self.head = newnode else: position = self.head while position.next != None: position = position.next position.next = newnode def deleteBeg(self): if self.head == None: print("List is empty...!") else: print("The deleted item is", self.head.element) self.head = self.head.next def deleteMid(self, ele): if self.head == None: print("List is empty...!") else: position = self.head while position.next != None and position.next.element != ele: position = position.next

print("The deleted item is", position.next.element) position.next = position.next.next def deleteEnd(self): if self.head == None: print("List is empty...!") else: position = self.head while position.next.next != None: position = position.next print("The deleted item is", position.next.element) position.next = None def find(self, ele): position = self.head flag = False while position != None: if position.element == ele: flag = True break position = position.next if flag: print("Element found...!") else: print("Element not found...!") def traverse(self): if self.head == None: print("List is empty...!") else: position = self.head while position != None: print(position.element, end = " ") position = position.next print()sll = SLL()while True: print("1.Insert Beg 2.Insert Middle 3.Insert End") print("4.Delete Beg 5.Delete Middle 6.Delete End") print("7.Find 8.Traverse 9.Exit") ch = int(input("Enter your choice : ")) if ch == 1: ele = int(input("Enter the element : ")) sll.insertBeg(ele) elif ch == 2: pos = int(input("Enter the position element : ")) ele = int(input("Enter the element : ")) sll.insertMid(pos, ele) elif ch == 3: ele = int(input("Enter the element : ")) sll.insertEnd(ele) elif ch == 4: sll.deleteBeg() elif ch == 5: ele = int(input("Enter the element : ")) sll.deleteMid(ele) elif ch == 6:

sll.deleteEnd() elif ch == 7: ele = int(input("Enter the element : ")) sll.find(ele) elif ch == 8: sll.traverse() elif ch == 9: break

[Or]

b How are circular linked lists implemented? (10)

In circular linked lists, the pointer of the last node points to the first node. Circular

linked lists can be implemented in two ways as singly and doubly linked lists with

or without headers.

Program:


Recommended