0% found this document useful (0 votes)
4 views3 pages

Data Structures Answers-1

The document provides a comprehensive overview of data structures, including the differentiation between linear and non-linear types, evaluation of a postfix expression using a stack, and various methods for graph representation in memory. It also includes C code for inserting and deleting in a linear queue as a linked list, constructing a Huffman tree for the word 'structures', and converting infix expressions to postfix. The content is structured as a question-and-answer format, suitable for a data structure examination paper.

Uploaded by

heenaknoor
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)
4 views3 pages

Data Structures Answers-1

The document provides a comprehensive overview of data structures, including the differentiation between linear and non-linear types, evaluation of a postfix expression using a stack, and various methods for graph representation in memory. It also includes C code for inserting and deleting in a linear queue as a linked list, constructing a Huffman tree for the word 'structures', and converting infix expressions to postfix. The content is structured as a question-and-answer format, suitable for a data structure examination paper.

Uploaded by

heenaknoor
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/ 3

Answers to Data Structure Question

Paper (May 2024 - C Scheme)


Q1 (a) Differentiate between linear and non-linear data structures.
Aspect | Linear Data Structures | Non-Linear Data Structures
------------------------|-----------------------------------------|--------------------------------------------
Arrangement | Sequential (e.g., array, linked list) | Hierarchical or interconnected
(e.g., trees, graphs)
Memory Usage | May have unused memory | Efficient memory utilization
Traversal | Single level traversal | Requires hierarchical traversal
Implementation | Simpler to implement | More complex to implement
Examples | Array, Stack, Queue, Linked List | Tree, Graph

Q1 (b) Evaluate postfix expression “78+45-*” using stack.


Steps:
1. Push 7 → Stack: [7]
2. Push 8 → Stack: [7,8]
3. ‘+’: 7 + 8 = 15 → Stack: [15]
4. Push 4 → Stack: [15,4]
5. Push 5 → Stack: [15,4,5]
6. ‘-’: 4 - 5 = -1 → Stack: [15, -1]
7. ‘*’: 15 * -1 = -15 → Stack: [-15]

Final Answer: -15

Q1 (c) Graph Representation in Memory


1. Adjacency Matrix: 2D array to represent connections. Uses more space.
2. Adjacency List: Array of linked lists. Efficient for sparse graphs.
3. Edge List: List of all edges. Simple but not efficient for lookups.

Q1 (d) Insert/Delete in Linear Queue as Linked List (C Code)


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

typedef struct Node {


int data;
struct Node* next;
} Node;
Node *front = NULL, *rear = NULL;

void enqueue(int val) {


Node* newNode = malloc(sizeof(Node));
newNode->data = val; newNode->next = NULL;
if (rear == NULL) front = rear = newNode;
else { rear->next = newNode; rear = newNode; }
}

void dequeue() {
if (!front) return;
Node* temp = front;
front = front->next;
if (!front) rear = NULL;
free(temp);
}

Q2 (a) Construct Huffman Tree for "structures" and find codes


Frequency Table: s: 2, t: 2, r: 2, u: 2, c: 1, e: 1

Steps:
1. Combine c(1)+e(1) = 2 → Node1
2. Node1(2)+s(2) = 4 → Node2
3. t(2)+r(2) = 4 → Node3
4. u(2)+Node2(4) = 6 → Node4
5. Node3(4)+Node4(6) = 10 → Root

Codes (example): s: 110, t: 00, r: 01, u: 10, c: 1110, e: 1111

Q2 (b) Infix to Postfix Conversion (C Code)


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 100

char stack[SIZE]; int top = -1;


void push(char ch) { stack[++top] = ch; }
char pop() { return stack[top--]; }
char peek() { return stack[top]; }
int precedence(char ch) {
if (ch == '^') return 3;
if (ch == '*' || ch == '/') return 2;
if (ch == '+' || ch == '-') return 1;
return 0;
}

void infixToPostfix(char* exp) {


for (int i = 0; exp[i]; i++) {
char ch = exp[i];
if (isalnum(ch)) printf("%c", ch);
else if (ch == '(') push(ch);
else if (ch == ')') {
while (peek() != '(') printf("%c", pop());
pop();
} else {
while (top != -1 && precedence(peek()) >= precedence(ch))
printf("%c", pop());
push(ch);
}
}
while (top != -1) printf("%c", pop());
}

int main() {
char exp[] = "a+(b*c)";
infixToPostfix(exp);
return 0;
}

You might also like