स्टैक्स के साथ काम

Python में Data Structures और Algorithms

Miriam Antona

Software Engineer

स्टैक्स

  • LIFO: Last-In First-Out
    • आखिरी डाला गया आइटम सबसे पहले निकाला जाएगा

किताबों के एक ढेर की तस्वीर.

Python में Data Structures और Algorithms

स्टैक्स

  • LIFO: Last-In First-Out
    • आखिरी डाला गया आइटम हमेशा सबसे पहले निकलेगा
  • केवल ऊपर से जोड़ सकते हैं
    • स्टैक पर push करना

किताबों के ढेर में ऊपर एक नई किताब जुड़ी हुई.

Python में Data Structures और Algorithms

स्टैक्स

  • LIFO: Last-In First-Out
    • आखिरी डाला गया आइटम हमेशा सबसे पहले निकलेगा
  • केवल ऊपर से जोड़ सकते हैं
    • स्टैक पर push करना
  • केवल ऊपर से ले सकते हैं
    • स्टैक से pop करना

किताबों के ढेर से ऊपर की किताब हटाई गई.

Python में Data Structures और Algorithms

स्टैक्स

  • LIFO: Last-In First-Out
    • आखिरी डाला गया आइटम हमेशा सबसे पहले निकलेगा
  • केवल ऊपर से जोड़ सकते हैं
    • स्टैक पर push करना
  • केवल ऊपर से हटा सकते हैं
    • स्टैक से pop करना
  • केवल आखिरी तत्व को पढ़ सकते हैं
    • स्टैक से peek करना

किताबों के ढेर में ऊपर की किताब के कवर की ओर इशारा करता तीर.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • Undo फंक्शनैलिटी

एक तत्व वाले स्टैक की छवि.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • Undo फंक्शनैलिटी
    • हर keystroke को push करें

स्टैक के ऊपर नया तत्व जुड़ा हुआ.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • Undo फंक्शनैलिटी
    • हर keystroke को push करें

स्टैक के ऊपर नया तत्व जुड़ा हुआ.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • Undo फंक्शनैलिटी
    • हर keystroke को push करें

स्टैक के ऊपर नया तत्व जुड़ा हुआ.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • Undo फंक्शनैलिटी
    • हर keystroke को push करें

स्टैक के ऊपर नया तत्व जुड़ा हुआ.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • Undo फंक्शनैलिटी
    • हर keystroke को push करें
    • आखिरी डाले गए keystroke को pop करें

स्टैक में से ऊपर से एक तत्व हटाया गया.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • सिंबल चेकर: ( [ { } ] )
    • opening symbols को push करें

एक opening symbol वाला स्टैक.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • सिंबल चेकर: ( [ { } ] )
    • opening symbols को push करें

स्टैक के ऊपर नया opening symbol जुड़ा हुआ.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • सिंबल चेकर: ( [ { } ] )
    • opening symbols को push करें

स्टैक के ऊपर नया opening symbol जुड़ा हुआ.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • सिंबल चेकर: ( [ { } ] )
    • opening symbols को push करें
    • closing symbol को check करें

opening symbols वाला स्टैक और "check "}"" शब्द.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • सिंबल चेकर: ( [ { } ] )
    • opening symbols को push करें
    • closing symbol को check करें
    • matching opening symbol को pop करें

स्टैक के ऊपर से एक opening symbol हटाया गया.

Python में Data Structures और Algorithms

स्टैक्स - वास्तविक उपयोग

  • फंक्शन कॉल्स
    • मेमोरी का ब्लॉक push करें
    • execution खत्म होने पर pop करें
Python में Data Structures और Algorithms

स्टैक्स - singly linked list से इम्प्लीमेंटेशन

लिंक्ड लिस्ट के रूप में दर्शाया गया स्टैक.

class Node:
  def __init__(self,data):
    self.data = data
    self.next = None
Python में Data Structures और Algorithms

स्टैक्स - singly linked list से इम्प्लीमेंटेशन

नोड्स के भागों के नामों के साथ लिंक्ड लिस्ट के रूप में स्टैक.

class Node:
  def __init__(self,data):
    self.data = data
    self.next = None
class Stack:
  def __init__(self):
    self.top = None
Python में Data Structures और Algorithms

स्टैक्स - singly linked list से इम्प्लीमेंटेशन

लिंक्ड लिस्ट के रूप में स्टैक, "TOP" ऊपर वाले नोड की ओर इंगित.

class Node:
  def __init__(self,data):
    self.data = data
    self.next = None
class Stack:
  def __init__(self):
    self.top = None
Python में Data Structures और Algorithms

स्टैक्स - push

खाली स्टैक और तत्वों वाले स्टैक का चित्रण.

def push(self, data):




Python में Data Structures और Algorithms

स्टैक्स - push

खाली और भरे स्टैक का चित्रण, जहाँ नया नोड जोड़ा जाएगा.

def push(self, data): 
  new_node = Node(data)

if self.top:
Python में Data Structures और Algorithms

स्टैक्स - push

खाली और भरे स्टैक का चित्रण, जहाँ नया नोड ऊपर वाले तत्व से जुड़ता है.

def push(self, data): 
  new_node = Node(data)
  if self.top:

new_node.next = self.top
Python में Data Structures और Algorithms

स्टैक्स - push

खाली और भरे स्टैक का चित्रण, जहाँ नया नोड जोड़ा जा चुका है.

def push(self, data): 
  new_node = Node(data)
  if self.top:
    new_node.next = self.top
  self.top = new_node
Python में Data Structures और Algorithms

स्टैक्स - pop

def pop(self):

if self.top is None:
return None
else:

स्टैक में "TOP" ऊपर वाले नोड की ओर इंगित.

Python में Data Structures और Algorithms

स्टैक्स - pop

def pop(self):
  if self.top is None:
    return None
  else:
    popped_node = self.top



स्टैक में "popped_node" ऊपर वाले नोड की ओर इंगित.

Python में Data Structures और Algorithms

स्टैक्स - pop

def pop(self):
  if self.top is None:
    return None
  else:
    popped_node = self.top
    self.top = self.top.next


"popped_node" ऊपर वाले नोड की ओर और "TOP" दूसरे नोड की ओर इंगित.

Python में Data Structures और Algorithms

स्टैक्स - pop

def pop(self):
  if self.top is None:
    return None
  else:
    popped_node = self.top
    self.top = self.top.next
    popped_node.next = None

"popped_node" स्टैक से अलग नोड की ओर और "TOP" ऊपर वाले नोड की ओर इंगित.

Python में Data Structures और Algorithms

स्टैक्स - pop

def pop(self):
  if self.top is None:
    return None
  else:
    popped_node = self.top
    self.top = self.top.next
    popped_node.next = None
    return popped_node.data 

"TOP" ऊपर वाले नोड की ओर इंगित करता हुआ स्टैक.

Python में Data Structures और Algorithms

स्टैक्स - peek

def peek(self):

if self.top:
return self.top.data
else:
return None
Python में Data Structures और Algorithms

Python में LifoQueue

  • LifoQueue:
    • Python का queue मॉड्यूल
    • स्टैक जैसा व्यवहार करता है
import queue


my_book_stack = queue.LifoQueue(maxsize=0)
my_book_stack.put("The misunderstanding") my_book_stack.put("Persepolis") my_book_stack.put("1984")
print("The size is: ", my_book_stack.qsize())
The size is: 3
print(my_book_stack.get())
print(my_book_stack.get())
print(my_book_stack.get())
1984
Persepolis
The misunderstanding
print("Empty stack: ", my_book_stack.empty())
Empty stack: True
Python में Data Structures और Algorithms

अभ्यास करते हैं!

Python में Data Structures और Algorithms

Preparing Video For Download...