We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20
Dynamic Data Structures
CEN116 Algorithms and Programming II
Stacks • Can be implemented as a constrained version of a linked list • Add new nodes and remove existing ones only at the top • Last-in, first-out (LIFO) data structure • Access a stack via a pointer to its top element • Link member in the stack’s last node is set to NULL to indicate the stack’s bottom • Diagram illustrates a stack with several nodes—stackPtr points to the stack’s top element Stacks Primary Stack Operations • push creates a new node and places it on top of the stack • pop removes a node from the stack’s top, frees that node’s memory and returns the popped value Implementing a Stack • Example program implements a simple stack of integers • Program allows you to • push a value onto the stack (function push), • pop a value off the stack (function pop) • terminate the program Function push • Places a new node onto the stack using the following steps: • Call malloc to create a new node, then assign the allocated memory’s address to newPtr • Assign to newPtr->data the pushed value • Assign *topPtr to newPtr->nextPtr – link member of new top node now points to the previous top node • Assign newPtr to *topPtr – modifies stackPtr in main to point to new top Function push Function pop • Removes the stack’s top node • Assign *topPtr to tempPtr – used to free the node’s memory • Assign (*topPtr)->data to popValue to save the top node’s value • Assign (*topPtr)->nextPtr to *topPtr so stackPtr in main now points to what was previously the stack’s second element (or NULL) • Free the memory pointed to by tempPtr • Return popValue Function pop Applications of Stacks • Stacks have many interesting applications • Whenever a function call is made, the called function must know how to return to its caller, so the return address is pushed onto a stack • In a series of function calls, the successive return addresses are pushed onto the stack in last-in, first-out order so that each function can return to its caller • Stacks contain the space created for automatic local variables on each invocation of a function • When the function returns to its caller, the space for that function’s automatic variables is popped off the stack, and these variables are no longer known to the program • Stacks are sometimes used by compilers when evaluating expressions and generating machine-language code Queues • A queue is similar to a checkout line in a grocery store: • The first person in line receives service first. • Other customers enter the line only at the end and wait for service. • You remove queue nodes only from its head (front) and insert nodes only at its tail (back) • First-in, first-out (FIFO) data structure • Insert and remove operations are known as enqueue (pronounced “en-cue”) and dequeue (pronounced “dee-cue”) Queues Queue Applications • For computers that have only a single processor, only one user at a time may be serviced. Entries for the other users are placed in a queue. Each entry gradually advances to the front of the queue as users receive service. The entry at the front of the queue is the next to receive service. • Similarly, for today’s multicore systems, there could be more programs running than there are processors. The programs not currently running are placed in a queue until a currently busy processor becomes available. Queues • Queues also support print spooling. An office may have only one printer. Many users can send documents to print at a given time. When the printer is busy, additional documents are spooled to memory or secondary storage, just as sewing thread is wrapped around a spool until it’s needed. The documents wait in a queue until the printer becomes available. • Information packets traveling over computer networks, like the Internet, also wait in queues. Each time a packet arrives at a network node, it must be routed to the next node on the network along the path to its final destination. The routing node routes one packet at a time, so additional packets are enqueued until the router can route them. Queues Illustrating a Queue • Note the separate pointers to the queue’s head and tail Implementing a Queue • Example program performs queue manipulations • insert a node in the queue (function enqueue) • remove a node from the queue (function dequeue) Function enqueue • Receives three arguments: • address of headPtr—the pointer to the queue’s head, • address of tailPtr—the pointer to the queue’s tail • value to insert • Steps: • Call malloc to create a new node and assign the allocated memory location to newPtr • If memory was allocated, assign value to newPtr->data, and assign NULL to newPtr->nextPtr • If queue is empty, assign newPtr to *headPtr – new node is queue’s head & tail • Otherwise, assign newPtr to (*tailPtr)->nextPtr – new node is tail • Assigns newPtr to *tailPtr to update the tail pointer to the new tail Function enqueue Function dequeue • Receives as arguments the addresses of the queue’s head and tail pointers • Removes the queue’s first node • Steps: • Assign (*headPtr)->data to value to save the data being dequeued • Assign *headPtr to tempPtr – used to free the memory. • Assign(*headPtr)->nextPtr to *headPtr so that the queue’s head pointer in main now points to the new head node • Check whether *headPtr is NULL. If so, assign NULL to *tailPtr because the queue is now empty. • Free the memory pointed to by tempPtr • Return value to the caller Function dequeue Trees • Nonlinear, two-dimensional data structures • Nodes contain two or more links • Binary tree nodes contain two links, as in the diagram at the right • None, one, or both of the links in each node may be NULL • The root node is the first node in a tree. • Each link in the root node refers to a child • The left child is the first node in the left subtree, and the right child is the first in the right subtree. • A given node’s children are called siblings • A node with no children is a leaf node • Trees are drawn with the root at the top—opposite of trees in nature Trees References • C How to Program, Ninth Edition by Deitel & Deitel, Pearson, 2022.