Simple C Dynamic Data Structure

Similar documents
gcc o driver std=c99 -Wall driver.c bigmesa.c

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

Pointer Casts and Data Accesses

Linked Lists. C Linked List

CS 1044 Program 6 Summer I dimension ??????

gcc o driver std=c99 -Wall driver.c everynth.c

Given that much information about two such rectangles, it is possible to determine whether they intersect.

Creating a String Data Type in C

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

CS 2604 Minor Project 1 DRAFT Fall 2000

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

CS 2604 Minor Project 1 Summer 2000

Decision Logic: if, if else, switch, Boolean conditions and variables

Pointer Accesses to Memory and Bitwise Manipulation

A Capacity: 10 Usage: 4 Data:

Pointer Accesses to Memory and Bitwise Manipulation

Pointer Accesses to Memory and Bitwise Manipulation

Both parts center on the concept of a "mesa", and make use of the following data type:

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

The Program Specification:

CS2304 Spring 2014 Project 3

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

CS 1044 Project 1 Fall 2011

Motivation was to facilitate development of systems software, especially OS development.

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Accessing Data in Memory

CS201 - Introduction to Programming Glossary By

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

CS 1044 Project 2 Spring 2003

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

CSCE 110 PROGRAMMING FUNDAMENTALS

Motivation was to facilitate development of systems software, especially OS development.

Short Notes of CS201

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

COMP 524 Spring 2018 Midterm Thursday, March 1

CS 1044 Program 2 Spring 2002

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS 2704 Project 1 Spring 2001

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

3. When you process a largest recent earthquake query, you should print out:

Do not start the test until instructed to do so!

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

CS 2301 Exam 3 B-Term 2011

"apple" "grape" "grape" "grape" "apple"

CS 2604 Minor Project 3 DRAFT Summer 2000

The following notes illustrate debugging a linked list implementation with gdb.

15-213/18-213/15-513, Spring 2018 C Programming Lab: Assessing Your C Programming Skills

15-213/18-213/15-513, Fall 2017 C Programming Lab: Assessing Your C Programming Skills

Introduction to Programming in C Department of Computer Science and Engineering

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

a f b e c d Figure 1 Figure 2 Figure 3

CS 215 Fundamentals of Programming II Spring 2018 Project 5

CS201 Some Important Definitions

CS 216 Exam 1 Fall SOLUTION

struct Properties C struct Types

C Introduction. Comparison w/ Java, Memory Model, and Pointers

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

This examination has 11 pages. Check that you have a complete paper.

Armide Documentation. Release Kyle Mayes

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Phase One: Process and Semaphore Queue Modules

Lab 4 - Linked Lists

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Ashish Gupta, Data JUET, Guna

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Do not start the test until instructed to do so!

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

PESIT-BSC Department of Science & Humanities

Programming. Lists, Stacks, Queues

Invoice Program with Arrays and Structures

You must include this cover sheet. Either type up the assignment using theory3.tex, or print out this PDF.

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Dynamic Data Structures

CS 111X - Fall Test 1

Programming Assignments #4. Group project: Priority-based Job Scheduling

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Largest Online Community of VU Students

Chapter 5. The Standard Template Library.

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

File Navigation and Text Parsing in Java

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Project No. 2: Process Scheduling in Linux Submission due: April 12, 2013, 11:59pm

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Recitation #11 Malloc Lab. November 7th, 2017

Transcription:

C Programming Simple C Dynamic Data Structure For this assignment, you will implement a program that manipulates a simple queue of integer values. Your program will include the following features: IntegerDT IntegerQueue Queue wrapper type to "duct-tape" an int32_t value to a queue node wrapper supplying a suitable queue interface for a queue of integers purely generic queue implementation, similar to generic doubly-linked list from notes To facilitate testing, your program will also make use of a supplied library for parsing files of commands. Your solution must conform exactly to the descriptions below, and your code must be organized exactly as described below. You will find header files for the elements described above on the course website. You must not make any changes to those files. For convenience, the header files are replicated at the end of this specification. You must implement the first three elements described above, placing the implementations in C source files IntegerDT.c, IntegerQueue.c, and Queue.c, respectively. Memory management requirements You must allocate the objects that make up the queue dynamically. As is the case with any non-garbage-collected language, it is your responsibility to deallocate all memory that you allocate dynamically as soon as you are done with it. The test code will not detect whether you have done this, but a TA will review your implementation to verify that you have done so. What to Submit You will create an uncompressed tar file containing your three implementation files: IntegerDT.c, IntegerQueue.c and Queue.c, and nothing else, and submit that tar file to the Curator. This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment, so use them wisely. Test your functions thoroughly before submitting them. Make sure that your functions produce correct results for every logically valid test case you can think of. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: http:www.cs.vt.edu/curator/ 1

Pledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: On my honor: - I have not discussed the C language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from another source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. <Student Name> Failure to include this pledge in a submission is a violation of the Honor Code. 2

Interfaces: Queue.h: /* Our queues have two header elements: the "front" just before the first element and the "rear" just after the last element. The 'prev' link of the front header is null, as is the 'next' link of the rear header. Their other two links point toward each other via the interior elements of the queue. An empty list looks like this: +-------+ +------+ [- front <---> rear -] +-------+ +------+ A queue with two elements in it looks like this: +-------+ +-------+ +-------+ +------+ [- front <---> 1 <---> 2 <---> rear -] +-------+ +-------+ +-------+ +------+ The symmetry of this arrangement eliminates lots of special cases in queue processing. (Because only one of the pointers in each header element is used, we could in fact combine them into a single header element without sacrificing this simplicity. But using two separate elements allows us to do a little bit of checking on some operations, which can be valuable.) This implementation of a queue does not require use of dynamically allocated memory. Instead, each structure that is a potential list element must embed a QNode member. All of the list functions operate on these QNode's. The QList_Entry macro allows conversion from a QNode back to a structure object that contains it. For example, suppose there is a need for a queue of 'struct Widget'. 'struct Widget' should contain a 'QNode' member, like so: struct Widget { QNode node; int bar;...other members... Then a list of 'struct Widget' can be be declared and initialized like so: Queue Widget_Q; QList_Init(&Widget_Q); Retrieval is a typical situation where it is necessary to convert from a QNode back to its enclosing structure. Here's an example using Widget_Q: QNode *e = Queue_Pop(Widget_Q); struct Widget *f = Queue_Entry (e, struct Widget, node); now, do something with f... The interface for this queue is inspired by the queue<> template in the 3

C++ STL. If you're familiar with queue<>, you should find this easy to use. However, it should be emphasized that these lists do *no* type checking and can't do much other correctness checking. If you screw up, it will bite you. Glossary of Queue terms: - "front": The first element in a queue. Undefined in an empty queue. Returned by Queue_Front(). - "back": The last element in a queue. Undefined in an empty list. Returned by Queue_Back(). - "tail": The element figuratively just after the last element of a queue. Well-defined even in an empty queue. Not returned by any Queue function. - "head": The element figuratively just before the first element of a queue. Well-defined even in an empty list. Not returned by any Queue function. */ - "interior element": An element that is not the head or tail, that is, a real queue element. An empty queue does not have any interior elements. Queue node: struct _QNode { struct _QNode *prev; struct _QNode *next; previous list element next list element typedef struct _QNode QNode; Queue object: struct _Queue { QNode front; QNode rear; sentinel node at the front of the queue sentinel node at the tail of the queue typedef struct _Queue Queue; Converts pointer to queue element NODE into a pointer to the structure that NODE is embedded inside. Supply the name of the outer structure STRUCT and the member name MEMBER of the NODE. See the big comment at the top of the file for an example. #define Queue_Entry(NODE, STRUCT, MEMBER) \ ((STRUCT *) ((uint8_t *) (NODE) - offsetof (STRUCT, MEMBER))) Initialize QNode pointers to NULL. Pre: pn points to a QNode object Post: pn->prev and pn->next are NULL void QNode_Init(QNode* pn); 4

Initialize Queue to empty state. Pre: pq points to a Queue object Post: *pq has been set to an empty state (see preceding comment void Queue_Init(Queue* pq); Return whether Queue is empty. Pre: pq points to a Queue object Returns true if *pq is empty, false otherwise bool Queue_Empty(const Queue* const pq); Insert *pnode as last interior element of Queue. Pre: pq points to a Queue object pnode points to a QNode object Post: *pnode has been inserted at the rear of *pq void Queue_Push(Queue* const pq, QNode* const pnode); Remove first interior element of Queue and return it. Pre: pq points to a Queue object Post: the interior QNode that was at the front of *pq has been removed Returns pointer to the QNode that was removed, NULL if *pq was empty QNode* const Queue_Pop(Queue* const pq); Return first interior element, if any. Pre: pq points to a Queue object Returns pointer first interior QNode in *pq, NULL if *pq is empty QNode* const Queue_Front(Queue* const pq); Return last interior element, if any. Pre: pq points to a Queue object Returns pointer last interior QNode in *pq, NULL if *pq is empty QNode* const Queue_Back(Queue* const pq); 5

IntegerQueue.h: /* IntegerQueue defines an interface suitable for a client who needs to create and manipulate a queue of integer values (int32_t). */ IntegerQueue uses the data wrapper IntegerDT, and the Queue type for the underlying physical structure. struct _IntegerQueue { Queue Q; generic Queue object typedef struct _IntegerQueue IntegerQueue; Initialize empty IntegerQueue. Pre: piq points to an IntegerQueue object Post: piq->q has been initialized to an empty state (see Queue.h for details) void IntegerQueue_Init(IntegerQueue* const piq); Return whether IntegerQueue is empty. Pre: piq points to aqn IntegerQueue object Returns true if piq->q is empty, false otherwise bool IntegerQueue_Empty(const IntegerQueue* const piq); Insert K at rear of IntegerQueue. Pre: piq points to an IntegerQueue object Post: An IntegerDT object containing the value K has been inserted at the rear of piq->q void IntegerQueue_Push(IntegerQueue* const piq, int32_t K); Remove and return data value from front of IntegerQueue. Pre: piq points to a nonempty IntegerQueue object Post: The IntegerDT object at the front of piq->q has been removed Returns the user data value that was in the IntegerDT object at the front of piq->q int32_t IntegerQueue_Pop(IntegerQueue* const piq); 6

IntegerDT.h: Wrapper type to "duct-tape" an int32_t payload to a QNode. struct _IntegerDT { int32_t payload; user data field QNode node; generic node typedef struct _IntegerDT IntegerDT; Insert K into payload field and set node pointers to NULL. Pre: pdt points to an IntegerDT object Post: pdt->payload == K pdt->node has its pointer fields set to NULL void IntegerDT_Init(IntegerDT* const pdt, int32_t K); 7