0% found this document useful (0 votes)
24 views3 pages

Reverse String Using Stack in Python

Practical_14_reverse a string using stack

Uploaded by

vishwajeet.myjob
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)
24 views3 pages

Reverse String Using Stack in Python

Practical_14_reverse a string using stack

Uploaded by

vishwajeet.myjob
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

# Python program to to reverse a string using stack

# Class representing a node in the class

class Node:

def __init__(self,data):

[Link] = data

[Link] = None

# Class to implement stack using a singly linked list

class Stack:

def __init__(self):

[Link] = None

# Function to check if the stack is empty

def is_empty(self):

# If head is None, the stack is empty

return [Link] is None

# Function to push an element onto the stack

def push(self,data):

# Create a new node with given data

new_node = Node(data)

if [Link] is None:

[Link] = new_node

else:

new_node.next = [Link]

[Link]=new_node

# Function to remove the top element from the stack


def pop(self):

if not self.is_empty():

temp = [Link]

[Link] = [Link]

return temp

# Creating a stack

st = Stack()

str=input("\nEnter the string to be reversed: ")

#String reversal

#Pushing individual characters from string into the stack

for ch in str:

[Link](ch)

rlist=[]

while (not st.is_empty()):

[Link]([Link]())

rstr=''.join(rlist)

print("\nReversed String is: ",rstr)

You might also like