To search a list of n objects, the LIST-SEARCH procedure takes Θ(n) time in the worst case, since it may have to search the entire list. An attribute L.head points to the first element of the If x.next = NIL, the element x has no successor and is therefore the lastĮlement, or tail, of the list. If x.prev = NIL, the element x has no predecessor and is therefore the first element, or head, of the list. Successor in the linked list, and x.prev points to its predecessor. Given an element x in the list, x.next points to its The object mayĪlso contain other satellite data. Unlike an array, however, in which the linear order is determined by the array indices, the order in a linked list is determined by a pointer in each object.Įach element of a doubly linked list L is an object withĪn attribute key and two other pointer attributes: next and prev. Procedures to insert elements into and delete elements from both ends of a dequeĪ linked list is a data structure in which the objects are arranged in a linear order. Queue) allows insertion and deletion at both ends. Queue allows insertion at one end and deletion at the other end, a deque (doubleended Whereas a stack allows insertion and deletion of elements at only one end, and a ![]() In stack T, the top is the rightmost element of T. ![]() Initially, set T.top = 0 and R.top = n + 1.Įssentially, stack T uses the first part of the array and stack R uses the last The PUSH and POP operations should run in O(1) time. Neither stack overflows unless the total number of elements in both stacks together is n. Explain how to implement two stacks in one array A in such a way that Initially empty stack S stored in array S. Illustrate the result of each operation in the sequence When Q.head = Q.tail + 1, the queue is full, and if we attempt to enqueue an element, then the queue overflows. If we attempt to dequeue an element from an empty queue, the queue underflows. When Q.head = Q.tail, the queue is empty. Sense that location 1 immediately follows location n in a circular order. , Q.tail - 1, where we “wrap around” in the The attribute Q.tail indexes the next location at which a newly arriving element will be inserted into the queue. The queue has an attribute Q.head that indexes, or points Operation DEQUEUE like the stack operation POP, DEQUEUE takes no element argument. INSERT operation on a queue ENQUEUE, and we call the DELETE The three stack operations below takes O(1) time. To pop an empty stack, we say the stack underflows, which is normally an error. When S.top = 0, the stack contains no elements and is empty. ![]() The stack consists of elements S, where S is theĮlement at the bottom of the stack and S is the element at the top. The INSERT operation on a stack is often called PUSH, and the DELETE operation, which does not take an element argument, is often called POP. Has been in the set for the longest time: the queue implements a first-in, first-out, ![]() Queue, the element deleted is always the one that The set is the one most recently inserted: the stack implements a last-in, first-out, or LIFO, policy Stacks and queues are dynamic sets in which the element removed from the set by the DELETE operation is prespecified.
0 Comments
Leave a Reply. |