CSS 133 Computer Programming for Engineers II

Linked Lists, Stack, Queue

Professor Pisan

1 Doubly

What can we do that we could not before?

What is the disadvantage?

How would removeNode change?

Review Code: https://github.com/pisan133/doubly-linkedlist

Lab: printBackward, findLargest, findSmallest, swapNodes

2 Extending Linked Lists Linked List: flexible

● insertAtFront, insertAtBack ● addBefore, addAfter ● contains ● empty ● removeNode, swap ● size

Stack: LIFO - Last In, First Out

● push, pop, top

Queue: FIFO - First in, First Out

● push, pop, front ● older versions: enqueue, dequeue, front

3 Linked List vs Array Arrays

● Fixed size ● Insert anywhere other than the end takes extra time ● Random access to elements possible ○ Binary search for ● Forward/backward traversal

Linked List

● Extendable ● Has to allocate/deallocate memory ● Flexible ○ Need front and back ptr if we want to insert at end ○ Need double linked list to traverse forwards and backwards ● No random access ○ Maintaining a sorted list easy ○ Sorting is time consuming ○ No binary search 4 Stack LIFO - Last In, First Out

● push, pop, top

● Function call, push return address onto the stack ● Backtracking - remember the series of choices, when stuck, go back and change the choice ○ http://cs.lmu.edu/~ray/notes/backtracking/ ● Reverse-Polish Notation: 3 4 + ○ Efficient for computer math. No parentheses. ○ Operators applied to the operands at the bottom of the stack

5 Implementation

Array

● Have a maximum stack size ● Keep track of index of the last element ● First element goes to index 0, next one at index 1, etc

Linked List

● Use insertAtFront for push, removeFromFront for pop (alternatively insert and remove at back)

6 Queue FIFO - First in, First Out

● push, pop, front ● older versions: enqueue, dequeue, front

Store objects and process them in the order they came

● Printer queue -- print the jobs in order

7 Implementation

Array

● Keep track of front and back of queue ○ First element goes to index 0, next one at index 1, etc ○ Dequeue removes element at index-0, then index-1, etc ● Option 1 (expensive) ○ Fill the array, shift all elements when we reach end of array ● Option 2: ○ Treat the array as circular ○ When we get to end of the array, wrap around and use the beginning

Linked List

● Use insertAtBack for enqueue, removeFromFront for dequeue

8 reverse a Linked List 0 0 20 30 40 50 20 30 40 50 void reverse(Node* head) { // only have dummy head if (head->nextPtr == NULL || head->nextPtr->nextPtr == NULL) return; Node* a = NULL; Node* b = head->nextPtr; // currently have a-->b, want to get b-->a and advance while (b != NULL) { Node* c = b->nextPtr; b->nextPtr = a; a = b; b = c; } // point head to the front (which used to be last) head->nextPtr = a; }

9 Lab-3: C Doubly Linked List https://github.com/pisan133/doubly-linkedlist printBackward, findLargest, findSmallest, swapNodes

// Linked list example from class exercise https://github.com/pisan133/linked-list

10 Review ● C Functions ○ printf, fgets, atoi, atof, typedef, struct, sizeof ○ malloc, free ○ casting ● Unix commands ○ ls, cd, pwd, mkdir, rmdir, chmod, cat, man, cp, mv, man, nano ● Bitwise Operators: & | ^ << >> ~ ● Singly Linked List structure ○ addBefore, addAfter, addSorted, removeNode, swap ● Doubly Linked List structure ● String functions ○ strlen, strcpy, strcat, strcmp ● Stack and Queue ● In-class exercises: BMI, Secret Key, Imaginary Numbers

Exam will only have C programming not C++

11 Clear as Mud

Anonymous end of class feedback

1. Encourage student reflection 2. Assist students in using time to study effectively 3. Provides information to instructor to correct misconceptions 4. Is more effective than asking for questions 5. Is fun!

http://bit.ly/ypmuddy

12