0% found this document useful (0 votes)
7 views8 pages

Ds

The document defines various data structures, including linear (Arrays, Linked Lists, Stacks, Queues) and non-linear (Trees, Graphs, Hash Tables) types. It also details operations on data structures, algorithms for infix to postfix conversion, and provides C functions for stack and queue operations, polynomial addition, linked list manipulation, and dynamic memory allocation. Additionally, it discusses sparse matrices, types of linked lists, their disadvantages, and algorithms for evaluating postfix expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Ds

The document defines various data structures, including linear (Arrays, Linked Lists, Stacks, Queues) and non-linear (Trees, Graphs, Hash Tables) types. It also details operations on data structures, algorithms for infix to postfix conversion, and provides C functions for stack and queue operations, polynomial addition, linked list manipulation, and dynamic memory allocation. Additionally, it discusses sparse matrices, types of linked lists, their disadvantages, and algorithms for evaluating postfix expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1. Define Data structure. Classify them briefly and list the Data structure operations.

A data structure is a way of organizing and storing data in a computer so that it can be
accessed and modified efficiently.

Data structures can be classified into two main types:

●​ Linear Data Structures: Elements are arranged sequentially or linearly. Examples


include Arrays, Linked Lists, Stacks, and Queues.
●​ Non-linear Data Structures: Elements are not arranged sequentially. Examples include
Trees, Graphs, and Hash Tables.

The common data structure operations are:

●​ Traversal: Accessing each element exactly once.


●​ Searching: Finding a specific element.
●​ Insertion: Adding a new element.
●​ Deletion: Removing an existing element.
●​ Sorting: Arranging elements in a specific order.
●​ Merging: Combining two lists.

Q2. Convert the given infix expression to postfix expression. Write the algorithm for the same.

Algorithm for Infix to Postfix Conversion

1.​ Initialize an empty stack and an empty string for the postfix expression.
2.​ Iterate through the infix expression from left to right.
3.​ If the current character is an operand, append it to the postfix string.
4.​ If the current character is an opening parenthesis '(', push it onto the stack.
5.​ If the current character is a closing parenthesis ')', pop operators from the stack and
append them to the postfix string until an opening parenthesis is encountered. Pop and
discard the opening parenthesis.
6.​ If the current character is an operator, pop operators from the stack and append them to
the postfix string as long as the stack is not empty, the top of the stack is not an opening
parenthesis, and the precedence of the top of the stack is greater than or equal to the
precedence of the current operator. Then, push the current operator onto the stack.
7.​ After iterating through the entire infix expression, pop any remaining operators from the
stack and append them to the postfix string.

Answer:

i) Infix expression: Postfix expression:

ii) Infix expression: Postfix expression:

Q3. Define stack. Write c functions for push() pop() and display() operations.

A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. The
operations are performed at one end, called the "top" of the stack.

#include <stdio.h>
#define MAX_SIZE 100

int stack[MAX_SIZE];
int top = -1;

void push(int value) {


if (top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
} else {
top++;
stack[top] = value;
printf("Pushed %d to stack\n", value);
}
}

int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
} else {
int value = stack[top];
top--;
printf("Popped %d from stack\n", value);
return value;
}
}

void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}

Q4. Define queues. Write c functions for qinsert() and qdelete() operations for linear queue.

A queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. Elements
are inserted at one end (rear) and deleted from the other end (front).
#include <stdio.h>
#define MAX_SIZE 100

int queue[MAX_SIZE];
int front = -1, rear = -1;

void qinsert(int value) {


if (rear == MAX_SIZE - 1) {
printf("Queue is full\n");
} else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("Inserted %d into the queue\n", value);
}
}

void qdelete() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Deleted %d from the queue\n", queue[front]);
front++;
}
}

Q5. Write a c function to add 2 polynomials using an array.

#include <stdio.h>
#define MAX_DEGREE 100

struct Polynomial {
int coeff[MAX_DEGREE];
int degree;
};

void addPolynomials(struct Polynomial p1, struct Polynomial p2,


struct Polynomial *result) {
int i;
result->degree = (p1.degree > p2.degree) ? p1.degree :
p2.degree;
for (i = 0; i <= p1.degree; i++) {
result->coeff[i] = p1.coeff[i];
}

for (i = 0; i <= p2.degree; i++) {


result->coeff[i] += p2.coeff[i];
}
}

Q6. Define singly linked list. Write a C function to insert a node at front end.

A singly linked list is a linear data structure where each element (node) contains data and a
pointer to the next node in the sequence.

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

struct Node {
int data;
struct Node* next;
};

void insertAtFront(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*)malloc(sizeof(struct
Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

Q7. Write a C function to delete a node from front end and rear end.

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

struct Node {
int data;
struct Node* next;
};

void deleteFromFront(struct Node** head_ref) {


if (*head_ref == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head_ref;
*head_ref = temp->next;
free(temp);
}

void deleteFromRear(struct Node** head_ref) {


if (*head_ref == NULL) {
printf("List is empty\n");
return;
}
if ((*head_ref)->next == NULL) {
free(*head_ref);
*head_ref = NULL;
return;
}
struct Node* second_last = *head_ref;
while (second_last->next->next != NULL) {
second_last = second_last->next;
}
free(second_last->next);
second_last->next = NULL;
}

Q8. Define sparse matrix. For the given sparse matrix. Write the diagrammatic linked list
representation.

A sparse matrix is a matrix where the number of zero elements is significantly greater than the
number of non-zero elements.

The linked list representation for the given sparse matrix would be as
follows:

●​ Node for (1, 0) with value 4: | row: 1 | col: 0 | value: 4 | next |


●​ Node for (1, 3) with value 3: | row: 1 | col: 3 | value: 3 | next |
●​ Node for (3, 0) with value 8: | row: 3 | col: 0 | value: 8 | next |
●​ Node for (3, 3) with value 1: | row: 3 | col: 3 | value: 1 | next |

These nodes would be linked together in a specific order, typically by row, and then by column.

Q9. Write an algorithm to add 2 polynomials using linked list give representation.

Algorithm to Add Two Polynomials Using Linked Lists


1.​ Create a new linked list to store the result.
2.​ Iterate through both polynomial linked lists simultaneously.
3.​ Compare the exponents of the current nodes from both lists.
○​ If poly1->exponent > poly2->exponent, add the node from poly1 to the result list and
move to the next node in poly1.
○​ If poly2->exponent > poly1->exponent, add the node from poly2 to the result list and
move to the next node in poly2.
○​ If poly1->exponent == poly2->exponent, add the coefficients and create a new node
with the sum and the common exponent. Add this new node to the result list and
move to the next nodes in both poly1 and poly2.

4.​ If one list is exhausted, add the remaining nodes of the other list to the result list.
5.​ Return the head of the result list.

Q10. List out the types of linked lists and disadvantageous of singly linked list.

The types of linked lists are:

●​ Singly Linked List: Nodes have a data part and a pointer to the next node.
●​ Doubly Linked List: Nodes have a data part, a pointer to the next node, and a pointer to
the previous node.
●​ Circular Linked List: The last node's pointer points back to the first node, forming a
circle.

Disadvantages of a singly linked list include:

●​ Unidirectional Traversal: It can only be traversed from head to tail.


●​ Reverse Traversal is Difficult: To traverse backward, you must start from the head and
traverse all the way to the desired node.
●​ Deletion of a Preceding Node: Deleting a node requires a pointer to the previous node,
which is not readily available.

Q11. Explain dynamic memory allocation functions with an example.

Dynamic memory allocation allows a program to allocate memory during runtime. The
key functions in C are:

●​ malloc(): Allocates a block of memory of a specified size and returns a void pointer to the
beginning of the block.
●​ calloc(): Allocates a block of memory for an array of elements, initializes all bytes to zero,
and returns a void pointer.
●​ realloc(): Resizes a previously allocated block of memory.
●​ free(): Deallocates the memory block pointed to by a pointer, freeing it for reuse.

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

int main() {
int* ptr;
int n = 5;
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Memory successfully allocated using malloc.\n");
free(ptr);
return 0;
}

Q12. With the help of algorithm, evaluate the postfix expression using stacks

Algorithm to Evaluate Postfix Expression

1.​ Initialize an empty stack.


2.​ Iterate through the postfix expression from left to right.
3.​ If the current character is an operand, push its value onto the stack.
4.​ If the current character is an operator, pop the top two operands from the stack, perform
the operation, and push the result back onto the stack.
5.​ After the expression is fully traversed, the final result will be the only element left on the
stack.

Answer:

Expression: Given values:

1.​ Push 6 (A). Stack:


2.​ Push 3 (B). Stack:
3.​ Push 2 (C). Stack:
4.​ Pop 2 and 3, perform . Push 1. Stack:
5.​ Push 5 (D). Stack:
6.​ Pop 5 and 1, perform . Push 5. Stack:
7.​ Pop 5 and 6, perform . Push 11. Stack:
8.​ Push 1 (E). Stack:
9.​ Push 7 (F). Stack:
10.​Pop 7 and 1, perform . Push 1. Stack:
11.​Pop 1 and 11, perform . Push 12. Stack:

The final result is 12.

Q13. Write c functions for qinsert() and qdelete() operations circular queue.
A circular queue is a linear data structure in which the operations are performed in a circular
manner. The last position is connected to the first position.

#include <stdio.h>
#define MAX_SIZE 100

int queue[MAX_SIZE];
int front = -1, rear = -1;

void qinsert(int value) {


if ((front == 0 && rear == MAX_SIZE - 1) || (rear == (front - 1)
% (MAX_SIZE - 1))) {
printf("Queue is full\n");
} else if (front == -1) {
front = 0;
rear = 0;
queue[rear] = value;
} else if (rear == MAX_SIZE - 1 && front != 0) {
rear = 0;
queue[rear] = value;
} else {
rear++;
queue[rear] = value;
}
}

void qdelete() {
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Deleted %d from queue\n", queue[front]);
if (front == rear) {
front = -1;
rear = -1;
} else if (front == MAX_SIZE - 1) {
front = 0;
} else {
front++;
}
}
}

AI responses may include mistakes.

You might also like