coen352_TA3[1]

7
1 COEN352 Data structure and algorithms – Tutorial 3 Tutor: May El Barachi elbar_m@encs.concordia.ca http://users.encs.concordia.ca/~elbar_m/ 2 Stacks A stack is a container of objects that are inserted and removed according to the LIFO principle Real life example: Plate dispenser Uses in computer engineering: Browsers and the “back” button Text editors and “undo” operation Two main operations: Push: Inserts an object on the top of the stack Pop: Remove and return the top object on the stack Array-based implementation of stack:

description

Algorithm

Transcript of coen352_TA3[1]

  • 1COEN352 Data structure and algorithms Tutorial 3

    Tutor: May El [email protected]://users.encs.concordia.ca/~elbar_m/

    2

    Stacks

    A stack is a container of objects that are inserted and removed according to the LIFO principle

    Real life example: Plate dispenser Uses in computer engineering:

    Browsers and the back button Text editors and undo operation

    Two main operations: Push: Inserts an object on the top of the stack Pop: Remove and return the top object on the stack

    Array-based implementation of stack:

  • 23

    Queues

    A queue is a container of objects that are inserted and removed according to the FIFO principle

    Real life example: waiting line in a shop Uses in engineering:

    Call center Two main operations:

    enqueue: Inserts an object at the rear of the queue dequeue: Remove and return the object at the front of the

    queue Array-based implementation of queue:

    4

    Array implementations of stacksand queues

    Array implementations of stack and queueare straightforward, but have one drawback: The size N of the array must be fixed in advance. These implementations are not very adaptable.

    Need to explore an alternate implementation.

  • 35

    Singly linked lists

    In its simplest form, a linked list is a collection of nodes that form a linear ordering together.

    Each node is a compound object that stores a data member (called its element) and a pointer (called next) to the next node in the list.

    6

    Consider a linked list that contains 3-character strings of common airport abbreviations.

    Moving from one node to another by following a next pointer is known as link hopping or pointer hopping.

    The first and last node is a linked list are called the head and tail of the list, respectively.

    There is also a HEAD and TAIL pointer variable.

    Singly linked lists

  • 47

    More flexible type of linked list that allows moving forward andbackward in the list

    Each node has two pointers, one pointing to the next element, and the other to the previous element

    We use two special nodes (header and trailer) to keep track of the beginning and the end of the list

    Doubly linked list

    8

    Exercise R- 4.11

    Describe how to insert an element at the beginning of a singly linked list, assuming that the list does not have a sentinel header node, and instead uses a pointer variable head to point to the first node in the list.

    Solution: - Create a new node with next pointing to head.- Set head pointing to the new node.

    new A

    head

    new A

    head

  • 59

    Exercise C- 4.2

    Suppose we are given an n-element array A whose elements are taken from the set {1,2,,n-1}. Describe a recursive algorithm for finding a repeated element in A. What is the running time of your algorithm?

    Solution:- Initialize an array B with size n-1 and all zero elements. - Scan array A from the index 0 towards n-1. For each element in A, check whether the corresponding element in B is one. - For example, if A[n-1]=7, then check whether B[6] is equal to 1. If yes,then 7 is a repeated element in A; if no, assign 1 to B[6].

    The running time complexity of this algorithm is O(n).

    10

    Code:Algorithm FindRepeatedElement(n, B)Input: Array A, Array B with size n-1, and all its elements are set to 0,repeatedElement = -1

    Output: repeatedElementif n = 1

    return repeatedElementelse

    if (B[A[n-1]- 1] = 0)B[A[n-1]- 1] = 1FindRepeatedElement(n-1, B)

    elserepeatedElement = A[n-1]return repeatedElement

    Exercise C- 4.2

  • 611

    Exercise C- 4.8Describe , in pseudo-code, a non-recursive method for finding , by link hopping, the middle node of a doubly linked list with header and trailer sentinels, and an odd number of real nodes between them (must only use link hopping). What is the running time of this method?

    Solution:Run a for loop, which begins at the two ends towards the middle.Finally, they arrive at the same element, which is the middle element.The running time of this method is O(n) .

    12

    Exercise C- 4.8Describe , in pseudo-code, a non-recursive method for finding , by link hopping, the middle node of a doubly linked list with header and trailer sentinels, and an odd number of real nodes between them(must only use link hopping). What is the running time of this method?

    Algorithm: FindMiddleNode(L, tmp_h, tmp_t)Input: A double linked list L with size n,tmp_h = header next, tmp_t = trailer prev

    Output: Middle node of Lfor (i = 0; i < (n+1)/2; i++)

    if L[tmp_h] = L[tmp_t]return L[tmp_h]

    elsetmp_h = tmp_hnexttmp_t = tmp_tprev

  • 713

    Exercise C- 4.9Describe how to implement the queue ADT using two stacks. What is the running time of the enqueue() and dequeue() functions in this case?

    Solution:To implement a queue using two stacks, stack1 and stack2, we can do the following: on enqueue() : push the element onto stack1. on dequeue() : pop each element from stack1 and push it onto stack2, until the

    last element is reached. Temporarily store this last element. Pop every- thing o of stack2 and push them back onto stack1. Return the last element.

    Enqueue takes 0(1) running time and dequeue takes 0(n) running time

    14

    Exercise C- 4.11Give an algorithm for concatenating two doubly linked lists L and M ,with header and trailer sentinel nodes, into a single list L , as in the previous exercise. What is the running time of this method?

    Solution:We have to use two temporary Node elements, temp1 and temp2.

    - Initialize temp1 to be the trailer node of L and temp2 to be the header node of M. - Make the element of temp1 have its next field point to temp2 and set the element of temp2 to have its prev field point to temp1.- Set to be L and then set the trailer node of to be the trailer node of M.

    This method takes constant, or O(1), amount of time.