queue c and data structures baojian hua [email protected]

23
Queue C and Data Structures Baojian Hua [email protected]

Post on 19-Dec-2015

237 views

Category:

Documents


0 download

TRANSCRIPT

Queue

C and Data StructuresBaojian Hua

[email protected]

Linear List Recall that a linear list has the form:

The delete and insert operations may insert or delete an arbitrary element e_i

If the delete is restricted at one end and insert at the other end, we get a queue

Example: Queue of Char

‘a’insert

‘b’

‘a’

insert

insert

‘b’

‘a’

‘c’

delete‘b’

‘c’

Abstract Data Types in C: Interface// in file “queue.h”#ifndef QUEUE_H#define QUEUE_H

typedef struct queue *queue;

queue new ();int size (queue q);int isEmpty (queue q);void enQueue (queue q, poly x);poly deQueue (queue q);poly getHead (queue q);

#endif

Implementation Using Extensible Array// in file “arrayQueue.c”

#include “queue.h”

struct queue

{

arrayList l;

};

// Recall the box strategy:

lq

Operations: “new”queue new (){ queue q = checkedMalloc (sizeof (*q)); q->l = newArrayList ();

return q;}

0 n-1

array

max

tail

lq

Operations: “size”int size (queue q){ return arrayListLength (q->l);}

0 n-1

array

max

tail

lq

Operations: “isEmpty”int isEmpty (queue q){ return arrayListIsEmpty (q->l);}

0 n-1

array

max

tail

lq

Operations: “enQueue”void enQueue (queue q, poly x){ arrayListInsertLast (stk->l, x); return;}

0 n-1

array

max

tail

lq

Operations: “deQueue”poly deQueue (queue q){ if (! arrayListIsEmpty (q->l)) return arrayListDeleteFirst (q->l); error (“empty queue”); return NULL; }

Operations: “deQueue”

0 n-1

array

max

tail

lq

Operations: “deQueue”

0 n-1

array

max

tail

lq

Analysis

What’s the complexity? enQueue: O(1) deQueue: O(n)

data movement Can we do better?

Lazy approach better amortized performance

Circular queue

Lazy Approach

Instead of moving data when “deQueue”, we move data only when reaching the tail of the queue O(n) on n operations which has O(1) amortized cost

Lazy Approach

What’s necessary modification? Leave this as a programming

assignment

0 n-1

array

max

tail

lq

Circular Queue

A refinement of the lazy approach is the circular queue

0 n-1

array

max

tail

lq

The formula:

tail = (tail+1)%max;

head = (head+1)%max;head

Implementation Using Linked List// in file “linkedQueue.c”

#include “queue.h”

struct queue

{

linkedList l;

};

lq

data

next

data

next

data

next…

Operations: “new”queue new (){ queue q = checkedMalloc (sizeof (*q)); q->l = newLinkedList ();

return q;}

lq

/\

/\

Operations: “size”int size (queue q){ return linkedListLength (q->l);}

lq

data

next

data

next

data

next…

Operations: “isEmpty”int isEmpty (queue q){ return linkedListIsEmpty (q->l);}

lq

data

next

data

next

data

next…

Operations: “enQueue”void enQueue (queue q, poly x){ // note the difference with extensible array- // based representation linkedListInsertLast (q->l, x); return;}

lq

data

next

data

next

data

next…

Operations: “deQueue”poly deQueue (queue){ if (! linkedListIsEmpty (q->l)) return linkedListDeleteFirst (q->l);

error (“empty queue”); return NULL;}

lq

data

next

data

next

data

next…

Analysis

What’s the complexity of these operations? enQueue: O(n)

search the last element deQueue: O(1)

Improvement: Circular linked list leave as programming assignment