0% found this document useful (0 votes)
26 views32 pages

New Microsoft Word Document

The document provides a comprehensive overview of data structures, defining key concepts such as linear and non-linear data structures, stacks, queues, and priority queues. It details various operations that can be performed on these structures, their applications in different fields, and the significance of recursion. Additionally, it covers specific algorithms, conversion rules for expressions, and methods for implementing stacks in C.

Uploaded by

Akshata Khatal
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)
26 views32 pages

New Microsoft Word Document

The document provides a comprehensive overview of data structures, defining key concepts such as linear and non-linear data structures, stacks, queues, and priority queues. It details various operations that can be performed on these structures, their applications in different fields, and the significance of recursion. Additionally, it covers specific algorithms, conversion rules for expressions, and methods for implementing stacks in C.

Uploaded by

Akshata Khatal
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/ 32

1. Define data structure.

A data structure is a storage that is used to store and organize data.


They are essential and responsible for organizing, processing, accessing, and
storing data efficiently. Various types of data structures have their characteristics,
features, applications, advantages, and disadvantages.
2. What do you mean by non-linear data structure? Give example.
The non-linear data structure is the kind of data structure in which the data may
be arranged in hierarchical fashion. For example- Trees and graphs.
3. What do you mean by linear data structure? Give example.
The linear data structure is the kind of data structure in which the data is linearly
arranged. For example- stacks, queues, linked list.
4. List the various operations that can be performed on data
structure.
Various operations that can be performed on the data structure are
• Create

• Insertion of element

• Deletion of element

• Searching for the desired element

• Sorting the elements in the data structure

• Reversing the list of elements.

5. List out the areas in which data structures are applied extensively.
Following are the areas in which data structures are applied extensively.
• Operating system- the data structures like priority queues are
used for scheduling the jobs in the operating system.
• Compiler design- the tree data structure is used in parsing the
source program. Stack data structure is used in handling
recursive calls.
• Database management system- The file data structure is used in
database management systems. Sorting and searching
techniques can be applied on these data in the file.
• Numerical analysis package- the array is used to perform the
numerical analysis on the given set of data.
• Graphics- the array and the linked list are useful in graphics
applications.
• Artificial intelligence- the graph and trees are used for the
applications like building expression trees, game playing.
6. What is recursion in data structure?
Programmers use the recursion technique to solve problems by breaking them
down into smaller, related subproblems. Recursion in the context of data
structures refers to the use of a recursive function to iteratively traverse or
modify a data structure. When dealing with recursive data structures or issues
that have a recursive nature, recursive algorithms are frequently used.

a. Recursive Functions: Recursive functions repeat their execution with a


smaller or simpler input by including a call to themselves in the function
body. The base case of a recursive function serves as the termination
condition, while the recursive case involves the function calling itself with
updated input. The base case guarantees that the recursion will ultimately
come to an end and avoids an endless cycle.
b. Recursive Data Structures: Recursive data structures are data structures
with self-referential elements that enable the definition of the structure in
terms of smaller instances of the same structure. Linked lists, trees, and
graphs are a few examples of recursive data structures. Each node in a
linked list has a reference to the one after it, creating a recursive chain. In a
similar way, nodes in trees and graphs can relate to other nodes in the same
structure.
c. Recursive Traversal: Recursive traversal entails utilizing recursion to
iteratively explore each element or node in a data structure. For instance,
while traversing a binary tree recursively, the traversal function first visits
the current node before iteratively explores the left and right subtrees. Up
till all nodes have been visited, this process is repeated. Depth-first search
(DFS) is a method that is frequently used to implement recursive traversal.
d. Recursion's Benefits and Drawbacks: Recursion has a number of
benefits, including the ability to quickly and elegantly resolve issues that
have recursive qualities. It enables the organic expression of issues that can
be broken down into smaller examples. Recursion can make some
algorithms easier to implement, especially when using recursive data
structures. Recursion may be advantageous, but there may be downsides,
such as greater memory usage and the risk for stack overflow if not handled
appropriately.
7. What is a priority queue? What are the applications for the priority
queue?
A priority queue is a type of abstract data resembling a normal queue, but here,
each
element is assigned a priority value. The order of elements in the priority queue
determines
their priority.
So, the elements with higher priority are processed first. In case two or more
elements have
the same priority, they are processed in the order they appear in the queue.
Some real-life applications of the priority queue are:
● Huffman code for data compression
● Graph algorithms like Prim’s Minimum spanning tree
● Robotics to plan and execute tasks based on priorities.
8. List some applications of queue Data Structure.
A queue is used to manage a group of elements in FIFO (First in, First out) order. A
few of its
applications are:
● Call management in call centers
● Buffers in MP3 players and CD players
● Handling interruptions in real-time systems
● In graphs for a breadth-first search algorithm
9. Define Stack
A Stack is an ordered list in which all insertions (Push operation) and deletion (Pop
operation) are made at one end, called the top. The topmost element is pointed
by top. The top is initialized to -1 when the stack is created that is when the stack
is empty. Stack is also referred as Last In First Out (LIFO) list.
10. What are the various Operations performed on the Stack?
The various operations that are performed on the stack are
CREATE(S) – Creates S as an empty stack.
PUSH(S,X) – Adds the element X to the top of the stack. POP(S) – Deletes the top
most elements from the stack. TOP(S) – returns the value of top element from the
stack. ISEMTPTY(S) – returns true if Stack is empty else false. ISFULL(S) - returns
true if Stack is full else false.

11. Write the postfix form for the expression -A+B-C+D?


A-B+C-D+
12. What are the postfix and prefix forms of the expression?
A+B*(C-D)/(P-R)
Postfix form: ABCD-*PR-/+ Prefix form: +A/*B-CD-PR
13. What is the difference between PUSH and a POP?
Push and pop are the two key data structure activities that stand for Pushing and
Popping.
These operations signify the way data is stored and retrieved in a stack.
Push- It denotes that a user is adding an element to the stack.
● It requires the name of the stack and the value of the entity.
● In case the stack is filled but you still use the ‘Push’ command, you’ll get an
‘overflow’
error, indicating that the stack can no longer accommodate another element.
Pop- It is an activity to retrieve or pull elements from the stack.
● It only needs the name of the stack.
● When you try to pull an element from an empty stack, you see an ‘underflow’
error.
When we talk about adding or removing an element, it refers to the top-most
available data
13. Define Queue
A Queue is an ordered list in which all insertions take place at one end called the
rear, while all deletions take place at the other end called the front. Rear is
initialized to -1 and front is initialized to 0. Queue is also referred as First In First
Out (FIFO) list.
14. What are the various operations performed on the Queue?
The various operations performed on the queue are CREATE(Q) – Creates Q as an
empty Queue.
Enqueue(Q,X) – Adds the element X to the Queue. Dequeue(Q) – Deletes a
element from the Queue. ISEMTPTY(Q) – returns true if Queue is empty else false.
ISFULL(Q) - returns true if Queue is full else false.
15.How do you test for an empty Queue?
The condition for testing an empty queue is rear=front-1. In linked list
implementation of queue the condition for an empty queue is the header node
link field is NULL.
16. Define Dequeue.

Deque stands for Double ended queue. It is a linear list in which insertions and
deletion are made from either end of the queue structure.
17. Define Circular Queue.

Another representation of a queue, which prevents an excessive use of memory


by arranging elements/ nodes Q1,Q2,…Qn in a circular fashion. That is, it is the
queue, which wraps around upon reaching the end of the queue
18. List any four applications of stack.
 Parsing context free languages
 Evaluating arithmetic expressions
 Function call
 Traversing trees and graph
 Tower of Hanoi

19. Define stack. What is the significance of TOP in stack?

Definition of Stack:
A stack is a linear data structure that follows the Last In, First Out (LIFO)
principle, meaning that the last element added to the stack is the first one to
be removed. It is analogous to a stack of plates, where you can only add or
remove a plate from the top.
Significance of TOP in Stack:
The TOP in a stack is a pointer or an index that keeps track of the position of
the most recently added element. It plays a crucial role in the stack
operations:
1. Push Operation: When an element is inserted into the stack, the TOP is
incremented to point to the newly added element.
2. Pop Operation: When an element is removed, the TOP is decremented to
point to the previous element.
3. Stack Overflow: If the TOP reaches the maximum size of the stack, no
more elements can be added.
4. Stack Underflow: If the TOP is at -1 (indicating an empty stack), no
elements can be removed.

20. What are the underflow and overflow conditions?

Underflow and Overflow Conditions in Stack


1. Stack Underflow:
o Definition: It occurs when trying to remove (pop) an element from an
empty stack.
o Condition: If TOP == -1, it means the stack is empty, and any further
pop operation results in underflow.
o Example:
if (TOP == -1) {
printf("Stack Underflow! Cannot pop from an empty stack.");
}
2. Stack Overflow:
o Definition: It occurs when trying to insert (push) an element into a
full stack.
o Condition: If TOP == MAX_SIZE - 1 (where MAX_SIZE is the stack's
capacity), no more elements can be added, resulting in overflow.
o Example:
if (TOP == MAX_SIZE - 1) {
printf("Stack Overflow! Cannot push more elements.");
}
Summary:
 Underflow happens when popping from an empty stack.
 Overflow happens when pushing into a full stack.
Both conditions need to be handled properly to prevent program crashes or
unexpected behavior.

21. Write a function to push an element into the stack.


22. Write a function to delete an element from the stack.
23. Give any two characteristics of stacks

Two Characteristics of Stacks:


1. Follows LIFO (Last In, First Out) Principle:
o The last element inserted into the stack is the first one to be removed.
o Example: If elements 10, 20, 30 are pushed, then 30 will be popped
first.
2. Operations are Performed at One End (TOP):
o Insertion (push) and deletion (pop) occur only at the TOP of the
stack.
o This makes stack operations efficient and easy to implement.
These characteristics make stacks useful in function calls, recursion, and
expression evaluation. 🚀
24. Point out the rules followed during the infix to postfix conversions.

Rules for Converting Infix to Postfix Expression:


Infix expressions (e.g., A + B) need to be converted into postfix notation
(e.g., AB+) using stack operations while following these rules:
1. Operands (A-Z, 0-9) are directly added to the postfix expression.
o Example: A + B → AB+
2. Operators are pushed onto a stack based on precedence.
o Operator precedence:
 ^ (Exponentiation) – Highest
 * / % (Multiplication, Division, Modulus)
 + - (Addition, Subtraction) – Lowest
o If the stack is empty or has a lower precedence operator, push the new
operator.
o If a higher or equal precedence operator is on top, pop and append it
before pushing the new operator.
3. Parentheses are handled with special rules:
o Opening bracket ( is always pushed onto the stack.
o Closing bracket ) causes popping until ( is found.
o A + (B * C) → ABC*+
4. After reading the expression, pop all remaining operators from the
stack.
o Example: A + B * C
 Postfix: ABC*+
25. Compare the working of stack and queue data structure.

26. Develop an algorithm for inserting a new element into the stack.

Algorithm for Inserting (PUSH) an Element into the Stack


Algorithm: PUSH(stack, TOP, MAX_SIZE, element)
1. Check for Overflow:
o If TOP == MAX_SIZE - 1, print "Stack Overflow" and exit.
2. Increment the TOP Pointer:
o TOP = TOP + 1
3. Insert the New Element at TOP Position:
o stack[TOP] = element
4. Print Success Message:
o "Element pushed successfully"
5. End
27. Define stack and queue and specify its operation.

Definition and Operations of Stack and Queue


1. Stack
A stack is a linear data structure that follows the LIFO (Last In, First
Out) principle, meaning the last element added is the first one removed.
Operations on Stack:
1. Push(x): Inserts element x at the top.
2. Pop(): Removes and returns the top element.
3. Peek()/Top(): Returns the top element without removing it.
4. isEmpty(): Checks if the stack is empty.
5. isFull(): Checks if the stack is full (in case of an array implementation).
✅ Example:
 Push(10) → Push(20) → Push(30) → Stack: [10, 20, 30]
 Pop() → Removes 30 → Stack: [10, 20]

2. Queue
A queue is a linear data structure that follows the FIFO (First In, First
Out) principle, meaning the first element added is the first one removed.
Operations on Queue:
1. Enqueue(x): Inserts element x at the rear (end).
2. Dequeue(): Removes and returns the front element.
3. Front(): Returns the front element without removing it.
4. isEmpty(): Checks if the queue is empty.
5. isFull(): Checks if the queue is full (in case of an array implementation).
✅ Example:
 Enqueue(10) → Enqueue(20) → Enqueue(30) → Queue: [10, 20, 30]
 Dequeue() → Removes 10 → Queue: [20, 30]

28. What are the methods to implement stack in C?

Methods to Implement a Stack in C


A stack can be implemented in C using two primary methods:
1. Using Arrays
 Fixed-size stack implementation
 Simple and efficient, but has a limited size
 Uses an array to store elements and a variable TOP to track the last inserted
element
✅ Pros: Simple and fast
❌ Cons: Fixed size, inefficient memory usage

2. Using Linked List


 Dynamic size (grows and shrinks as needed)
 Uses a struct node with a pointer to track elements
 ✅ Pros: No fixed size, efficient memory usage
❌ Cons: Requires extra memory for pointers, slightly complex
29. What are the various Operations performed on the Stack?

Operations Performed on a Stack


A stack is a LIFO (Last In, First Out) data structure that supports the following
key operations:
30. List any four applications of stack

Four Applications of Stack:


1. Function Calls and Recursion:
o The system uses a call stack to keep track of function calls.
o When a function is called, its return address and local variables are
pushed onto the stack.
o When the function completes, its data is popped from the stack.
2. Expression Evaluation (Infix to Postfix & Postfix Evaluation):
o Used to convert infix expressions (e.g., A + B * C) to postfix
expressions (ABC*+).
o Also helps in evaluating postfix expressions using a stack.
3. Undo/Redo Operations in Editors:
o Text editors and applications like MS Word use stacks to store undo
and redo operations.
o When you undo, the last action is popped from the stack.
4. Backtracking (Maze Solving, Game Moves, Browser History):
o Stacks are used in backtracking algorithms, such as solving a maze
or DFS (Depth First Search) in graphs.
o Web browsers use a stack to manage the history of visited pages
(Back/Forward navigation).
Stacks are essential in many programming and real-world applications! 🚀

31. What are the features of stacks?

Features of Stacks
1. Follows LIFO (Last In, First Out) Principle:
o The last element inserted is the first one to be removed.
o Example: If elements A, B, C are pushed, C will be popped first.
2. Operations are Performed on One End (TOP):
o Push (Insert) and Pop (Delete) occur only at the top of the stack.
o This makes stacks efficient for managing data.
3. Fixed or Dynamic Size:
o Array-based stack: Fixed size, limited by memory.
o Linked-list-based stack: Dynamic size, grows as needed.
4. Supports Basic Operations:
o Push() – Insert an element.
o Pop() – Remove the top element.
o Peek()/Top() – View the top element.
o isEmpty() – Check if the stack is empty.
5. Efficient Time Complexity:
o Push and pop operations take O(1) time (constant time).
6. Used for Backtracking & Recursion:
o Stacks help in function calls, undo operations, and maze solving.
7. Overflow & Underflow Conditions:
o Overflow: Occurs when trying to push into a full stack.
o Underflow: Occurs when trying to pop from an empty stack.
Stacks are simple yet powerful data structures widely used in computing! 🚀

32. Write a routine for IsEmpty condition of queue.


Routine for Checking if a Queue is Empty
In a queue, the isEmpty() function checks whether the queue contains any
elements or not.
33. Write the routine to push a element into a stack.

Routine to Push an Element into a Stack


The Push() function inserts an element at the top of the stack. It first checks
for an overflow condition (if the stack is full), then inserts the new element.
34. What are the methods to implement stack in C?

Methods to Implement a Stack in C


A stack can be implemented in C using two primary methods:
1. Using Arrays
 Fixed-size stack implementation
 Simple and efficient but has a limited size
 Uses an array to store elements and a variable TOP to track the last inserted
element
✅ Pros: Simple, fast access
❌ Cons: Fixed size, inefficient memory usage

2. Using Linked List


 Dynamic size (grows and shrinks as needed)
 Uses a struct node with a pointer to track elements
✅ Pros: No fixed size, efficient memory usage
❌ Cons: Requires extra memory for pointers, slightly complex
35. Write the routine to insert a element onto a queue.

Routine to Insert an Element into a Queue (Enqueue Operation)


The Enqueue() function inserts an element at the rear of the queue. Before
inserting, it checks for overflow condition (if the queue is full).
36. List out the basic operations that can be performed on a stack
37. State and explain the different ways of representing expressions.

Different Ways of Representing Expressions


Mathematical expressions can be represented in three main notations:

1. Infix Notation
 Definition: The operator is placed between operands.
 Example: A + B
 Evaluation Order: Requires operator precedence and parentheses to
define the order.
 Example with Precedence: A + B * C (Multiplication * is done before
Addition +).
 Challenge: Needs extra processing for evaluation (operator precedence
and associativity).

2. Prefix Notation (Polish Notation)


 Definition: The operator is placed before the operands.
 Example: + A B (equivalent to A + B)
 Complex Expression: + * A B C (equivalent to A * B + C)
 Evaluation Order:
1. Start from right to left.
2. Perform operations when operands appear.
 Advantage: No need for parentheses or precedence rules.
 Example Conversion:
o Infix: (A + B) * C
o Prefix: * + A B C

3. Postfix Notation (Reverse Polish Notation - RPN)


 Definition: The operator is placed after the operands.
 Example: A B + (equivalent to A + B)
 Complex Expression: A B C * + (equivalent to A + (B * C))
 Evaluation Order:
1. Start from left to right.
2. Push operands onto a stack.
3. Apply operators when encountered.
 Advantage: No need for parentheses or precedence rules.
 Example Conversion:
o Infix: (A + B) * C
o Postfix: A B + C *

38. State the rules to be followed during infix to postfix conversions

Rules for Infix to Postfix Conversion


When converting an infix expression (e.g., A + B * C) to a postfix expression
(e.g., A B C * +), the following rules must be followed:
1. Operand Handling:
o Direct Output: Any operand (e.g., variables or numbers) is directly
appended to the postfix expression.
2. Operator Handling:
o Stack Usage: Operators are pushed onto a stack.
o Precedence: When an operator is encountered, compare its
precedence with the operator at the top of the stack:
 If the new operator has higher precedence, push it onto the
stack.
 If it has lower or equal precedence, pop operators from the
stack until you find an operator with lower precedence, then push
the new operator.
o Associativity: When operators have the same precedence, their
associativity (left-to-right or right-to-left) determines whether to push
or pop.
3. Parentheses Handling:
o Left Parenthesis (: Always push onto the stack.
o Right Parenthesis ): Pop and output operators from the stack until a
left parenthesis ( is encountered. Discard both parentheses.
4. End of Expression:
o After processing the entire infix expression, pop and output all
remaining operators from the stack.
Following these rules ensures that the resulting postfix expression maintains
the correct order of operations without the need for parentheses.

39. Given the prefix for an expression. Write its postfix: -*-+abc/ef-g/hi

40. Mention the advantages of representing stacks using linked lists than arrays

Advantages of Using Linked Lists Over Arrays for Stacks


Using a linked list to implement a stack has several advantages over using
an array-based stack:
1. Dynamic Size (No Fixed Limit)
o Linked List: Grows or shrinks dynamically as elements are added or
removed.
o Array: Has a fixed size, leading to overflow if the stack exceeds its
allocated space.
2. Efficient Memory Usage
o Linked List: Uses memory only as needed, avoiding wastage.
o Array: Allocates a fixed memory block, which may be underutilized
or lead to overflow.
3. No Overflow (Unless Memory is Full)
o Linked List: No stack overflow as long as system memory is available.
o Array: Stack overflow occurs if TOP == MAX_SIZE - 1, even if memory
is available.
4. Easy Insertion & Deletion (O(1) Complexity)
o Linked List: push() and pop() operations take constant time (O(1))
without shifting elements.
o Array: Involves index shifting in some cases (not in simple stacks,
but in dynamic resizing scenarios).
5. Better for Unknown or Large Data Sizes
o Linked List: Suitable for applications where the number of elements
is unknown or large.
o Array: Requires pre-allocation, which may be insufficient or
wasteful.

41. Define double ended queue.

Double-Ended Queue (Deque) - Definition


A Double-Ended Queue (Deque) is a special type of queue in which insertion
and deletion can be performed at both ends (front and rear).
42. What are the various operations performed on the Queue?

Operations Performed on a Queue


A queue is a FIFO (First In, First Out) data structure, meaning elements are
added at the rear and removed from the front. The main operations
performed on a queue are:
43. Distinguish between stack and queue.

44. How do you test for an empty queue?

45. What is circular queue?

46. Circular queue is better than standard linear queue, Why

47. Classify the different types of queues.

48. Write a routine to display the contents of queue.

49. Write down the function to insert an element into a queue, in which the
queue is implemented as an array.
50. Analyze and write a routine to check whether the queue is full or empty.
51. For railway reservation the queue data structure is preferred –Justify.

52. Develop an algorithm for deleting an element in a double ended queue.

53. Describe with an example how to evaluate arithmetic expressions using


stacks.
54. Explain array based implementation of stacks.

You might also like