ECE 103 Engineering Programming Chapter 63 Queue Implementation

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
STACKS AND QUEUES. A LINKED LIST IMPLEMENTATION OF A QUEUE data next data next NULL data next cnt front rear queue node Queue: First-In-First-Out (FIFO)
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Data Structures - Queues
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Linked Lists. Dynamic Data Structure Applications –where amount of required memory is determined at run-time.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CSC2100B Tutorial 2 List and stack implementation.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Programming Practice 3 - Dynamic Data Structure
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Linked Lists and Generics Written by J.J. Shepherd.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
Lecture No.09 Data Structures Dr. Sohail Aslam
Chapter 12 – Data Structures
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Queue data structure.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
(2-1) Data Structures & The Basics of a Linked List I
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Topic 16 Queues Adapted from Mike Scott’s materials.
Queue, Deque, and Priority Queue Implementations
(2-1) Data Structures & The Basics of a Linked List I
Memory Organization Process 1 (browser) Code Process 3 (word)
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues: Implemented using Arrays
Review & Lab assignments
Data Structures and Algorithms
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 62 Stack Implementation
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Linked Lists.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Circular Queues: Implemented using Arrays
General List.
Queues: Implemented using Linked Lists
CSCS-200 Data Structure and Algorithms
Module 13 Dynamic Memory.
Presentation transcript:

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 Wong @ PSU ECE

Syllabus Pointer Version

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

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

Bank queue (singly-linked) 4 head id money op next id money op next tail id money op next NULL 4

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, 127.50, DEPOSIT); add_to_queue(&head, &tail, 2111, 32.00, DEPOSIT); add_to_queue(&head, &tail, 3312, 550.91, 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

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

Check if the queue is empty boolean is_empty (node *head) { return (head == NULL) ? TRUE : FALSE; } 7

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

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

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

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

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