Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🟣 Stack Data Structure interview questions and answers to help you prepare for your next data structures and algorithms interview in 2025.

NotificationsYou must be signed in to change notification settings

Devinterview-io/stack-data-structure-interview-questions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 

Repository files navigation

data-structures-and-algorithms

You can also find all 46 answers here 👉Devinterview.io - Stack Data Structure


1. What is aStack?

Astack is a simple data structure that follows theLast-In, First-Out (LIFO) principle. It's akin to a stack of books, where the most recent addition is at the top and easily accessible.

Core Characteristics

  • Data Representation: Stacks can hold homogeneous or heterogeneous data.
  • Access Restrictions: Restricted access primarily to the top of the stack, making it more efficient for certain algorithms.

Stack Operations

  1. Push: Adds an element to the top of the stack.
  2. Pop: Removes and returns the top element.
  3. Peek: Returns the top element without removing it.
  4. isEmpty: Checks if the stack is empty.
  5. isFull (for array-based stacks): Checks if the stack is full.

All the above operations typically have a time complexity of$O(1)$, making stack operations highly efficient.

Visual Representation

Stack Data Structure

Practical Applications

  1. Function Calls: The call stack keeps track of program flow and memory allocation during method invocations.

  2. Text Editors: The undo/redo functionality often uses a stack.

  3. Web Browsers: The Back button's behavior can be implemented with a stack.

  4. Parsing: Stacks can be used in language processing for functions like balanced parentheses, andbinary expression evaluation.

  5. Memory Management: Stacks play a role in managing dynamic memory in computer systems.

  6. Infix to Postfix Conversion: It's a crucial step for evaluating mathematical expressions such as2 + 3 * 5 - 4 in the correct precedence order. Stack-based conversion simplifies parsing and involves operators such aspush andpop until the correct order is achieved.

  7. Graph Algorithms: Graph traversal algorithms such as Depth First Search (DFS) deploystacks as a key mechanism to remember vertices and explore connected components.

Code Example: Basic Stack

Here is the Python code:

classStack:def__init__(self):self.stack= []defpush(self,item):self.stack.append(item)defpop(self):ifnotself.is_empty():returnself.stack.pop()defpeek(self):ifnotself.is_empty():returnself.stack[-1]defis_empty(self):returnlen(self.stack)==0defsize(self):returnlen(self.stack)

2. WhyStack is considered aRecursive data structure?

Astack is considered arecursive data structure because its definition is self-referential. At any given point, a stack can be defined as a top element combined with another stack (the remainder).

Whenever an element is pushed onto or popped off a stack, what remains is still a stack. Thisself-referential nature, where operations reduce the problem to smaller instances of the same type, embodies the essence ofrecursion

3. What are the primary operations performed on aStack and their time complexities?

Let's look into the fundamental operations of astack and their associated time complexities.

Stack Operations and Complexity

  • Push (Time: $O(1)$): New elements are added at the top of the stack, making this a$O(1)$.
  • Pop (Time: $O(1)$): The top element, and the only one accessible, is removed during this$O(1)$ operation.
  • Peek (Time: $O(1)$): Viewing the top of the stack doesn't alter its structure, thus taking$O(1)$.
  • Size (Time: $O(1)$): Stacks typically keep track of their size, ensuring$O(1)$ performance.
  • isEmpty (Time: $O(1)$): Checks for stack emptiness and usually completes in$O(1)$ time.

Code Example: Stack

Here is the Python code:

stack= []stack.append(1)# Pushes 1 onto the stackstack.append(3)# Pushes 3 onto the stackprint(stack.pop())# Outputs 3; removes the top element from the stackprint(stack[-1])# Outputs 1; peek at the top elementprint(len(stack))# Outputs 1; returns the size of the stackprint(notstack)# Outputs False; checks if the stack is empty<br>## 4. When should I use _Stack_ or _Queue_ data structures instead of _Arrays/Lists_?**Queues**and**Stacks**providestructuredwaystohandledata,offeringdistinctadvantagesovermoregenericstructureslike**Lists**or**Arrays**.### Key Features#### Queues-**Characteristic**:First-In-First-Out (FIFO)-**Usage**:Idealfororderedprocessing,suchasprintqueuesorBFStraversal.#### Stacks-**Characteristic**:Last-In-First-Out (LIFO)-**Usage**:PerfectfortasksrequiringreverseorderlikeundoactionsorDFStraversal.#### Lists/Arrays-**Characteristic**:RandomAccess-**Usage**:Suitablewhenyouneedrandomaccesstoelementsordon'trequirestrictorderordatamanagement.<br>## 5. What are _Infix_, _Prefix_, and _Postfix_ notations?Incomputerscience,**infix**,**prefix**,and**postfix**notationsaremethodsofwritingmathematicalexpressions.Whilehumansgenerallyuseinfixnotation,machinescanmoreefficientlyparseprefixandpostfixnotations.### Infix, Prefix and Postfix Notations-**Infix**:Operatorsareplacedbetweenoperands.Thisisthemostcommonnotationforhumansduetoitsintuitiveness.Example: $1+2$-**Prefix**:Operatorsareplacedbeforeoperands.Theorderofoperationsisdeterminedbythepositionoftheoperatorratherthanparentheses.Example: $+1 \times23$whichevaluatesto $1+ (2 \times3)=7$-**Postfix**:Operatorsareplacedafteroperands.Theorderofoperationsisdeterminedbythesequenceinwhichoperandsandoperatorsappear.Example: $123 \times+$whichevaluatesto $1+ (2 \times3)=7$### Relation to Stacks-**Conversion**:Stackscanfacilitatetheconversionofexpressionsfromonenotationtoanother.Forinstance,theShuntingYardalgorithmconvertsinfixexpressionstopostfixnotationusingastack.-**Evaluation**:Bothpostfixandprefixexpressionsareevaluatedusingstacks.Forpostfix:1.Operandsarepushedontothestack.2.Uponencounteringanoperator,therequiredoperandsarepopped,theoperationisexecuted,andtheresultispushedback.Forexample,fortheexpression $123 \times+$:-1ispushedontothestack.-2ispushed.-3ispushed.- $\times$isencountered.3and2arepopped,multipliedtoget6,whichisthenpushed.- $+$isencountered.6and1arepopped,addedtoget7,whichisthenpushed.This7istheresult.Evaluatingprefixexpressionsfollowsasimilar**stack-basedmethod**buttraversestheexpressiondifferently.### Code Example: Postfix EvaluationHereisthePythoncode:```pythondefevaluate_postfix(expression):stack= []tokens=expression.split()# Handle multi-digit numbersfortokenintokens:iftoken.isdigit():stack.append(int(token))else:operand2=stack.pop()operand1=stack.pop()stack.append(perform_operation(operand1,operand2,token))returnstack[0]defperform_operation(operand1,operand2,operator):operations= {'+':operand1+operand2,'-':operand1-operand2,'*':operand1*operand2,'/':operand1/operand2  }returnoperations[operator]# Example usageprint(evaluate_postfix('1 2 + 3 4 * -'))# Output: -7

6. Explain howStacks are used inFunction Call management in programming languages.

Whenfunctions are called in programming languages, the system typically uses acall stack to manage the call sequence and memory allocation. Let's take a look at how this process works.

The Call Stack

Thecall stack maintains a record of all the active function calls that a program makes. When a new function is called, it's added to the top of the stack. Once a function finishes its execution, it's removed from the stack, and control returns to the calling function.

This "last in, first out" behavior is well-suited tostack data structures.

How the Call Stack Works

  1. Function Call: When a function is called, astack frame is created and pushed onto the call stack. This frame contains important information about the state of the function, such as local variables and the return address, which points to the instruction after the function call.

  2. Local Execution: The CPU executes the instructions within the called function. The function accesses its inputs, processes data, and calls other functions as needed.

  3. Return: If the called function doesn't make any further function calls, it exits, and its stack frame is removed. Alternatively, if the function makes additional calls, the call stack grows further.

  4. Stack Unwinding: Once the initial (or other topmost) function call is finished, there are no more functions to execute. The stack then shrinks, starting with the top frame, until it's empty.

Code Example: Using the Call Stack

Here is code written in Python:

defmultiply(a,b):result=a*breturnresultdefcalculate(a,b,c):temp=multiply(b,c)returna+tempresult=calculate(2,3,4)print(result)# Output: 14

In this example, whencalculate is called, it first callsmultiply and then performs addition. The call stack looks like this during the execution ofcalculate:

  1. calculate with parameters (2, 3, 4)
  2. multiply with parameters (3, 4)

Oncemultiply completes, its stack frame is removed, andcalculate continues with the next line of code.

Benefits and Limitations of Call Stack

Benefits

  • Automatic Memory Management: The call stack automatically allocates memory for local variables and function parameters, simplifying memory management for developers.
  • Efficiency: Its simple structure makes it efficient for managing function calls in most programming scenarios.

Limitations

  • Size Limitations: The stack has a fixed memory size allocated at program startup, which can lead to stack overflow errors if the stack grows too large.
  • No Random Access: Elements in the stack can only be accessed or removed in a last-in-first-out manner, limiting its use in some algorithms and data structures.

7. Describe an application whereStacks are naturally suited over other data structures.

Stacks find natural utility in various practical use-cases, such as in text editors for tracking actions and providing the "undo" and "redo" functionalities.

Code Example: Undo and Redo Stack

Here is the Python code:

classUndoRedoStack:def__init__(self):self._undo_stack= []self._redo_stack= []defpush(self,action):self._undo_stack.append(action)# When a new action is pushed, the redo stack needs to be resetself._redo_stack= []defundo(self):ifself._undo_stack:action=self._undo_stack.pop()self._redo_stack.append(action)returnactiondefredo(self):ifself._redo_stack:action=self._redo_stack.pop()self._undo_stack.append(action)returnaction

Stack-Based Undo and Redo Workflow

In a typical text editor, the user can:

  • Type Text: Each time new text is entered, it represents anaction that can beundone orredone.
  • Perform Undo/Redo: The editor navigates through previous actions, whether to reverse or reinstate them.

8. CompareArray-based vsLinked List stack implementations.

Array-based stacks excel in time efficiency and direct element access. In contrast,linked list stacks are preferable for dynamic sizing and easy insertions or deletions.

Common Features

  • Speed of Operations: Bothpop andpush are$O(1)$ operations.
  • Memory Use: Both have$O(n)$ space complexity.
  • Flexibility: Both can adapt their sizes, but their resizing strategies differ.

Key Distinctions

Array-Based Stack

  • Locality: Consecutive memory locations benefit CPU caching.
  • Random Access: Provides direct element access.
  • Iterator Needs: Preferable if indexing or iterators are required.
  • Performance: Slightly faster for top-element operations and potentially better for time-sensitive tasks due to caching.
  • Push:$O(1)$ on average; resizing might cause occasional$O(n)$.

Linked List Stack

  • Memory Efficiency: Better suited for fluctuating sizes and limited memory scenarios.
  • Resizing Overhead: No resizing overheads.
  • Pointer Overhead: Requires extra memory for storing pointers.

Code Example: Array-Based Stack

Here is the Python code:

classArrayBasedStack:def__init__(self):self.stack= []defpush(self,item):self.stack.append(item)defpop(self):returnself.stack.pop()ifself.stackelseNone

Code Example: Linked List Stack

Here is the Python code:

classNode:def__init__(self,data=None):self.data=dataself.next=NoneclassLinkedListStack:def__init__(self):self.head=Nonedefpush(self,item):new_node=Node(item)new_node.next=self.headself.head=new_nodedefpop(self):ifself.head:temp=self.headself.head=self.head.nextreturntemp.datareturnNone

9. Implement aDynamic Stack that automatically resizes itself.

Problem Statement

Implement aDynamic Stack that automatically resizes itself when it reaches its capacity.

Solution

Resizing a stack involves two main operations:shrinking andexpanding the stack when needed. A common strategy is todouble the stack's size each time it reaches full capacity andhalve it when it becomes 25% full, as this provides efficient amortized performance.

Key Operations

  1. push(item): Add an item to the stack.
  2. pop(): Remove and return the top item from the stack.
  3. is_full(): Check if the stack is full.
  4. is_empty(): Check if the stack is empty.
  5. expand(): Double the stack's capacity.
  6. shrink(): Halve the stack's capacity.

Algorithm Steps

  1. Start with an initial capacity for the stack. In this example, it's2.
  2. Whenever apush operation encounters a full stack, call theexpand method before the addition.
  3. Whenever apop operation leaves the stack 25% full, call theshrink method.

This ensures the stack dynamically adjusts its size based on the current number of elements.

Complexity Analysis

  • Time Complexity:

    • push:$O(1)$ amortized. Althoughexpand can take up to$O(n)$ time, it is only triggered once every$n$push operations, resulting in an average of$O(1)$ perpush.
    • pop,is_full, andis_empty:$O(1)$.
    • expand andshrink:$O(n)$ in the worst case, but they are infrequently called, so their amortized time is$O(1)$ per operation.
  • Space Complexity:$O(n)$ where$n$ is the number of elements in the stack. This accounts for the stack itself and any additional overhead such as the temporary arrays used during resizing.

Implementation

Here is the Python code:

classDynamicStack:# Initialize the stack with an initial capacitydef__init__(self,capacity=2):self.capacity=capacityself.stack= [None]*capacityself.top=-1# Push an element to the stackdefpush(self,item):ifself.is_full():self.expand()self.top+=1self.stack[self.top]=item# Pop the top element from the stack and return itdefpop(self):ifself.is_empty():raiseIndexError('Stack is empty')item=self.stack[self.top]self.top-=1ifself.top<self.capacity//4:self.shrink()returnitem# Check if the stack is fulldefis_full(self):returnself.top==self.capacity-1# Check if the stack is emptydefis_empty(self):returnself.top==-1# Double the stack's capacitydefexpand(self):self.capacity*=2new_stack= [None]*self.capacityforiinrange(self.top+1):new_stack[i]=self.stack[i]self.stack=new_stack# Halve the stack's capacitydefshrink(self):self.capacity//=2new_stack= [None]*self.capacityforiinrange(self.top+1):new_stack[i]=self.stack[i]self.stack=new_stack

10. What are the performance implications of aFixed-size Array Stack Implementation?

Whilefixed-size array stacks offer simplicity and often better performance for certain operations, such as data caching and real-time processing, several limitation are to be considered.

Space and Memory Management

  • Limited Capacity: Fixed-size arrays impose a maximum capacity for stacks, introducing the potential for overflow.
  • Pre-allocated Memory: Fixed-size arrays require memory to be allocated in advance for the maximum capacity, leading to potential inefficiencies if this capacity is not fully utilized.
  • Consistent Size: Stacks using fixed-size arrays do not auto-resize, leading to inefficient memory use if the actual size of the stack varies significantly from the allocated size.

Time Complexity of Fixed-Size Array Stacks

  • Push Operation:$O(1)$ (constant time), until the array is full and a resizing operation is initiated which leads to$O(n)$ in the worst case.
  • Pop Operation:$O(1)$ - simple memory deallocation or index decrement.
  • Peek Operation:$O(1)$ - equivalent to pop.
  • Search Operation:$O(n)$ - in the worst case, when the element is at the top of the stack or not present.

Data Structure Sensitivity

  • Space Sensitivity: Stacks using a fixed-size array have predictable, constant memory requirements.
  • Performance Sensitivity: While operations on non-fixed size stacks might have$O(1)$ average-case time complexity parameters, certain operations on fixed-size stacks can degrade in worst-case scenarios, justifying the$O(n)$ worst-case complexity.

Practical Applications

  • Real-Time Systems: Fixed-size arrays can be preferable for applications with strict timing requirements, as memory operations are more deterministic.
  • Embedded Systems: In resource-constrained environments, using fixed-size arrays can help manage memory more efficiently due to their predictable memory requirements.
  • Cache Systems: The use of fixed-size arrays is significant in caches, where the predictability of space requirements is essential.

Code Example: Fixed-Size Stack

Here is the Python code:

classFixedSizeStack:def__init__(self,capacity=10):self.stack= [None]*capacityself.top=-1defpush(self,value):ifself.top==len(self.stack)-1:print("Stack is full, cannot push.")returnself.top+=1self.stack[self.top]=valuedefpop(self):ifself.top==-1:print("Stack is empty, cannot pop.")returnvalue=self.stack[self.top]self.top-=1returnvaluedefpeek(self):ifself.top==-1:print("Stack is empty, no top element.")returnreturnself.stack[self.top]defis_empty(self):returnself.top==-1defis_full(self):returnself.top==len(self.stack)-1

11. Design aStack that supportsRetrieving the min element inO(1).

Problem Statement

The goal is to design astack data structure that can efficiently retrieve both the minimum element and the top element in$O(1)$ time complexity.

Solution

To meet the time complexity requirement, we'll maintain two stacks:

  1. Main Stack for standard stack functionality.
  2. Auxiliary Stack that keeps track of the minimum element up to a given stack position.

Algorithm Steps

  1. Pop andPush

    • For each element$e$ in theMain Stack, check if it's smaller than or equal to the top element in theAuxiliary Stack. If$e$ is the new minimum, push it onto both stacks.
  2. Minimum Element Retrieval: The top element of theAuxiliary Stack will always be the minimum element of the main stack.

Complexity Analysis

  • Time Complexity:$O(1)$ for all operations.
  • Space Complexity:$O(N)$, where$N$ is the number of elements in the stack.

Implementation

Here is the Python code:

classMinStack:def__init__(self):self.stack= []self.min_stack= []defpush(self,element):self.stack.append(element)ifnotself.min_stackorelement<=self.min_stack[-1]:self.min_stack.append(element)defpop(self):ifnotself.stack:returnNonetop=self.stack.pop()iftop==self.min_stack[-1]:self.min_stack.pop()returntopdeftop(self):returnself.stack[-1]ifself.stackelseNonedefgetMin(self):returnself.min_stack[-1]ifself.min_stackelseNone

12. How can you design aStack to be thread-safe?

Ensuringthread safety in a traditional stack, where operations are based on a last-in, first-out (LIFO) approach, can be achieved through a variety of techniques. I will give examples of three different approaches here:

  1. Locking Mechanism: Where a thread synchronizes access through synchronization techniques like locks.

  2. Non-Blocking Mechanism: Using atomic operations without explicit locks.

  3. Data Structure Selection: Choosing inherently thread-safe data structures that mimic stack operations.

Locking Mechanism

This approach synchronizes operations on the stack using a lock. While a thread holds the lock, other threads are blocked from entering critical sections.

Here is the Python code:

importthreadingclassLockedStack:def__init__(self):self.stack= []self.lock=threading.Lock()defpush(self,item):withself.lock:self.stack.append(item)defpop(self):withself.lock:ifself.stack:returnself.stack.pop()returnNone

Non-Blocking Mechanism

This approach uses atomic operations onprimitive data types, which are guaranteed to happen without interruption.

Here is the Python code for this approach:

importqueueimportthreadingdefnon_blocking_push(q,item):whileTrue:old_q=q.queuenew_q=old_q.copy()new_q.append(item)ifq.queue==old_q:q.queue=new_qreturndefnon_blocking_pop(q):whileTrue:old_q=q.queuenew_q=old_q.copy()ifq.queue:new_q.pop()ifq.queue==old_q:q.queue=new_qreturnq.queue[-1]returnNoneq=queue.LifoQueue()threading.Thread(target=non_blocking_push,args=(q,1)).start()threading.Thread(target=non_blocking_push,args=(q,2)).start()threading.Thread(target=non_blocking_pop,args=(q,)).start()

For theMulti-threaded Setup, you can run this Python code:

Data Structure Selection

Somecontainer classes, like the LifoQueue in Python, are inherently designed to be thread-safe, thus making their contents and operations secure in a multi-threaded environment.

Here is the Python code:

importqueueimportthreadingq=queue.LifoQueue()q.put(1)q.put(2)print(q.get())print(q.get())

13. Implement aStack with aFind-Middle operation inO(1) time.

Problem Statement

The task is to design astack data structure that supportspush,pop,findMiddle, anddeleteMiddle operations, all in constant$O(1)$ time complexity.

Solution

We can solve this challenge using a doubly linked list where each node also includes a pointer to the middle node. This solution, though not built on arrays, has a clear control flow and keeps a consistent time complexity.

Algorithm Steps

  1. Initialize an empty stack and setmiddle toNULL.
  2. During the push operation, update themiddle pointer based on the current number of nodes. If the number of nodes is odd,middle moves up, otherwise, it stays at the same position.
  3. The pop operation, on top of removing the item, also adjusts themiddle pointer if the removed node was pointing to it.
  4. findMiddle anddeleteMiddle simply involve accessing or manipulating the node thatmiddle points to.

Complexity Analysis

  • Time Complexity:
    • push:$O(1)$ - Constant time for every node insertion.
    • pop:$O(1)$ - Always removes the top node in a constant time regardless of the stack size.
    • findMiddle anddeleteMiddle:$O(1)$ - Directly accessed through themiddle pointer.
  • Space Complexity:$O(n)$ - Additional space is required for the pointers associated with each node.

Implementation

Here is the Python code:

classDLLNode:def__init__(self,x):self.data=xself.next=Noneself.prev=NoneclassMyStack:def__init__(self):self.head=Noneself.mid=Noneself.count=0defpush(self,x):new_node=DLLNode(x)new_node.next=self.headifself.count==0:self.mid=new_nodeelse:self.head.prev=new_nodeifself.count%2!=0:self.mid=self.mid.prevself.head=new_nodeself.count+=1defpop(self):ifself.count==0:print("Stack is empty")returnitem=self.head.dataself.head=self.head.nextifself.headisnotNone:self.head.prev=Noneself.count-=1# Update mid if the removed node was the middle oneifself.count%2==0:self.mid=self.mid.nextreturnitemdeffindMiddle(self):ifself.count==0:print("Stack is empty")returnreturnself.mid.datadefdeleteMiddle(self):ifself.count==0:print("Stack is empty")returntemp=self.midself.mid.prev.next=self.mid.nextself.mid.next.prev=self.mid.previfself.count%2!=0:self.mid=self.mid.prevelse:self.mid=self.mid.nextdeltemp

14. Implement aLinked List usingStack.

Problem Statement

The task is to implementlinked list using astack.

Solution

Linked lists are usually built with nodes, each containing a value and a pointer to the next node. However, you can alsosimulate linked lists using stacks, which follow a "last in, first out" (LIFO) mechanism.

Algorithm Steps

  1. Initialize: Two stacks:list_stack andtemp_stack.
  2. Add to Head: Push tolist_stack.
  3. Remove from Head: Pop fromlist_stack.
  4. Insert: Pop items totemp_stack until the insertion point, then push the new item and thetemp_stack items back tolist_stack.
  5. Delete: Similar to insert but pop the node to be deleted before pushing items back.

Complexity Analysis

  • Time Complexity:

    • Add to Head:$O(1)$
    • Remove from Head:$O(1)$
    • Insert at position$k$:$O(k)$
    • Delete at position$k$:$O(k)$
  • Space Complexity:

    • $O(n)$ forlist_stack, where$n$ is the number of elements.
    • $O(k)$ fortemp_stack during insert or delete operations at position$k$, with an overall space complexity still being$O(n)$ in the worst case (when$k = n$).

Implementation

Here is the Python code:

classLinkedListStack:def__init__(self):self.list_stack,self.temp_stack= [], []defpush(self,data):self.list_stack.append(data)defpop(self):returnself.list_stack.pop()definsert(self,data,pos):whilepos:self.temp_stack.append(self.list_stack.pop())pos-=1self.list_stack.append(data)whileself.temp_stack:self.list_stack.append(self.temp_stack.pop())defdelete(self,pos):whilepos:self.temp_stack.append(self.list_stack.pop())pos-=1self.list_stack.pop()whileself.temp_stack:self.list_stack.append(self.temp_stack.pop())defdisplay(self):print(self.list_stack)# Examplell_stack=LinkedListStack()ll_stack.push(1)ll_stack.push(2)ll_stack.push(3)ll_stack.insert(10,1)print("Before deletion:")ll_stack.display()ll_stack.delete(2)print("After deletion:")ll_stack.display()

15. Implement Doubly Linked List using Stacks with min complexity.

Problem Statement

The task is to implement aDoubly LinkedList usingStacks.

Solution

Using two stacks,forwardStack andbackwardStack, we can emulate adoubly linked list.

  • Insertion:

    • Beginning: Transfer elements frombackwardStack toforwardStack, then push the new element ontobackwardStack.
    • End: Transfer fromforwardStack tobackwardStack and push the new element ontobackwardStack.
  • Traversal:

    • Forward: Pop frombackwardStack, push ontoforwardStack.
    • Backward: Pop fromforwardStack, push ontobackwardStack.
  • Deletion: Traverse the needed stack to locate and remove the desired element, whether it's a specific item or the first/last entry.

Complexity Analysis

  • Time Complexity:

    • Operations likeinsertInBeginning,insertAtEnd,moveForward,moveBackward,delete,deleteFirst, anddeleteLast have a worst-case time complexity of$O(n)$ due to the potential full traversal of a stack.
  • Space Complexity:$O(n)$, where$n$ is the number of nodes in the doubly linked list, primarily occupied by the two stacks.

Implementation

Here is the Java code:

importjava.util.Stack;classDoubleLinkedList {privateStack<Integer>forwardStack;privateStack<Integer>backwardStack;publicDoubleLinkedList() {forwardStack =newStack<>();backwardStack =newStack<>();    }publicvoidinsertInBeginning(intdata) {// Move all elements to forwardStack to insert at the beginningwhile (!backwardStack.isEmpty()) {forwardStack.push(backwardStack.pop());        }backwardStack.push(data);    }publicvoidinsertAtEnd(intdata) {// Move all elements to backwardStack to insert at the endwhile (!forwardStack.isEmpty()) {backwardStack.push(forwardStack.pop());        }backwardStack.push(data);    }publicvoidmoveForward() {if (backwardStack.isEmpty()) {System.out.println("No more elements to move forward.");return;        }System.out.println("Moving forward: " +backwardStack.peek());forwardStack.push(backwardStack.pop());    }publicvoidmoveBackward() {if (forwardStack.isEmpty()) {System.out.println("No more elements to move backward.");return;        }System.out.println("Moving backward: " +forwardStack.peek());backwardStack.push(forwardStack.pop());    }publicvoiddelete(intdata) {Stack<Integer>tempStack =newStack<>();booleandeleted =false;while (!backwardStack.isEmpty()) {if (backwardStack.peek() ==data && !deleted) {backwardStack.pop();deleted =true;break;            }tempStack.push(backwardStack.pop());        }while (!tempStack.isEmpty()) {backwardStack.push(tempStack.pop());        }if (!deleted) {System.out.println("Element not found.");        }    }publicvoiddeleteFirst() {if (backwardStack.isEmpty()) {System.out.println("List is empty.");return;        }backwardStack.pop();    }publicvoiddeleteLast() {while (!forwardStack.isEmpty()) {backwardStack.push(forwardStack.pop());        }if (!backwardStack.isEmpty()) {backwardStack.pop();        }else {System.out.println("List is empty.");        }    }}

Explore all 46 answers here 👉Devinterview.io - Stack Data Structure


data-structures-and-algorithms

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp