0% found this document useful (0 votes)
11 views4 pages

Data Structure Past Paper 2023

The document outlines various data structure concepts, including the implementation of a queue using two stacks, a recursive version of a number printing function, and tree traversal results. It also compares stacks and queues, trees and graphs, as well as linear and nonlinear data structures. Additionally, it provides a complete Java implementation of a stack using an array, detailing methods for stack operations.

Uploaded by

cece.shahzad
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)
11 views4 pages

Data Structure Past Paper 2023

The document outlines various data structure concepts, including the implementation of a queue using two stacks, a recursive version of a number printing function, and tree traversal results. It also compares stacks and queues, trees and graphs, as well as linear and nonlinear data structures. Additionally, it provides a complete Java implementation of a stack using an array, detailing methods for stack operations.

Uploaded by

cece.shahzad
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
You are on page 1/ 4

Solved Question Paper

Q1. Stack Design Using Two Stacks (Flowchart Strategy)


Strategy to implement a queue using two stacks:

Let the two stacks be:


- Stack1: for enqueue operations
- Stack2: for dequeue operations

Steps:
1. Enqueue Operation (Insert element):
- Push the element onto Stack1.

2. Dequeue Operation (Remove element):


- If Stack2 is empty:
- While Stack1 is not empty:
- Pop elements from Stack1 and push them into Stack2.
- Pop the top element from Stack2.

These steps describe the logic behind the flowchart.

Q2. Recursive Version of Code


Original Code (Loop-based):

public static void printNumbers(int n) {


for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}

Recursive Version:

public class LoopExample {


public static void printNumbers(int n) {
if (n == 0) return;
printNumbers(n - 1);
System.out.println(n);
}

public static void main(String[] args) {


int count = 5;
printNumbers(count);
}
}

Q3. Tree Traversal Results


Inorder (Left, Root, Right): 1, 3, 4, 6, 7, 8, 10, 13, 14

Preorder (Root, Left, Right): 8, 3, 1, 6, 4, 7, 10, 14, 13

Postorder (Left, Right, Root): 1, 4, 7, 6, 3, 13, 14, 10, 8

Q4. Differences
i. Stack vs Queue:

Stack Queue

Follows LIFO (Last In First Out) Follows FIFO (First In First Out)

Only one end is used for insertion and Insertion at rear and deletion at front
deletion

Examples: undo feature in editors Examples: printer queue, task scheduling

ii. Tree vs Graph:

Tree Graph

Hierarchical structure Can be cyclic or acyclic

One path between two nodes Multiple paths possible

No cycles Can have cycles

Rooted structure (has root) No root required

iii. Linear vs Nonlinear Data Structure:

Linear Nonlinear

Elements are arranged in sequence Elements connected in hierarchical manner

Examples: Array, Linked List Examples: Tree, Graph


Easier to implement More complex in traversal

Q1(b). Stack Implementation Using Array in Java


Below is a complete implementation of a stack using an array in Java. It includes methods
for push, pop, isEmpty, isFull, and a display method for testing.

public class Stack {


private int maxSize;
private int[] stackArray;
private int top;

public Stack(int size) {


maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

public void push(int value) {


if (!isFull()) {
stackArray[++top] = value;
System.out.println("Pushed: " + value);
} else {
System.out.println("Stack is full!");
}
}

public int pop() {


if (!isEmpty()) {
return stackArray[top--];
} else {
System.out.println("Stack is empty!");
return -1;
}
}

public boolean isEmpty() {


return (top == -1);
}

public boolean isFull() {


return (top == maxSize - 1);
}
public void display() {
if (isEmpty()) {
System.out.println("Stack is empty.");
return;
}
System.out.print("Stack contents: ");
for (int i = 0; i <= top; i++) {
System.out.print(stackArray[i] + " ");
}
System.out.println();
}

public static void main(String[] args) {


Stack stack = new Stack(5);
stack.push(10);
stack.push(20);
stack.push(30);
stack.display();

stack.pop();
stack.display();

System.out.println("Is stack empty? " + stack.isEmpty());


System.out.println("Is stack full? " + stack.isFull());
}
}

You might also like