Unit 2: Queues Flashcards

1
Q

Queue ADT

A

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

Enqueue: Add an item to the end.
Dequeue: Remove an item from the front.
Front/Peek: View the front item without removing it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Array Implementation of Queue

A

define MAX 100

Structure:

c

typedef struct {
int front;
int rear;
int arr[MAX];
} Queue;

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

Types of Queues

A

Simple Queue: Basic FIFO structure.
Circular Queue: Connects the end of the queue back to the front.
Deque (Double-Ended Queue): Allows insertion and deletion at both ends.
Priority Queue: Elements are processed based on priority rather than order.

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

Queue Operations in C

A

Enqueue Function:

c

void enqueue(Queue *q, int value) {
if (q->rear == MAX - 1) {
printf(“Queue Overflow\n”);
return;
}
q->arr[++q->rear] = value;
}

Dequeue Function:

c

int dequeue(Queue *q) {
if (q->front > q->rear) {
printf(“Queue Underflow\n”);
return -1;
}
return q->arr[q->front++];
}

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

Circular Queue Implementation

A

Structure:

c

typedef struct {
int front;
int rear;
int arr[MAX];
} CircularQueue;

Enqueue Function:

c

void enqueue(CircularQueue *q, int value) {
if ((q->rear + 1) % MAX == q->front) {
printf(“Queue Overflow\n”);
return;
}
q->rear = (q->rear + 1) % MAX;
q->arr[q->rear] = value;
}

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

Singly Linked List Structure

A

Structure:

c

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

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

Linked Lists

A

Definition: A linked list is a linear data structure where each element is a separate object, with each element (node) containing a data part and a reference (link) to the next node.
Types:

Singly Linked List: Each node points to the next node.
Doubly Linked List: Each node points to both the next and previous nodes.
Circular Linked List: The last node points back to the first node.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Insertion in Singly Linked List

A

Insertion at Beginning:
void insertAtBeginning(Node **head, int newData) {
Node newNode = (Node)malloc(sizeof(Node));
newNode->data = newData;
newNode->next = *head;
*head = newNode;
}

Insertion at End:
void insertAtEnd(Node head, int newData) {
Node *newNode = (Node
)malloc(sizeof(Node));
newNode->data = newData;
newNode->next = NULL;
if (
head == NULL) {
*head = newNode;
return;
}
Node *last = *head;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}

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

Doubly Linked List Structure

A

Structure:

c

typedef struct DNode {
int data;
struct DNode *next;
struct DNode *prev;
} DNode;

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

Deletion in Singly Linked List

A

Deletion at Beginning:
void deleteAtBeginning(Node **head) {
if (*head == NULL) return;
Node *temp = *head;
head = (head)->next;
free(temp);
}

Deletion at End:
void deleteAtEnd(Node head) {
if (
head == NULL) return;
if ((
head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
Node *secondLast = *head;
while (secondLast->next->next != NULL) {
secondLast = secondLast->next;
}
free(secondLast->next);
secondLast->next = NULL;
}

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

Doubly Linked List Insertion

A

Insertion at Beginning:

c

void insertDAtBeginning(DNode head, int newData) {
DNode *newNode = (DNode
)malloc(sizeof(DNode));
newNode->data = newData;
newNode->next = head;
newNode->prev = NULL;
if (
head != NULL) (
head)->prev = newNode;
*head = newNode;
}

Insertion at End:

c

void insertDAtEnd(DNode head, int newData) {
DNode *newNode = (DNode
)malloc(sizeof(DNode));
newNode->data = newData;
newNode->next = NULL;
if (
head == NULL) {
newNode->prev = NULL;
*head = newNode;
return;
}
DNode *last = *head;
while (last->next != NULL) last = last->next;
last->next = newNode;
newNode->prev = last;
}

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

Circular Linked List

A

Definition: A circular linked list is a linked list where all nodes are connected in a circle, with the last node pointing back to the first node.
Structure:

c

typedef struct CNode {
int data;
struct CNode *next;
} CNode;

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

Linked List Applications

A

Applications:

Dynamic Memory Allocation: Can grow and shrink as needed.
Implementing Stacks and Queues: Using linked lists.
Polynomial Arithmetic: Storing polynomial terms.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Hashing

A

Definition: Hashing is the process of mapping data to a fixed-size table using a hash function.
Hash Function: A function that takes an input and produces a fixed-size string of characters.

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

Hash Table

A

Structure: A hash table stores key-value pairs, allowing for efficient data retrieval.
Collision Resolution: Techniques to handle situations when two keys hash to the same index.

Chaining: Each table index points to a linked list.
Open Addressing: Find the next available slot.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Hash Function Example

A

Simple Hash Function:

c

int hash(int key) {
return key % TABLE_SIZE;
}

15
Q

Collision Resolution by Chaining

A

typedef struct HashNode {
int key;
int value;
struct HashNode *next;
} HashNode;

typedef struct HashTable {
HashNode **table;
} HashTable;

16
Q

Search Function in Hash Table

A

Chaining Method:
int search(HashTable *ht, int key) {
int index = hash(key);
HashNode *node = ht->table[index];
while (node) {
if (node->key == key) {
return node->value;
}
node = node->next;
}
return -1; // Not found
}

17
Q

Deletion in Hash Table

A

Chaining Method:
void delete(HashTable *ht, int key) {
int index = hash(key);
HashNode *node = ht->table[index];
HashNode *prev = NULL;

while (node) {
    if (node->key == key) {
        if (prev) {
            prev->next = node->next;
        } else {
            ht->table[index] = node->next;
        }
        free(node);
        return;
    }
    prev = node;
    node = node->next;
} }