Computer Programming using C



Similar documents
Data Structures Using C++ 2E. Chapter 5 Linked Lists

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

DATA STRUCTURES USING C

DATA STRUCTURE - QUEUE

Common Data Structures

Chapter 3: Restricted Structures Page 1

Analysis of a Search Algorithm

Data Structures and Algorithms Stacks and Queues

Introduction to Data Structures and Algorithms

Linked Lists Linked Lists, Queues, and Stacks

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Application of Stacks: Postfix Expressions Calculator (cont d.)

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Data Structures and Data Manipulation

10CS35: Data Structures Using C

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Module 2 Stacks and Queues: Abstract Data Types

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Data Structure [Question Bank]

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Lecture 12 Doubly Linked Lists (with Recursion)

Timing of a Disk I/O Transfer

DATA STRUCTURE - STACK

Stacks. Linear data structures

Monitors, Java, Threads and Processes

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Chapter 6 Concurrent Programming

Universidad Carlos III de Madrid

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Optomate Training Compendium Appointment Book Page 2

EMC RepliStor for Microsoft Windows ERROR MESSAGE AND CODE GUIDE P/N REV A02

Data Structures UNIT III. Model Question Answer

Algorithms and Data Structures

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Data Structures and Algorithms

PES Institute of Technology-BSC QUESTION BANK

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Design Patterns in C++

I/O Management. General Computer Architecture. Goals for I/O. Levels of I/O. Naming. I/O Management. COMP755 Advanced Operating Systems 1

Basic Data Structures and Algorithms

PROBLEMS (Cap. 4 - Istruzioni macchina)

STACKS,QUEUES, AND LINKED LISTS

Node-Based Structures Linked Lists: Implementation

Instructions for applying data validation(s) to data fields in Microsoft Excel

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

IBM TCP/IP Network Port Monitor

Sequential Data Structures

6. Storage and File Structures

Assignment Kits. Summary Kit Contents Lecture 1: Kit cover sheet (page 40)

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Accounting 402 Illustration of a change in inventory method

LABORATORY. 14 Disk Scheduling OBJECTIVE REFERENCES. Watch how the operating system schedules the reading or writing of disk tracks.

BCS2B02: OOP Concepts and Data Structures Using C++

CSE 326: Data Structures B-Trees and B+ Trees

Parallel Compression and Decompression of DNA Sequence Reads in FASTQ Format

Programming with Data Structures

SVMi-4 & SVM-400. Voice Mail System. System Administration Manual

Red Hat Linux Internals

Big Data and Scripting. Part 4: Memory Hierarchies

Unordered Linked Lists

Algorithms and Data Structures

Linux Driver Devices. Why, When, Which, How?

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

Communité - MIC. Frequently Asked Questions. 1. What is Communité - MIC 3.0 (Messaging Interaction Center)?

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Operating Systems and Networks

Attacking Host Intrusion Prevention Systems. Eugene Tsyrklevich

Client-server Sockets

MESSAGES QUEUES. Group 05. zondag 20 mei 12

Changing How the Mouse Works in Windows 7

St S a t ck a ck nd Qu Q eue 1

Thread Synchronization and the Java Monitor

UC CubeSat Main MCU Software Requirements Specification

ACT State Testing Online Services Tutorial

OpenGL Insights. Edited by. Patrick Cozzi and Christophe Riccio

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Cpt S 223. School of EECS, WSU

Quality of Service (QoS) on Netgear switches

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

SYSTEM ecos Embedded Configurable Operating System

================================================================

System Software Prof. Dr. H. Mössenböck

Zoom Guide. Zoom Meeting Experience. Hardware Requirements. Joining a Zoom Meeting

Data Structures Using C++ 2E. Chapter 5 Linked Lists

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

In this chapter you will find information on the following subjects:

Transcription:

Computer Programming using C Lecture 11: Buffers and linked lists: solutions Dr Julian Miller Room P/M/101 Ext 2383; E-mail: jfm@ohm http://www.elec.york.ac.uk/intsys/users/jfm7 Contents Solution to buffers exercises Linked lists (solutions) In the lab this week: Doubly linked lists 1

Buffers In lab 14, you looked at two kinds of buffers, LIFO (stack) and FIFO (queue) In exercise 14.1 you were asked to complete a buffer_remove function buffer_remove retrieves and removes an item of data from the stack LIFO Buffer (stack): demo 2

Exercise 14.1: solution Exercise 14.2 The programs provided for lab 14 don t check whether the buffer is full or empty. This means that the program could crash under certain circumstances. You were asked to warn the user in these cases (and disallow possible situations that might cause an error) 3

Exercise 14.2: solution Idea: construct a validation function that checks for overflow and underflow situations and warns the user. Exercise 14.2: calling buffer_validate In the main function modify the switch statement as follows 4

Exercise 14.3: Implementing a FIFO buffer FIFO is a queue. This differs from the stack in where we add items. In a stack items are added and remove from the same place, the top of the stack In a queue, items are removed from the top (as with stack) but added at another place. This is best handled with two markers to tell us where the queue begins and ends buffer_head, buffer_foot So in exercise 14.3 you were asked to implement a queue FIFO buffer (queue): demo 5

In buffer.c Exercise 14.3: Initialise All we need to do is initialise two variables one for the head of the queue and one for the foot Exercise 14.3: buffer_add Now we need to modify the routine that adds things to the queue buffer 6

Exercise 14.3: buffer_remove Also we need to modify the routine that removes things from the queue buffer Exercise 14.3: buffer_validate Also we need to change the buffer validation to take into account the changes and to prevent buffer overflow. 7

Exercise 14.3: Calling buffer_validate Exercise 14.4: Implementing a circular FIFO buffer The implementation of a queue in Exercise 14.3 is not ideal we can only add items at the buffer_foot irrespective of where that is in the actual array. To solve this we need a circular queue where the item after the top element is the bottom element. To achieve this we need alter buffer_add and buffer_remove so that buffer_head, buffer_foot Wrap around. 8

FIFO buffer (circular queue): demo Exercise 14.4: buffer_add 9

Exercise 14.4: buffer_remove Exercise 14.4: buffer_validate 10

Linked lists In last weeks lab15 you encountered linked lists and were edit and add to a linked list database program. The data structure looked like: The user menu As it stood: 1. you could not go to the last student 2. You could not remove a student details 11

Exercise 15.4 1. You were asked to add a last pointer to the list data structure 2. Modify list_init so that last is initialised to be NULL 3. Modify the function list_last so that the current pointer pointed to the last record 4. Modify the function list_add so that the last pointer was given the address of the last record Exercise 15.4: solution 1 2 3 12

Exercise 15.4: list_add 4 Exercise 15.5: creating list_remove Three conditions we need to think about 1. Database is empty This is dealt with in function query_remove 2. Only one record in the database We need to free the memory and make first, current and last pointers NULL 3. Two or more records in database Bypass the first record and free the memory 13

Exercise 15.5: list_remove, step 2 2. Only one record in the database We need to free the memory and make first, current and last pointers NULL Exercise 15.5: list_remove, step 3 3. Two or more records in database Bypass the first record and free the memory 14

Exercise 15.5: list_remove, step 3. The code In the lab this week: Doubly linked lists In the program it has a previous option. This is much easier to implement with doubly linked lists. These are very useful also for FIFO (queues) 15