Beruflich Dokumente
Kultur Dokumente
Linked list
Array
Stack
queue
Linked list
A linked list is a linear data structure where each
element is a separate object.
Each element (we will call it anode) of a list is
comprising of two items - the data and a reference to the
next node. The last node has a reference tonull. The
entry point into a linked list is called the headof the list.
It should be noted that head is not a separate node, but
the reference to the first node. If the list is empty then the
head is a null reference.
Circular linked list - where last node of the list points back to
the first node (or the head) of the list.
Head
Data
items
Next
Data
items
Next
Data
items
Next
Inserting "after"
Find a node containing "key" and insert a new node
after
it. In the picture below, we insert a new node
after "e":
if(tmp != null)
tmp.next = new Node<AnyType>(toInsert,
tmp.next);
}
Inserting "before"
Find a node containing "key" and insert a new node
before that node. In the picture below, we insert a new
node before "a":
For the sake of convenience, we maintain two
referencesprevandcur. When we move along the list we
shift these two references, keepingprevone step
beforecur. We continue untilcurreaches the node before
which we need to make an insertion. Ifcurreaches null,
we don't insert, otherwise we insert a new node
betweenprevandcur.
Insert
before
Add Last
The method appends the node to the end of the list.
This requires traversing, but make sure you stop at the
last node
public void addLast(AnyType item)
{
if(head == null) addFirst(item);
else
{
Node<AnyType> tmp = head;
while(tmp.next != null) tmp =
tmp.next;
tmp.next = new
Node<AnyType>(item, null);
}
}
if( head.data.equals(key) )
{
head = head.next;
return;
}
Arrays
-a data structure, which stores a fixed-size sequential collection of elements of the same
type.
Declaring Array Variables:
To use an array in a program, you must declare a variable to reference the array, and you
must
specify the type of array the variable can reference. Here is the syntax for declaring an
array
variable:
dataType[] arrayRefVar; // preferred way.
or
dataType arrayRefVar[]; // works but not preferred way.
Creating Arrays:
You can create an array by using the new operator with
the following syntax:
arrayRefVar = new dataType[arraySize];
Declaring an array variable, creating an array, and
assigning the reference of the array to the variable can
be combined in one statement.
dataType[] arrayRefVar = new dataType[arraySize]; or
dataType[] arrayRefVar = {value0, value1, ..., valuek};
Processing Arrays
When processing
array elements, we often
use either for loop or
foreach loop because
all of the elements in an
array are of the same
type and the size of the
Output
array is known.`
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5
Stack
A stack is an abstract data type ADT, commonly used in
most programming languages. It is named stack as it
behaves like a real-world stack, for example deck of
cards or pile of plates etc.
A real-world stack allows operations at one end only.
Stack ADT allows all data operations at one end only. At
any given time, We can only access the top element of a
stack. This feature makes it LIFO data structure. LIFO
stands for Last-in-first-out. Here, the element which is
placed inserted or added last, is accessed first.
In stack terminology, insertion operation is called PUSH
operation and removal operation is called POP operation.
Basic Operations
push pushing storing an element on the stack.
pop removing accessing an element from the stack.
When data is PUSHed onto stack. To use a stack
efficiently we need to check status of stack as well. For
the same purpose, the
following functionality is added to stacks
peek get the top data element of the stack, without
removing it.
isFull check if stack is full.
isEmpty check if stack is empty.
PUSH Operation
Push
Operation
Top
d
c
stac
k
stac
k
Pop Operation
Accessing the content while removing it from stack,
is known as pop operation. In array
implementation of pop operation, data element is
not actually removed, instead top is decremented to
a lower position in stack to point to next value. But
in linked-list implementation, pop actually removes
data element and deallocates memory space.
Pop
Operation
Top
d
c
stac
k
stac
k
Top
QUEUE
Queue is an abstract data structure, somewhat similar to
stack. In contrast to stack, queue is opened at both end.
One end is always used to insert data enqueue and the
other is used to remove data dequeue. Queue follows
First-In-First-Out methodology, i.e., the data item stored
first will be accessed first.
Enqueue Operation
As queue maintains two data pointers, front and rear, its operations are
comparatively more difficult to implement than stack.
The following steps should be taken to enqueue insert data into a queue
Step 1 Check if queue is full.
Step 2 If queue is full, produce overflow error and exit.
Step 3 If queue is not full, increment rear pointer to point next empty space.
Step 4 Add data element to the queue location, where rear is pointing.
Step 5 return success.
Dequeue Operation
Accessing data from queue is a process of two tasks access the data where
front is pointing and
remove the data after access. The following steps are taken to perform dequeue
operation
Step 1 Check if queue is empty.
Step 2 If queue is empty, produce underflow error and exit.
Step 3 If queue is not empty, access data where front is pointing.
Step 3 Increment front pointer to point next available data element.
Step 5 return success.