Stacks - -peek get value of next element to be removed...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Stacks, Queues, Insertion Sort -both special cases of lists -stacks defined as doing insertions and deletions only at one end of the list (head or tail) and never in the middle -queues do insertions at one end and deletions at the other Stacks Queues Add Head(“push”) Tail(“enqueue”) Remove Head(“pop”) Head(“dequeue”) Type Last-In, First-Out (LIFO) First-In, First-Out (FIFO) Uses -poles in the Towers of Hanoi -evaluation of expressions -function calls -process scheduling in OS -message server for internet chat room -calls arriving at a call center -printer queue Standard Queue and Stack operations -create/initialize -need to set head, tail pointers to null, or -allocate array space -add – push/enqueue -remove –pop/dequeue -returns the stored value -error if stack/queue is empty -isEmpty – status of the stack/queue -isFull -useful for array implementations -error to push/enqueue if full
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: -peek get value of next element to be removed without removing it-clear less common, usually just process to empty Insertion Sort-same rough efficiency of selection sort T(n) proportional to n2-recall merge sort T(n) prop to nlogn-a different solution for same problem (6;3,5,8,2) //before ; - already sorted, after to be sorted-assume first m elements sorted-want forst m+1 elements to be sorted-take element m+1 out-shift all elements in 1 st m that are greater than m+1 st element 1 to the right temp 3 (6; , 5, 8, 2) ( , 6; 5, 8, 2) (3, 6; 5, 8, 2) void insertionSort(int arr, const int length) { for(int top=1;top<length; top++){ int temp = arr[top]; int I=top; while(I>0 && temp<arr[I-1]){ arr[I]=arr[I-1]; //shift one to the right i--; } arr[I]=temp; //move new element into blank, left by shifting } }...
View Full Document

This note was uploaded on 04/17/2008 for the course ECE APS105 taught by Professor Maclean during the Spring '08 term at University of Toronto- Toronto.

Page1 / 2

Stacks - -peek get value of next element to be removed...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online