Home Page class 12 @ Python

CBSE Class 12 : Data Structures

Introduction to Data Structures

CBSE Class 12 : Searching

Creation And Traversal
What is Searching?
Linear/Sequential Search
Binary Search
Insertion Of Element
Deletion Of Element

CBSE Class 12 : Sorting

What is sorting?
Bubble Sort
Selection Sort
Insertion Sort

CBSE Class 12 : Stacks

Stacks
Applications of Stacks
Implementation of Stacks

CBSE Class 12 : Queues

Queues
Applications Of Queues
Implementation of Queues

Class 12 Data Structures | Stacks 9

Example:1
Write a python program to read a line of text and Print it in reverse (Reverse word by word).
Sol:
example:
Input line:
hello how are you

output:
you are how hello

line=input("Enter any line of text ")
stack=[]
for word in line.split():
    print(word)
    stack.append(word)
print("stack ",stack)
print("total words are ",len(stack))
n=len(stack)
for i in range(n-1,-1,-1):
    print(stack[i],end=' ')

Output:

Enter any line of text hello how are you
hello
how
are
you
stack  ['hello', 'how', 'are', 'you']
total words are  4
you are how hello 

Example:2
Write a python program to read a line of text and Print it in reverse.
Sol:

example:
Input line:
hello how are you

output:
uoy era woh olleh

line=input("Enter any line of text ")
stack=[]
for word in line:
    print(word)
    for ch in word:
        stack.append(ch)
print("stack ",stack)
print("total words are ",len(stack))
n=len(stack)
for i in range(n-1,-1,-1):
    print(stack[i],end='')

Output:

Enter any line of text hello how are you
h
e
l
l
o
 
h
o
w
 
a
r
e
 
y
o
u
stack  ['h', 'e', 'l', 'l', 'o', ' ', 'h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u']
total words are  17
uoy era woh olleh