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

text 2

Data structures notes

Uploaded by

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

text 2

Data structures notes

Uploaded by

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

UNIT-I

1.a) What are the postfix and prefix forms of the given below expression? A+B*(C-
D)/(P-R)
b) List three examples that uses linked list. c) What are the merits and demerits
of array implementation of lists?
2. a) Convert the infix (a+b)*(c+d)/f into postfix & prefix expression
b) Write the routine to insert an element into a queue.
c) Write the routine for insertion operation of singly linked list.

Here are the solutions to your questions:

1. a) Postfix and Prefix Forms

For the expression A + B * (C - D) / (P - R):

Postfix Form:

A B C D - * P R - / +

Prefix Form:

+ A / * B - C D - P R

1. b) Examples that Use Linked Lists

1. Dynamic memory allocation: Linked lists are used in memory management


for dynamically allocating and deallocating memory blocks.
2. Undo functionality in text editors: The undo operation maintains a
linked list of changes made, where each node represents an operation.
3. Browser history: Linked lists store previous and next URLs for back-
and-forward navigation.

1. c) Merits and Demerits of Array Implementation of Lists

Merits:

1. Direct Access: Elements can be accessed using indices, providing faster


access time compared to linked lists.
2. Memory locality: Arrays benefit from memory locality, which speeds up
access due to caching.
3. Ease of use: Arrays are simple and straightforward to implement.

Demerits:

1. Fixed size: The size of the array must be declared in advance, leading
to either wasted space or insufficient capacity.
2. Insertion/Deletion overhead: Adding or removing elements requires
shifting elements, which is time-consuming.
3. No dynamic resizing: Unless using a dynamic array structure (e.g.,
std::vector in C++), arrays don’t automatically resize.

2. a) Convert Infix to Postfix and Prefix

For the expression (a + b) * (c + d) / f:

Postfix Form:

a b + c d + * f /
Prefix Form:

/ * + a b + c d f

2. b) Routine to Insert an Element into a Queue

Here is a basic routine for inserting an element into a queue implemented as an


array:

void enqueue(int queue[], int *front, int *rear, int maxSize, int element) {
if ((*rear + 1) % maxSize == *front) {
printf("Queue is full. Cannot insert.\n");
return;
}
if (*front == -1) {
*front = 0;
}
*rear = (*rear + 1) % maxSize;
queue[*rear] = element;
}

2. c) Routine for Insertion in Singly Linked List

Here is a function to insert an element into a singly linked list at a specific


position:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* next;
} Node;

void insert(Node** head, int data, int position) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;

if (position == 0) { // Insert at the beginning


newNode->next = *head;
*head = newNode;
return;
}

Node* temp = *head;


for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}

if (temp == NULL) {
printf("Position out of bounds.\n");
free(newNode);
return;
}

newNode->next = temp->next;
temp->next = newNode;
}
1. a) A circular queue has a size of 5 and has 3 elements 10,20 and 40 where F=2
and R=4. After inserting 50 and 60, what is the value of F and R. Trying to
insert 30 at this stage what happens? Delete 2 elements from the queue and
insert 70, 80 & 90.
Explain and also show the sequence of steps with necessary diagrams with the
value of F & R
b) Mention any four applications of stack.

2. a) What is a DeQueue? Explain its operation with example.


b) Define an efficient representation of two stacks in a given area of memory with
n words and explain.
c) What are the features of stacks?

1. a) Circular Queue Analysis

Initial State:

• Size of the circular queue: 5


• Elements: [10, 20, 40]
• Front (F): 2
• Rear (R): 4

Step 1: Inserting 50 and 60

• Inserting 50: Since R = 4 and the queue is circular, increment R modulo


size (5).
• New R = (4 + 1) % 5 = 0. Insert 50 at position R.
• Queue: [50, -, 10, 20, 40].
• Inserting 60: Increment R modulo size (5).
• New R = (0 + 1) % 5 = 1. Insert 60 at position R.
• Queue: [50, 60, 10, 20, 40].

Trying to Insert 30:

• At this stage, F = 2 and R = 1.


• The queue is full because (R + 1) % 5 == F (2 == 2).
• Attempting to insert 30 causes an overflow.

Step 2: Deleting 2 Elements

• Deleting First Element (10): Increment F modulo size (5).


• New F = (2 + 1) % 5 = 3.
• Queue after deletion: [50, 60, -, 20, 40].
• Deleting Second Element (20): Increment F modulo size (5).
• New F = (3 + 1) % 5 = 4.
• Queue after deletion: [50, 60, -, -, 40].

Step 3: Inserting 70, 80, 90

• Inserting 70: Increment R modulo size (5).


• New R = (1 + 1) % 5 = 2. Insert 70 at position R.
• Queue: [50, 60, 70, -, 40].
• Inserting 80: Increment R modulo size (5).
• New R = (2 + 1) % 5 = 3. Insert 80 at position R.
• Queue: [50, 60, 70, 80, 40].
• Inserting 90: Increment R modulo size (5).
• New R = (3 + 1) % 5 = 4. Insert 90 at position R.
• Queue: [50, 60, 70, 80, 90].

Final State:
• F = 4
• R = 4

1. b) Applications of Stack

1. Function call management: Used to manage function calls in recursion or


during execution of nested functions.
2. Expression evaluation and conversion: Helps in evaluating postfix
expressions and converting infix to postfix/prefix.
3. Undo operations: Used in applications like text editors to track recent
changes.
4. Balancing symbols: Checks for matching parentheses or brackets in
expressions.

2. a) What is a DeQueue?

A DeQueue (Double-Ended Queue) is a linear data structure where elements can be


inserted or deleted from both ends (front and rear).

Operations:

1. InsertFront: Adds an element at the front.


2. InsertRear: Adds an element at the rear.
3. DeleteFront: Removes an element from the front.
4. DeleteRear: Removes an element from the rear.

Example:

• Start with an empty DeQueue of size 5: [ -, -, -, -, - ].


• InsertRear(10): [ 10, -, -, -, - ].
• InsertFront(20): [ 20, 10, -, -, - ].
• DeleteRear(): [ 20, -, -, -, - ].
• InsertRear(30): [ 20, 30, -, -, - ].

2. b) Efficient Representation of Two Stacks in a Single Array

Concept:

We can efficiently represent two stacks in an array of size n by letting:


1. Stack1 grow from the left (index 0) towards the right.
2. Stack2 grow from the right (index n-1) towards the left.

Implementation:

• If Stack1’s top is at top1 and Stack2’s top is at top2, then:


• Initial condition: top1 = -1, top2 = n.
• Push to Stack1: Increment top1 and insert the element at arr[top1].
• Push to Stack2: Decrement top2 and insert the element at arr[top2].
• Check Overflow: Stack1 and Stack2 collide when top1 + 1 == top2.

2. c) Features of Stacks

1. LIFO Structure: Stack operates on a Last-In-First-Out principle.


2. Dynamic Memory Allocation: Can be implemented dynamically using linked
lists.
3. Limited Access: Only the top element can be accessed or modified
directly.
4. Recursive Nature: Supports recursive algorithms by managing function
calls.

1. a) What are the limitations of queue? Explain the algorithms for various
[7M]
operations of circular queue.
b) What are the applications of queue?
c) Write a routine for IsEmpty condition of queue.

2. a) What are the draw backs of single linked list? Write and explain the
algorithm
for search and modify operations in doubly linked list with example.
b) What are enqueue and dequeue operations?
c) Explain the usage of stack in recursive algorithm implementation?

1. a) Limitations of Queue

Queues have the following limitations:


1. Fixed Size (Static Implementation): If implemented using arrays, the
queue has a fixed size, causing overflow even when there is unused space due to
elements being dequeued.
2. Inefficiency in Shifting: In a basic linear queue implementation, after
multiple dequeue operations, unused space is created but cannot be reused unless
elements are shifted.
3. No Backward Traversal: Unlike linked lists, queues do not allow
traversal or access to previous elements.

Algorithms for Circular Queue Operations

A circular queue overcomes the inefficiency of linear queues by treating the array
as circular.

Let queue[] be the array, front the front index, rear the rear index, and size the
capacity of the queue.
1. Initialization:

front = -1, rear = -1

2. Enqueue (Insert):
• Check if the queue is full:

if ( (rear + 1) % size == front )


Queue is full

• If the queue is empty, set front = 0.


• Update rear:

rear = (rear + 1) % size


queue[rear] = element

3. Dequeue (Delete):
• Check if the queue is empty:

if (front == -1)
Queue is empty

• Remove the element:

element = queue[front]

• Update front:

if (front == rear) // Single element


front = rear = -1
else
front = (front + 1) % size

4. Peek (Get Front Element):


• Return queue[front] if not empty.

1. b) Applications of Queue

1. Job Scheduling: Queues are used in operating systems for managing


processes (e.g., CPU scheduling).
2. Data Buffering: Used in I/O devices such as printers, keyboards, or
network routers.
3. Breadth-First Search (BFS): Utilized in graph traversal algorithms.
4. Asynchronous Data Transfer: Used in inter-process communication or
message passing.

1. c) Routine for IsEmpty Condition of Queue

int isEmpty(int front, int rear) {


return (front == -1);
}

2. a) Drawbacks of Singly Linked List

1. No Backward Traversal: Elements cannot be accessed in reverse order.


2. Higher Access Time: Sequential access is required to reach any element.
3. Extra Memory Overhead: Each node requires extra memory for the pointer
field.
4. Insertion/Deletion Complexity: Insertion or deletion of nodes at
arbitrary positions requires traversal to that position.

Algorithm for Search and Modify in Doubly Linked List

1. Search:
• Traverse the list starting from the head.
• Compare the target value with the current node’s data.
• If found, return the pointer to the node; otherwise, continue.
2. Modify:
• Traverse the list to find the node.
• Update the data field of the node with the new value.

Example:

#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;

Node* search(Node* head, int target) {


Node* temp = head;
while (temp != NULL) {
if (temp->data == target)
return temp;
temp = temp->next;
}
return NULL;
}

void modify(Node* head, int target, int newValue) {


Node* node = search(head, target);
if (node != NULL)
node->data = newValue;
else
printf("Element not found.\n");
}

2. b) Enqueue and Dequeue Operations

1. Enqueue:
The process of inserting an element into the queue.
• In a circular queue, rear is incremented, and the element is added at
the new position.
2. Dequeue:
The process of removing an element from the queue.
• In a circular queue, front is incremented, and the element at the
original front is removed.

2. c) Usage of Stack in Recursive Algorithm Implementation

1. Call Stack Management:


• Stacks are used internally by programming languages to manage recursive
function calls. Each function call’s parameters, local variables, and return
addresses are pushed onto the stack.
2. Backtracking Algorithms:
• Recursive algorithms such as solving a maze or generating permutations
use stacks to maintain state during backtracking.
3. Example: Factorial Calculation Using Recursion
• Recursive calls to calculate factorial n! push the intermediate states
onto the stack.

Example Code:

int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1); // Recursive call pushes onto stack
}

You might also like