midterm2015spring

# Page 3 of 8 problem 2 12 points consider the

• Test Prep
• 8
• 100% (1) 1 out of 1 people found this document helpful

This preview shows pages 3–7. Sign up to view the full content.

Page 3 of 8

This preview has intentionally blurred sections. Sign up to view the full version.

Problem 2. (12 points): Consider the following structure definitions: typedef struct queue queue_t; typedef struct node node_t; struct node { node_t *next; void *val; }; struct queue { node_t *head; node_t *tail; }; For this problem you are to complete a handful of methods that implement a queue data structure. The following program demonstrates their use: queue_t *q = make_queue(); enqueue(q, "supercali"); printf("%s", dequeue(q)); enqueue(q, "fragi"); enqueue(q, "listic"); enqueue(q, "expiali"); enqueue(q, "docious"); char *c = NULL; while ((c = dequeue(q))) { printf("%s", c); } free(q); When run, the program’s output is as follows: supercalifragilisticexpialidocious Note that there are no memory leaks. Complete the implementations of make_queue , enqueue and dequeue on the following page. Page 4 of 8
/* allocates and returns an empty queue */ queue_t *make_queue () { queue_t *q = malloc(sizeof(_______________)); q->head = q->tail = NULL; return q; } /* add val at the tail of the queue q */ void enqueue (queue_t *q, void *val) { if (q->head == NULL) { _______________ = _______________ = malloc(sizeof(_______________)); _______________ = NULL; } else { _______________ = _______________ = malloc(sizeof(_______________)); _______________ = NULL; } q->tail->val = val; } /* removes and returns the item at the head of the queue q, * or NULL if q is empty. */ void *dequeue (queue_t *q) { if (q->head == NULL) { return NULL; } void *val = _______________; node_t *p = _______________; q->head = _______________; free(p); if (q->head == NULL) { q->tail = NULL; } return val; } Page 5 of 8

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.
• Spring '09
• Saelee
• Pointer, printf, Exit, Parent process, SIGCHLD

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern