0% found this document useful (0 votes)
14 views

Data Structure

The document discusses various data structures and their time complexities for common operations like access, search, insertion, and deletion. It provides worst case and average time complexities for different data structures including arrays, stacks, queues, linked lists, hash tables, binary search trees, AVL trees, and red-black trees. It also defines stable sorting as maintaining the original order of elements with equal values after sorting, and unstable sorting as changing the original order of equal elements.

Uploaded by

Oporajita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Data Structure

The document discusses various data structures and their time complexities for common operations like access, search, insertion, and deletion. It provides worst case and average time complexities for different data structures including arrays, stacks, queues, linked lists, hash tables, binary search trees, AVL trees, and red-black trees. It also defines stable sorting as maintaining the original order of elements with equal values after sorting, and unstable sorting as changing the original order of equal elements.

Uploaded by

Oporajita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Stack

Implementation of Queue using two Stacks

QUEUE

STACK 2
STACK 1
Implementation of Stack using two Queues

q1

q2

STACK

Explain how to implement two stacks in one array A[1..n].


Answer:
The first stack starts at 1 and grows up towards n, while the second starts form
n and grows down towards 1. Stack overflow happens when an element is
pushed when the two stack pointers are adjacent.
Evaluation of Postfix Expression

Following is algorithm for evaluation postfix expressions.

1) Create a stack to store operands (or values).


2) Scan the given expression and do following for every scanned element.
…..a) If the element is a number, push it into the stack
…..b) If the element is a operator, pop operands for the operator from stack.
Evaluate the operator and push the result back to the stack
3) When the expression is ended, the number in the stack is the final answer

Evaluation of Postfix Expression:Let the given expression be 2 3 1 * + 9 -

1
2 3 3
2 9
3 2 5
2 5

-4

Prefix: + AB (operator before it’s operands)


Infix: A+B (operator in the middle of it’s operands)
Postfix: AB+ (operator after it’s operands)
Example: Infix to postfix, infix to prefix: (a + b) ∗ (c + d)

 Infix to postfix
SCANNED STACK EXPRESSION
( (
a a
+ (+ a
b (+ ab
) ab+
* * ab+
( *( ab+
c *( ab+c
+ *(+ ab+c
d *(+ ab+cd
) * ab+cd+
ab+cd+*

 infix to prefix: (a + b) ∗ (c + d)
 inverse infix expression: (d+c)*(b+a)

SCANNED STACK EXPRESSION


( (
d d
+ (+ d
c (+ dc
) dc+
* * dc+
( *( dc+
b *( dc+b
+ *(+ dc+b
a *(+ dc+ba
) * dc+ba+
dc+ba+*

Final result= *+ab+cd


Linked List
step 1: Node declaration
step 2: Declare variables thats points to the node.
Step 3: Allocate memory for new node
Step 4: Insert new node value

Create a new node:


step 1: Node declarations

Step 2: Declare variables thats points to the node.

Step 3: Allocate memory for new node

Step 4: Insert new node value


 Write a function to count the number of nodes in a given singly linked list.

Solution:
 Write a function to display all the nodes values in a given singly linked list.

Solution:

 Write a function that searches a given key ‘x’ in a given singly linked list. The
function should return true if x is present in linked list and false otherwise.

Solution:
 Write a GetNth() function that takes a linked list and an integer index and
returns the data value stored in the node at that index position.

Solution:
 Can you implement the dynamic-set operation INSERT on a singly linked list
in O(1) time? How about DELETE?

Solution:
INSERT - yes, words can be inserted directly at the beginning of the list,
DELETE - no, because it requires traversing the whole list.

 Implement a stack using a singly linked list L. The operations PUSH and POP
should still take O(1) time.

Solution:
The PUSH operation adds an element in the beginning of the list and
the POP operation removes the first element from the list.

 Implement a queue by a singly linked list L. The operations ENQUEUE and


DEQUEUE should still take O(1) time.

Solution:
 We need to keep track of the last element of the list.
 Whenever we ENQUEUE, we should be inserting the element after it
and marking the new last element of the list.
 Whenever we DEQUEUE, we should pop the first element of the list.
Worst Case time complexity of different data structures for different operations

Data structure Access Search Insertion Deletion


Array O(1) O(N) O(N) O(N)
Stack O(N) O(N) O(1) O(1)
Queue O(N) O(N) O(1) O(1)
Singly Linked list O(N) O(N) O(1) O(1)
Doubly Linked List O(N) O(N) O(1) O(1)
Hash Table O(N) O(N) O(N) O(N)
Binary Search Tree O(N) O(N) O(N) O(N)
AVL Tree O(log N) O(log N) O(log N) O(log N)
Binary Tree O(N) O(N) O(N) O(N)
Red Black Tree O(log N) O(log N) O(log N) O(log N)

Average time complexity of different data structures for different operations

Data structure Access Search Insertion Deletion


Array O(1) O(N) O(N) O(N)
Stack O(N) O(N) O(1) O(1)
Queue O(N) O(N) O(1) O(1)
Singly Linked list O(N) O(N) O(1) O(1)
Doubly Linked List O(N) O(N) O(1) O(1)
Hash Table O(1) O(1) O(1) O(1)
Binary Search Tree O(log N) O(log N) O(log N) O(log N)
AVL Tree O(log N) O(log N) O(log N) O(log N)
B Tree O(log N) O(log N) O(log N) O(log N)
Red Black Tree O(log N) O(log N) O(log N) O(log N)
Stable sorting: - If a sorting algorithm, after sorting the contents, does not change
the sequence of similar content in which they appear, it is called stable sorting.
Example: - Merge Sort, Insertion Sort, Bubble Sort, and Binary Tree Sort.

Unstable sorting: - If a sorting algorithm, after sorting the contents, changes the
sequence of similar content in which they appear, it is called unstable sorting.
Example: - QuickSort, Heap Sort, and Selection sort

You might also like