Introduction to Data Structures
Data structures are primarily classified into:
Collection of elements of the same type placed in contiguous memory locations.
int arr[] = {10, 20, 30, 40};
Consist of nodes, each containing data and a reference to the next node.
struct Node {
int data;
struct Node* next;
};
Follows Last-In-First-Out (LIFO) principle.
Pile of plates analogy.
void push(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = top;
top = temp;
}
Follows First-In-First-Out (FIFO) principle.
Line at a ticket counter analogy.
void enqueue(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if (rear == NULL) {
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
Nodes connected by edges without forming a cycle.
struct Node {
int data;
struct Node* left;
struct Node* right;
};
Collections of nodes or vertices connected by edges.
Represent complex relationships like social networks.
struct Graph {
int numVertices;
struct Node** adjLists;
};
Understanding flow crucial in both fields.
push
: Add an item to the top
pop
: Remove an item from the top
enqueue
: Add an item to the end
dequeue
: Remove an item from the front
#include
int main() {
// This is a comment, it is for readability and is ignored by the compiler
printf("Hello, World!"); // Print "Hello, World!" to the terminal
return 0; // Exit status of the program
}
#include
: Includes Standard Input Output library
int main()
: Entry point of the program
{}
: Enclose the body of the
main()
function
//
/* ... */
printf
: Outputs "Hello, World!" to the screen
return 0;
: Ends the
main
function, indicating successful execution
Syntax rules must be followed for the code to compile and run correctly.
int number;
This declares
number
as an integer variable, capable of storing an integer.
=
operator.
number = 10;
The variable
number
now holds the value
10
.
int number = 10;
The variable
number
is declared and initialized to
10
.
number = 20; // The value of 'number' is now changed to 20
float
or
double
:
float decimalNumber = 3.14;
These topics can be found in the "Concept of Data Structures" section.
int
: To store integers.
float
: For single-precision floating-point numbers.
double
: For double-precision floating-point numbers.
char
: To store individual characters.
enum
: Defines a set of named integer constants.
void
: Represents the absence of type.
int
typically uses 4 bytes (32 bits).
int
: `-2,147,483,648 to 2,147,483,647`.
#include
int main() {
int integerVar = 10; // An integer variable
float floatVar = 3.14f; // A float variable
double doubleVar = 3.1415926535; // A double variable
char charVar = 'A'; // A character variable
printf("Integer variable: %d\n", integerVar);
printf("Float variable: %f\n", floatVar);
printf("Double variable: %lf\n", doubleVar);
printf("Character variable: %c\n", charVar);
return 0;
}
#include
int main() {
int a = 5, b = 2;
int addition, subtraction, multiplication, division, modulus;
int increment, decrement;
int relational, logical;
// Arithmetic operations
addition = a + b; // addition is 7
subtraction = a - b; // subtraction is 3
multiplication = a * b; // multiplication is 10
division = a / b; // division is 2 (since both are integers)
modulus = a % b; // modulus is 1 (remainder of division)
// Increment and Decrement
increment = ++a; // 'a' is increased to 6, increment is 6
decrement = --b; // 'b' is decreased to 1, decrement is 1
// Relational operation (just one example)
relational = a > b; // Will be 1 (true) because 6 is greater than 1
// Logical operation (using the result of the relational operation)
logical = relational && (b == 1); // Will be 1 (true)
printf("Addition: %d\n", addition);
printf("Subtraction: %d\n", subtraction);
printf("Multiplication: %d\n", multiplication);
printf("Division: %d\n", division);
printf("Modulus: %d\n", modulus);
printf("Increment: %d\n", increment);
printf("Decrement: %d\n", decrement);
printf("Relational (a > b): %d\n", relational);
printf("Logical ((a > b) && (b == 1)): %d\n", logical);
return 0;
}
#include
#include
// Function to simulate an analog signal for temperature
void readAnalogSignal(float temperature) {
printf("Current temperature is: %.2f degrees\n", temperature);
}
// Function to simulate a digital signal for a simple LED state
void readDigitalSignal(bool LEDstate) {
if(LEDstate) {
printf("LED is ON\n");
} else {
printf("LED is OFF\n");
}
}
int main() {
float temperatureExample = 23.5; // Example of an analog signal
bool LEDstateExample = true; // Example of a digital signal
readAnalogSignal(temperatureExample); // Outputs: Current temperature is: 23.50 degrees
readDigitalSignal(LEDstateExample); // Outputs: LED is ON
return 0;
}
A key relationship between voltage, current, and resistance.
`V = IR`Current is directly proportional to voltage and inversely proportional to resistance.
Work done by an electric current measured in watts (W).
Understanding these fundamentals helps in designing and analyzing circuits.
Crucial for creating devices from simple flashlights to complex computers.
Useful for bridging understanding of abstract concepts in electronics and data structures in C.
Understanding the fundamental concept in electronics describing the relationship between voltage, current, and resistance.
Key principle:
Example Calculation:
Solving for Voltage:
Analogous to C Programming:
Key Takeaway:
The mathematical representation:
`$$ \sum I_{\text{in}} = \sum I_{\text{out}} $$`The mathematical representation:
`$$ \sum V = 0 $$`Consider a closed circuit loop:
According to KVL:
`$$ V_1 = IR_1 + IR_2 $$`Where:
Voltage drops equal supplied voltage \( V_1 \) since energy is conserved.
Which of the following statements about a simple electronic circuit is correct?
Which of the following statements correctly represents Kirchhoff's Voltage Law (KVL)?