Unit 1: Introduction to Data Structures and Stacks Flashcards

1
Q

What is ADT?

A

Definition: An ADT is a model for data types where the data type is defined by its behavior (operations) rather than its implementation.
Components:

Data: Values stored in the structure.
Operations: Functions that can be performed on the data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Data Structure Definition

A

Definition: A data structure is a way of organizing and storing data so that it can be accessed and modified efficiently.
Types:

-Primitive: Integer, Float, Character, etc.
-Non-primitive: Arrays, Structures, Linked Lists, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Classification of Data Structures

A

Linear Data Structures: Arrays, Linked Lists, Stacks, Queues.
Non-linear Data Structures: Trees, Graphs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Operations on Data Structures

A

Common Operations:

Traversal: Visiting each element.
Insertion: Adding new data.
Deletion: Removing existing data.
Searching: Finding data.
Updating: Modifying data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Stack ADT

A

Definition: A stack is a linear data structure that follows the Last In First Out (LIFO) principle.
Basic Operations:

Push: Add an item to the top.
Pop: Remove the item from the top.
Peek/Top: View the item at the top without removing it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Array Implementation of Stack

A

Structure:
code:-
#define MAX 100
typedef struct {
int top;
int arr[MAX];
} Stack;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Stack Operations in C

A

Push Function:
void push(Stack *s, int value) {
if (s->top == MAX - 1) {
printf(“Stack Overflow\n”);
return;
}
s->arr[++s->top] = value;
}
——————————————-
Pop Function:
int pop(Stack *s) {
if (s->top == -1) {
printf(“Stack Underflow\n”);
return -1;
}
return s->arr[s->top–];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Stack Applications

A

Applications:
Expression Evaluation: Converting infix expressions to postfix.
Backtracking: Used in algorithms like maze solving.

Example: Postfix Evaluation

int evaluatePostfix(char* exp) {
Stack s;
s.top = -1;
for (int i = 0; exp[i]; i++) {
if (isdigit(exp[i])) {
push(&s, exp[i] - ‘0’);
} else {
int val2 = pop(&s);
int val1 = pop(&s);
switch (exp[i]) {
case ‘+’: push(&s, val1 + val2); break;
case ‘-‘: push(&s, val1 - val2); break;
case ‘*’: push(&s, val1 * val2); break;
case ‘/’: push(&s, val1 / val2); break;
}
}
}
return pop(&s);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Advantages of Stack

A

Advantages of Stacks:

-Simplicity: Stacks are a simple and easy-to-understand data structure, making them suitable for a wide range of applications.
-Efficiency: Push and pop operations on a stack can be performed in constant time (O(1)), providing efficient access to data.
-Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last element added to the stack is the first one removed. This behavior is useful in many scenarios, such as function calls and expression evaluation.
-Limited memory usage: Stacks only need to store the elements that have been pushed onto them, making them memory-efficient compared to other data structures.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Disadvantages of Stacks:

A

Limited access: Elements in a stack can only be accessed from the top, making it difficult to retrieve or modify elements in the middle of the stack.
Potential for overflow: If more elements are pushed onto a stack than it can hold, an overflow error will occur, resulting in a loss of data.
Not suitable for random access: Stacks do not allow for random access to elements, making them unsuitable for applications where elements need to be accessed in a specific order.
Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number of elements that need to be stored is unknown or highly variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly