Data Structure

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

DATA STRUCTURE

INTRODUCTION
We know that computers store and process data with an extra ordinary speed and
accuracy. So, it is highly essential that the data is stored efficiently and can be
accessed fast. Also, the processing of data should happen in the smallest possible
time but without losing the accuracy.

Data structures deal with how the data is organized and held in the memory when a program
processes it.
• Data structures are fundamental concepts of computer science which helps in writing
efficient programs in any language.
• A data structure is a named group of data of different data types which is stored in a specific
way and can be processed as a single unit.
• A data structure has well-defined operations, behavior and properties.

Data type vs Data structure

• A data type defines a set of values along with well-defined operations stating its input-output
behavior.
• A data structure is a physical implementation that clearly defines a way of storing, accessing,
manipulating data stored in a data structure.

For example, List as a data type which can store heterogeneous data type of elements,
but when implemented as data structure its behavior is clearly prescribed (searching,
sorting etc.)
List

• Lists in Python are used to store collection of heterogeneous items.


• These are mutable, which means that you can change their content without
changing their identity.
• You can recognize lists by their square brackets [ and ] that hold elements separated
by a comma.
• Lists are built into Python: you do not need to invoke them separately.

Stack
A stack is a container of objects that are inserted and removed according to the Last-InFirst-
Out (LIFO) concept.

• Think of a scenario where at a dinner party where there is a


stack of plates, plates are always added or removed from
the top of the pile.
• In computer science, this concept is used for evaluating
expressions and syntax parsing, scheduling
algorithms/routines, etc.

Linear List
• A linear data structure is that those elements form a
sequence.
• When elements of linear structure are homogeneous and
are represented in memory by means of sequential memory
locations, these linear structures are called arrays.  Linear
List size: Length=UB-LB+1
Stack
Stack is a linear/sequence structure in which insertion and deletion can take place only at
one end, i.e., stack’s top. Because of this, stack is called LIFO (Last in First out) data

structure. For example, a pile of books, a stack of coins where you can remove only the
top book or the coin placed at the top.

Implementation of stack using list


• The implementation of stack using list in Python is the easiest of all programming language.
• Basic operations performed on stack are:
1. Creating a stack
2. Push/Adding elements to the stack
3. Checking for empty stack
4. Pop/Deleting elements from a stack
5. Traversal/Displaying a stack

List methods used and Important things to remember


1) list.append(element) – It is used to implement push operations(used to append
or add elements at the end of the list)
2) list.pop() –It is used to implement pop operations(removing elements at the
end) 3) list[::-1]-List slicing is used to print the elements in the reverse order from top
position to list[0]
4) top=len(list)-1 (length of list -1)
5) stack - LIFO(Last In First Out)
6) Push and Pop through one end(Top)
Applications of Stack

1. Reversing a word/line: This can be accomplished by pushing each character on to a stack as it


is read. When the line is finished, characters are popped off the stack and they will come off in the
reverse order.

2.The compilers use stacks to store the previous state of a program when a function is
called during recursion.

2. Backtracking is a form of recursion. But it involves choosing only one option out of
possibilities. Used in solving Puzzle Sudoku.

3. Undo mechanism in Text editors by keeping all the text changes in a stack.

Implementation of stack – menu oriented program


SAMPLE QUESTIONS:

MCQ/Very Short Answer Type Questions(1-mark)

1. Choose the correct output for the following stack operation(* top position)

(a) 8 5 2 5
1* (b) 8 5
5 2 1*
(c) 2 5 5 1*
(d) 5 2 1*
(e) Which list method can be used to perform Push operation in a stack implemented by list?
(f) append()
(g) extend()
(h) push()
(i) insert()

2. Which list method can be used to perform Pop operation in a stack


implemented by list?
(a) pop()
(b) pop(1)
(c) remove()
(d) pop(0)
4. Consider the following operation performed on a stack of size 3, What will be the
output? (* top position)

(a) overflow
(b) underflow
(c) 10 20 30 40 50*
(d) 10 20 40 50*
5. Based on the below given code, Write answer to the following questions i to v
(i) Identify the suitable code for statement 1?

a) colour.insert(len(colour),n)
b) colour.append(len(colour),n)
c) colour.append()
d) colour.extend()

(ii) Identify the suitable code for statement 2?

a) push(colour,c[i])
b) push(colour)
c) push(c[i])
d) push(colour,i)
(iii) Identify the suitable code for statement 3?

a) colour==[]:
b) colour.isEmpty():
c) len(colour)=0:
d) None of the above

(iv) Fill in the statement to delete an element from the stack?

a) colour.pop(1)
b) colour.pop()
c) del colour[1]
d) colour.delete(1)

(v) Fill the statement 5,to call pop function


a) pop(c)
b) pop(colour)
c) call pop(colour)
d) def pop(colour)

6. What do you mean by Data Structure?


7. LIFO data structure is?
8. Can we have nested list?
9. Name one linear data structure.
10. Name one non-linear data structure.
11. Name the operation for insertion in a stack.
12. Name the operation for deletion from a stack.
13. Name the function to find length of a list.
14. Indexing in list starts from?

Short Answer Type Questions(2-marks)

1. How is Data Structure different from Data Type?


2. Define Stack and Queue
3. Name some operations commonly performed on data structures?
4. What is a list?
5. What is traversing? Write python code to traverse a list.
6. Name the methods used for inserting and deleting elements from a list.
7. Write some applications of stack.

Application based Short Answer Type Questions(2-marks)

8. Predict the output with respect to the list L=[40,20,30,10,50]


(a) print(L)
(b) print(len(L))
(c) L.pop() ; print(L)
(d) L.append(70); print(L)
(e) L.sort(); print(L)

9. Find the output:


(a) se
c
o
n
dl
is
t=
[1
,2
,3
,
[4
,5
,
[6
,7
,8
],
9]
,1
0,
1
1]
pr
in
t(l
e
n(
se
c
o
n
dl
is
t)
)
L=[1, 2, 3, [4, 5, [ 6, 7, 8 ], 9 ] ,10, 11]
(b) L
[1
]
(c
)
L
[3
]
(d) L[3][1]
(e) L[3][2]
[0] (f) L[3]
[2]
(g) L[3][2][1]
(h) L[3][3]

10. Predict the output:


(a) b=[[9,6],[4,5],[7,7]] X=b[:2] X.append(10) print(X)

(b) b=[[9,6],[4,5],[7,7]] X=b[:2] X[1].append(10) print(X)


11. Consider STACK=[‘a’,’b’,’c’,’d’]. Write the STACK content after each operations: a)
STACK.pop( )
b) STACK.append(‘e’)
c) STACK.append(‘f’)
d) STACK.pop( )

12. Write a program to implement a stack for the students(studentno, name). Just implement
Push.

13. Write a program to implement a stack for the students(studentno, name). Just implement Pop
and display.

14. If
L=["Python", "is", "a", ["modern", "programming"], "language", "that", "we", "use"] ,
then find the output:
(a) L[0][0]
(b) L[3][0][2]
(c) L[3:4][0]
(d) L[3:4][0][1]
(e) L[3:4][0][1][3]
(f) L[0:9][0]
(g) L[0:9][0][3]
(h) L[3:4][1]

15. What is the difference between pop() and pop(0)?

Long Answer Type Questions (3-marks)


1. Write a program for linear search in a list.

2. Write a program for bubble sort.

3. Write PushOn(Book) and Pop(Book) methods/functions in Python to add a


new Book and delete a Book from a list of Book titles, considering them to act as
push and pop operations of the Stack data structure.

4. Write functions in python for performing stack operations implemented by


using list and also write a function for the menu driven operations to implement all
the stack operations? (Hint: Use global variables)

5. Write a function in python to perform Push Operation in a stack implemented


by using list. Maximum size of the stack can be input by the user along with original
stack and also display the top position and stack elements after push operation.
>>> li=[3,4,5]
>>> push(li,5)
enter the
element:20
Top position after Push operation: 3 stack after Push operation: [20, 5, 4, 3]

6. Write a function in python to perform Pop Operation in a stack implemented


by using list. Print the element deleted, top position and also display the stack
elements before and after the pop operation?
>>> li=[20,5,4,3]
>>> pop(li)
Original stack [3, 4, 5, 20]
Top position: 3 Deleted
element 3 stack after Pop
Operation: [4, 5, 20]
7. Write a function in python to display elements of a stack implemented by
using list. Use both traditional and python methods. Print stack empty message, if
elements are not there.
>>> li=[20,5,4,3]
>>> display(li)
Stack elements: [3, 4, 5, 20]
>>> displaytraditional(li) Stack elements:
3 4 5 20
>>> l2=[]
>>> display(l2) Stack empty
8. Write a function in Python PUSH(Arr),where Arr is a list of numbers, from
this list push all even numbers into a stack implemented by using a list. Display the
stack if it has at least one element, otherwise display “stack empty” message.
>>> li=[1,6,89,100,25,29]
>>> push(li)
The item 1 can't be inserted because it is not an even number The item 89 can't be
inserted because it is not an even number The item 25 can't be inserted because it is
not an even number The item 29 can't be inserted because it is not an even number
Stack elements after push operation : [100, 6]

9. Write a function in Python PUSH(mydict,maxsize=5),where mydict is a


dictionary of phone book(name and mobile numbers), from this dictionary push all
phone numbers to a stack implemented by using list and also the display the numbers
that Display the stack if it has at least one element, otherwise display “stack empty”
message >>>mydict={1234567890:"Shyam",94567892:"Ram",8657456789012:
"Karun",9674123789:"Tharun"}
>>> push(mydict)
Stack elements after push operation :
[[1234567890, 94567892, 8657456789012, 9674123789]]

10. Write a function in Python PUSH(mydict),where mydict is a dictionary of


phone book(name and mobile numbers), from this dictionary push only phone
numbers having last digit is greater than 5 to a stack implemented by using list and
also display the stack if it has at least one element, otherwise display “stack empty”
message.
>>> mydict={9446789123:"Ram",8889912345:"Sam",7789012367:"Sree"} >>>
push(mydict)
Phone number: 9446789123 last digit is less than five which can't be pushed Stack elements
after push operation : [7789012367, 8889912345]
11. Write a function in Python PUSH(mydict),where mydict is a dictionary of
phone book(name and mobile numbers), from this dictionary push only phone
numbers having 10 digits into a stack implemented by using list . Display the stack if
it has at least one element, otherwise display “stack empty” message.
>>>mydict={1234567890:"Shyam",94567892:"Ram",8657456789012:"Karun",
9674123789:"Tharun"}
>>>
push(mydict) Digit of
phone number 8
Phone number: 94567892 doesn't have 10 digits,which can't be pushed Digit of phone
number 13
Phone number: 8657456789012 doesn't have 10 digits,which can't be pushed Stack elements
after push operation : [9674123789, 1234567890]
12. Write a function in Python PUSH(Arr),where Arr is a list of numbers, From
this list push all numbers divisible by 5 in to a stack implemented by using a list.
Display the stack if it has at least one element, otherwise display appropriate error
message. >>> li=[10,2,5,6,15,30]
>>> push(li)
The item 2 can't be inserted because it is not divisible by 5 The item 6 can't be inserted
because it is not divisible by 5 Stack elements: [30, 15, 5, 10]

13. Write a function in Python POP(Arr),where Arr is a stack implemented by a


list of numbers. The function returns the value deleted from the stack and stack status
after pop operation.
>>> li=[10,20,30,40,50]
>>> dele,st=pop(li)
Original stack [50, 40, 30, 20, 10]
>>> print("Element deleted=",dele,"Stack after deletion",st[::-1])
Element deleted= 50
Stack after deletion [40, 30, 20, 10]
Or
>>> pop(li)
Original stack [40, 30, 20, 10]
(40, [10, 20, 30])

ANSWER KEY
I. MCQ/ Very Short Answer Type Questions(1-mark)
1. (d) 5 2 1*
2. (a) append()
3. (a) pop() 4. (a) overflow
5.
(i). a colour.insert(len(colour),n)
(ii). a push(colour,c[i]) (iii).a colour==[]
(iv). b colour.pop()
(v). b pop(colour)
6. Data Structure means organization of data. A data structure has well defined operations or
behaviour.
7. STACK
8. Yes
9. Lists
10. Graphs
11 PUSH
12 pop
13. len()
14. 0

Short Answer Type Questions(2-marks)


1. Data Structure provides information regarding organization of data whereas Data Type
provides information regarding the domain of values and operations that can be performed on data.
2. Stack – A stack is a linear list also known as LIFO list with the special property that items
can be added or removed from only one end called the top.
Queue – A queue is a linear list also known as FIFO list with the special property that
items can be added at one end and removed from the other.
3. Traversal, Insertion, Deletion, Searching, Sorting, Merging etc.
4. A list is a mutable sequence of data elements indexed by their position. A list is represented
using [ ] . e.g L=[10,20,30]
5. Traversing means accessing or visiting or processing each element of any data structure.
L=[10,20,30,40,5
0] for x in L:
print(x)
6. Various methods for inserting elements in a list are - insert(), append(), extend() and methods
used for deleting items from a list are – pop() , remove(), clear()
7. Reversing a string, compilers use stack to store previous state of program, undo mechanism
in text editors and backtracking.

Application based Short Answer Type Questions(2-marks)


8. (a) Ans: [40, 20, 30, 10, 50]
(b) Ans: 5
(c) Ans:50
[40, 20, 30, 10]
(d) Ans: [40, 20, 30, 10, 70]
(e) Ans: [10, 20, 30, 40, 70]
9. (a) Ans:6
(b) Ans: 2
(c) Ans:[4, 5, [6, 7, 8], 9]
(d) Ans:5 (e) Ans:6
(f) Ans
: [6, 7, 8]
(g) Ans
:7 (h)
Ans:9
10. (a) Ans:
[[9, 6], [4, 5],
10]
(b) Ans: [[9, 6], [4, 5, 10]]

11. Ans: ['a', 'b',


'c'] Ans: ['a',
'b', 'c',’e’]
Ans: ['a', 'b', 'c',’e’,’f’]
Ans: ['a', 'b', 'c',’e’]
12.

14. (a) Ans:


‘P’ (b) Ans:
‘d’
(c) Ans: ['modern', 'programming']
(d) Ans: 'programming'
(e) Ans: 'g'
(f) Ans: 'Python'
(g) Ans: 'h'
(h) Ans: IndexError: list index out of range
15. Ans: pop()
will delete
the last
element of a
list whereas
pop(0) will
delete
element at
index zero of
a list
Long Answer Type Questions (3-marks)
5. #Stack operation-Push

6.#stack operation-Pop

7. Ans: Program for pop and display operation in a stack


8.

9.

10.
11.
12

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy