You are on page 1of 4

Chapter 3: List, Stacks and Queues.

In order to understand this chapter, we need to learn basic terms


used in programming, such as modules and abstract data type (ADT).
Modules are logical units that do a specific job in a program. They were
created in order to ease the debugging of routines and for people to
work simultaneously on a program. Because of its independence, being
a subunit of a program, it is easier to modify the codes when something
goes wrong.
An abstract data type (ADT) is an extension of module design and it
consists of a set of operations (mathematical abstractions). Once these
operations are in a program, calling the appropriate function can
perform any operation. Examples of ADT are lists, queues, trees, etc.
1. List ADT
A general list has the form a1, a2, , an , with list being of size n. If a
list has a size 0, it is called null list and the position of element a i in a list
is i.
-

Simple Array Implementations of Lists: allows operations to be carried on


linear time, but the running time for insertion and deletion is slow and
the list size must be known in advance, so they are not commonly used
on lists.

Linked lists: lists not stored contiguously, so that moving an element


doesnt require moving entire sections. They contain elements and a
pointer to its successor (next pointer); if p is declared to be a pointer to
a structure, then the value stored in p is interpreted as the location. The
delete command can be executed in one pointer change, while the
insert command requires obtaining a new cell form the system by using
a malloc call and then executing two pointer maneuvers.

Programming details: a header or a dummy node is the sentinel node of


our linked list and its position is 0. This allows us to show the basic
pointer manipulations without obscuring the code with special cases,
such as deletions and insertions. These special cases become easy to
program with the use of the find and find_previous routines that can
require, in the worst case, an average running time of 0(n).

Common errors:
1. The most common error is when a pointer p is not pointing at a
valid part of memory or if its indirection is illegal; you must make
sure that the pointer is not NULL.
2. The only way to create a record that is not already declared is to
use the malloc command, but if you want to run down a list with a

pointer, it is not appropriate to use it. Also, keep in mind that the
number of calls to malloc should equal the size of the list, plus 1 if
there is a header, otherwise you will not have a working program.
3. The free(p) commando allows the program to reclaim a space that
is no longer usable, but the direction of the pointer does not
change
. It is recommended to free a cell after a deletion so
that intermingled routines and memory does not become a
problem to the efficiency of your program; in order to ensure this
you need to keep a temporary variable set to the cell that is going
to be disposed.
4. The command malloc(sizeof node_ptr) doesnt allocate space for a
structure, only for a pointer.
-

Doubly linked lists: Add an extra field to the data structure, containing a
pointer to the previous cell; this may add space requirement and
doubles the cost for insertion and deletions command, but it also
simplifies your program.

Circularly linked lists: When the last cell keep a pointer back to the first,
can be used with or without a header and with doubly linked lists.

Radix Sort and Multilists: Radix sort is known as card sort; it can be used
to sort numbers out of buckets by certain criteria within a running time
of 0(p(n+b)) and it ensures minimum failure on the program. While
Multilists are used when linking different arrays of lists between each
other, using headers, instead of using circularly linked links or ndimensional arrays when programming; this tool ensures a simple and
speedy code, but it can require memory space.

Cursor Implementation of Linked Lists: Cursors are an alternative when


pointers are not available. In order to do this we must keep a list of cells
that are not in any list (the freelist) with cell 0 as a header. Then a value
of 0 for next is the equivalent to a pointer and we code the equivalent
for malloc and free cells in the CURSOR_SPACE array; to perform a
malloc, the first element is removed from the list, while performing a
free routine we place the cell at the front of the freelist. If there is no
more space available, the routine sets p = 0. In order to use the cursor
functions we must pass and return the identical parameters as the
pointer implementation; they must follow the ADT specification and take
and perform specific operations

5. The Stack ADT

A stack is a list that can only delete and insert at the end of a list
(top); functions pop and push respectively. They are often known as LIFO
(last in, first out) lists, with pushes as input operations and pop and top,
as output. We must note that pop deletes de last inserted element and
when used on an empty cell it is considered an error in the ATD stack,
while performing a push on an empty cell means an error on the
implementation stack.
To create an empty stack we create a header node and with
make_null we set the pointer to NULL.
-

Linked List Implementation of Stacks: On a single linked list we


perform push by inserting at the top of the list, a pop by deleting
the element at the front and a top returns the value of the element
at the front of the list. While these operations take constant time,
the expenses of this implementation is that the calls to malloc and
free are expensive. In order to avoid this expense, we must create
a second stack that is empty, in order to push an element of the
first list there.

Array Implementation of Stacks: To implement an array of stacks,


we must note that each stack comes with the top of it (tos),
considered -1 for an empty stack. To push an element x on the
stack we must increment tos and then set STACK[tos] = x, with
STACK as the array representing the actual stack. To pop we set
the return value to STACK[tos] and then decrement tos.

The fields top_of_stack and stack_size are commonly used on stacks


implementation. Dispose_stack frees a stack structure, while
create_stack requires an argument in the array implementation to create
a stack.
6. The Queue ADT
A queue is a list that does insertion at one end of the list, while
deletion is performed at the other end. Basics operation are enqueue,
which inserts an element at the end of a list, and dequeue, which
deletes and returns the element at the star of the list.
- Array Implementations of Queues: For each queue data structure we
keep an array QUEUE[] and the positions q_front and q_rear and q_size
as the number of elements on a queue. The major problem with this
implementation is that queues, as stacks, stay small even in the
presence of a lot of operations; it can appear full after just 10 enqueues.
In order to solve this, q_front or q_rear needs to be wrapped around in
the beginning, but we must check the queue for emptiness and to keep

track of the size of the queue; we must ensure that enqueues are not
larger than the size of the queue

You might also like