0% found this document useful (0 votes)
4 views4 pages

Corrections

The document provides Python implementations for a stack and a queue using the queue module, showcasing basic operations like push, pop, enqueue, and dequeue. It also includes a simple socket server-client setup for basic information exchange, allowing the server to receive messages from the client and respond. Both implementations demonstrate how to manage data structures and establish network communication in Python.

Uploaded by

mauuudhoke07
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)
4 views4 pages

Corrections

The document provides Python implementations for a stack and a queue using the queue module, showcasing basic operations like push, pop, enqueue, and dequeue. It also includes a simple socket server-client setup for basic information exchange, allowing the server to receive messages from the client and respond. Both implementations demonstrate how to manage data structures and establish network communication in Python.

Uploaded by

mauuudhoke07
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
You are on page 1/ 4

9: Stack and Queue in Python using queue Module.

import queue
Stack implementation using LifoQueue
class Stack: def init(self): self.stack = queue.LifoQueue()
def push(self, item):
self.stack.put(item)

def pop(self):
if not self.stack.empty():
return self.stack.get()
else:
return "Stack is empty"

def display(self):
temp_stack = []
while not self.stack.empty():
temp_stack.append(self.stack.get())

for item in temp_stack[::-1]: # Reverse to maintain order for re-adding


print(item)
self.stack.put(item)

Queue implementation using Queue


class Queue: def init(self): self.q = queue.Queue()
def enqueue(self, item):
self.q.put(item)

def dequeue(self):
if not self.q.empty():
return self.q.get()
else:
return "Queue is empty"

def display(self):
temp_queue = []
while not self.q.empty():
temp_queue.append(self.q.get())

for item in temp_queue:


print(item)
self.q.put(item)

Stack operations
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
print("Stack elements (LIFO):")
stack.display() print("Popped element from stack:", stack.pop())
print("Stack after pop:")
stack.display()

Queue operations
q = Queue()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
print("\nQueue elements (FIFO):")
q.display() print("Dequeued element from queue:", q.dequeue())
print("Queue after dequeue:")
q.display()
11. Creation of simple socket for basic information exchange between server & Client.

import socket

# Server Code def

server():

def start_server(): server_socket = socket.socket(socket.AF_INET,


socket.SOCK_STREAM) server_socket.bind(("127.0.0.1", 65432)) # Bind to localhost and port 65432

server_socket.listen(1) print("Server is listening for connections...")

conn, addr = server_socket.accept()


print(f"Connected by {addr}")

while True:
data = conn.recv(1024).decode() # Receive data from the client
if not data or data.lower() == 'exit':
print("Client disconnected.")
break
print(f"Client: {data}")

response = input("Server: ") # Enter response to send to client


conn.sendall(response.encode())

conn.close()
server_socket.close()

if name == "__main__":

start_server()

# Client Code

def start_client():

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

client_socket.connect(("127.0.0.1", 65432)) # Connect to server

print("Connected to server. Type 'exit' to disconnect.")


while True:
message = input("Client: ")
if message.lower() == 'exit':
client_socket.sendall(message.encode())
break

client_socket.sendall(message.encode()) # Send message to server


response = client_socket.recv(1024).decode() # Receive response from
server
if not response:
print("Server disconnected.")
break
print(f"Server: {response}")

client_socket.close()

if name == "__main__":

start_client()

You might also like