Download presentation
Presentation is loading. Please wait.
Published byJohan Lesmana Modified over 5 years ago
1
ECE 103 Engineering Programming Chapter 63 Queue Implementation
Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE
2
Syllabus Pointer Version
3
Queue : Pointer Version
In this example, a bank queue is implemented as a singly-linked list using pointers. Nodes are created at runtime using malloc(). A B C head D Queue tail “enqueue” operation “dequeue” operation 2
4
Define structure of list element
struct list_node { int id; /* Customer ID number */ float money; /* Amount of money */ bank_op op; /* Transaction type */ struct list_node *next; /* Pointer to next node */ }; typedef struct list_node node; typedef enum { FALSE, TRUE } boolean; typedef enum { DEPOSIT, WITHDRAWAL } bank_op; 3
5
Bank queue (singly-linked) 4 head id money op next id money op next
tail id money op next NULL 4
6
Create the queue 5 int main (void) {
node *head = NULL; /* Pointer to head of queue */ node *tail = NULL; /* Pointer to tail of queue */ node *p; /* Work pointer */ /* Perform several bank transactions */ add_to_queue(&head, &tail, 1031, , DEPOSIT); add_to_queue(&head, &tail, 2111, , DEPOSIT); add_to_queue(&head, &tail, 3312, , WITHDRAWAL); print_queue(head); remove_from_queue(&head, &tail); printf("Searching for customer ID 3312:\n"); if ((p = search_queue(head, 3312)) == NULL) printf("ID was not found in the queue.\n"); else print_node(p); return 0; } 5
7
Create a new queue node node * create_node (int id, float money, bank_op op) { node *p; /* Work pointer */ /* Use run-time memory allocation to create new node */ if ((p = (node *) malloc(sizeof(node))) == NULL) printf("ERROR: Unable to allocate memory for node\n"); exit(1); } /* Initialize contents of new node with transaction information */ p->id = id; p->money = money; p->op = op; p->next = NULL; return p; /* Return pointer to newly created node */ 6
8
Check if the queue is empty
boolean is_empty (node *head) { return (head == NULL) ? TRUE : FALSE; } 7
9
Add a new node to the tail of the queue
void add_to_queue (node **head, node **tail, int id, float money, bank_op op) { node *new_node; new_node = create_node(id, money, op); if (!is_empty(*head)) /* Queue was not empty, so add node to the tail of the queue */ (*tail)->next = new_node; *tail = new_node; } else /* Queue was empty, so node is now the entire queue */ *head = new_node; 8
10
Remove the node at the head of the queue
void remove_from_queue (node **head, node **tail) { node *p = *head; if (!is_empty(*head)) *head = (*head)->next; if (is_empty(*head)) *tail = NULL; free(p); /* Deallocate memory of removed node */ } 9
11
Print a single node in the queue
void print_node (node *p) { if (!is_empty(p)) printf("ID=%4d Money= $%8.2f Op=%s\n", p->id, p->money, p->op ? "DEP" : "WTH" ); } else printf("The node is NULL.\n"); 10
12
Print the contents of the entire queue
void print_queue (node *head) { node *p = head; if (!is_empty(head)) while (p != NULL) printf("ID=%4d Money= $%8.2f Op=%s\n", p->id, p->money, p->op ? "DEP" : "WTH" ); p = p->next; } else printf("List is empty\n"); printf("\n"); 11
13
Search the queue for a specific key value
node * search_queue (node *head, int search_key) { node *p = head; while (p != NULL) if (p->id == search_key) break; /* Found it */ p = p->next; } return p; 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.