You are on page 1of 6

Phys 499

Objectives of this lecture


An introduction to stacks, queues, and linked-lists

Stacks

A stack is a type of data structure that allows you access to a single


element at any given time: the very last element added to it. This is
what is called as a lifo structure (last-in first-out).

Most microprocessors use a stack-based architecture. When a function


is called, its return address and arguments are pushed onto a stack
and when that function terminates, those values are popped off from
the stack. These stack operations are built into microprocessors.

Note that one danger in recursive programming is that the stack that
keeps return address and arguments could overflow if too many
recursive calls are made.

See a demo of stack data structure by following this link:

https://www.cs.usfca.edu/~galles/visualization/StackArray.html

There are three basic operations on a stack: push onto a stack, pop off
an element from a stack, and verify whether the stack is empty or not.
Here we will give pseudocode for stack operations.

a) push operation

Algorithm push(newElement)

//increment index to the top element;


topElementIndex topElementIndex + 1
stack[topElementIndex] newElement

b) pop operation

Algorithm pop()

elementToReturn = stack[topElementIndex]
topElementIndex topElementIndex - 1
return elementToReturn
c) isEmpty operation

Algorithm isEmpty()
if topElementIndex = -1
return true
else
return false

In this weeks homework assignment we will go over an example of


stack data structure use in parsing.

Queues

A queue works like a line at a movie theater: the first person to join
the rear of the line is the first person to reach to the front of the line to
buy a ticket. A queue is similar to a stack except in this behavior: the
first element added to it is the first element to be removed from it.
This is what is called as a fifo structure, first-in first-out.

See a demo of queue data structure by following this link:

https://www.cs.usfca.edu/~galles/visualization/QueueArray.html

There are two basic queue operations, insert and remove. Below is
the pseudocode for these basic operations. Note that in a queue data
structure two location identifiers (pointers) are kept: one for the
beginning of the queue and the other for the end of the queue. Having
two pointers makes both insertion and addition efficient. In the
pseudocode below the variable endOfQueueIndex will be used as the
pointer to the last element in a queue and beginningOfQueueIndex
will be used as the pointer to the first element in a queue.

a) insert operation

Algorithm insert(newElement)

// increment the index to the next available slot in the


queue
endofQueueIndex endOfQueueIndex +1
queue[endOfQueueIndex] = newElement

b) remove operation
Algorithm remove()

elementToRemove = queue[beginningOfQueueIndex]
beginningOfQueueIndex beginningOfQueueIndex + 1
return elementToRemove

Note that the pseudocode discussed above assumes an array


implementation of queues. It does not go into details of how array
boundary is handled. This is a detail left to the implementation.

One example of the usage of queue data structure is in WEB servers,


which need to handle multiple incoming requests. Typically, requests
are queued and threads running inside a WEB server are handed over
incoming requests based on the arrival order.

Linked-Lists

A singly linked-list is a collection of nodes that contain two essential


pieces of information: data maintained in the node and information
about how to access the next node, that is, a pointer to the next
element. If information on how to access the previous node is added
to each node, a singly linked-list becomes a doubly linked-list. In
other words, in a singly linked-list one can only access the next
element in the list and in a doubly linked-list one can access both the
next element and the previous element in the list. See Figures 1 and
2 below for singly linked-list and doubly linked-list.

Figure 1. A singly linked-list of n elements

Figure 2. A doubly linked-list of n elements

See a demo of linked-list data structure implementing a stack by


following this link:
https://www.cs.usfca.edu/~galles/visualization/StackLL.html

A linked-list data structure has an advantage over an array collection


in that there is no restriction on the number of elements it can hold.
Structure could grow and shrink dynamically depending on the need.
Array size, on the other hand, need to be determined before an array
structure is created and resizing an array is a costly operation. On the
other hand, accessing a particular element in a linked-list is not as
efficient as accessing an array element: time to access a particular
element depends on the location of that element in the linked-list
structure. In an array, on the other hand, the time to access an
element is the same for all elements.

A linked-list data structure typically maintains a pointer to the first


element in the list. This pointer provides the entry into the structure.
All basic operations start with this pointer, which is sometimes called
header.

Basic operations on a linked-list are inserting a new element, deleting


an element, searching for an element, and traversing all elements.
Below is the pseudocode for these operations:

a) Algorithm insert(newElement)

// create a new data node to add to the linked-list


newNode = allocateNewNode(newElement)

// just add the new element to the beginning of the list


// newElements address becomes the entry point to the list
newNode.nextElement header
header address(newNode)

b) Algorithm delete(element)

previousNode null
nextNode header

while nextNode != null and nextNode.element != element do


// Keep a reference to the previous element
previousNode nextNode

// Move to the next element


nextNode nextNode nextElement
end while

// The loop above terminates either when the element is found


or
// when the list is completely traversed and the element is not
found.
If nextNode != null
// This implies that the element pointed by nextNode is the
one
// to be deleted. So, the node before it should now point
to the
// node after it.
PreviousNode nextElement = nextElement
nextElement
deallocate memory used by nextElement
else
// Else block will be entered when element is not found.
Nothing
// to do here

c) Algorithm find(element)

nextNode header

while nextNode != null and nextNode.element != element do


// Move to the next element
nextNode nextNode nextElement
end while

// The loop above terminates either when the element is found


or
// when the list is completely traversed and the element is not
found.
If nextNode != null
return nextNodeelement
else
// Else block will be entered when element is not found.
return null
d) Algorithm traverseList()

nextNode header

while nextNode != null and nextNode.element != element do


// Work on the operation on the current element
someOperation(nextNode)

// Move to the next element


nextNode nextNode nextElement
end while

You might also like