0% found this document useful (0 votes)
9 views5 pages

Data Structure Ideas and Solutions

to get to know about the data structure ideas and problems solutions that how it is done in programming

Uploaded by

hijani29714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Data Structure Ideas and Solutions

to get to know about the data structure ideas and problems solutions that how it is done in programming

Uploaded by

hijani29714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structure Ideas and Solutions

Question 1: Implementing a Stack Using an Array

Question:
Implement a stack data structure using an array in a programming language of
your choice. The stack should support the following operations:

1. push(x) - Add element x to the top of the stack.


2. pop() - Remove and return the top element of the stack. If the stack is
empty, return an error message.
3. peek() - Return the top element of the stack without removing it. If the
stack is empty, return an error message.
4. isEmpty() - Return true if the stack is empty, otherwise return false.

Solution (Python):

class Stack:

def __init__(self):

self.stack = []

def push(self, x):

self.stack.append(x)

def pop(self):

if not self.isEmpty():

return self.stack.pop()

else:

return "Stack is empty"


def peek(self):

if not self.isEmpty():

return self.stack[-1]

else:

return "Stack is empty"

def isEmpty(self):

return len(self.stack) == 0

# Example usage:

stack = Stack()

stack.push(10)

stack.push(20)

print(stack.peek()) # Output: 20

print(stack.pop()) # Output: 20

print(stack.isEmpty()) # Output: False

print(stack.pop()) # Output: 10

print(stack.isEmpty()) # Output: True

print(stack.pop()) # Output: Stack is empty

Question 2: Finding the Middle Element in a Linked List

Question:
Given a singly linked list, write a function to find and return the middle element. If
the list has an even number of elements, return the second middle element.
Solution (Python):

class Node:

def __init__(self, data):

self.data = data

self.next = None

class LinkedList:

def __init__(self):

self.head = None

def append(self, data):

new_node = Node(data)

if not self.head:

self.head = new_node

return

last_node = self.head

while last_node.next:

last_node = last_node.next

last_node.next = new_node

def find_middle(self):

slow_ptr = self.head

fast_ptr = self.head
while fast_ptr and fast_ptr.next:

slow_ptr = slow_ptr.next

fast_ptr = fast_ptr.next.next

return slow_ptr.data if slow_ptr else None

# Example usage:

ll = LinkedList()

ll.append(1)

ll.append(2)

ll.append(3)

ll.append(4)

ll.append(5)

print(ll.find_middle()) # Output: 3

ll.append(6)

print(ll.find_middle()) # Output: 4

Question 3: Checking for Balanced Parentheses

Question:
Write a function to check if a given string containing parentheses is balanced. A
string is considered balanced if each opening parenthesis ( has a corresponding
closing parenthesis ) and they are correctly nested.

Solution (Python):

def is_balanced(s):

stack = []
for char in s:

if char == '(':

stack.append(char)

elif char == ')':

if not stack or stack[-1] != '(':

return False

stack.pop()

return len(stack) == 0

# Example usage:

print(is_balanced("()")) # Output: True

print(is_balanced("(())")) # Output: True

print(is_balanced("(()")) # Output: False

print(is_balanced(")(")) # Output: False

print(is_balanced("((())(()))")) # Output: True

You might also like