Sie sind auf Seite 1von 23

Derived Data types

Those data types which are implementation


independent as they can be implemented in one or
other way are known as derived data types. These
data types are normally built by combination of primary
or built-in data types and associated operations on
them.

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.

Types of linked list


Single linked list - Item Navigation is forward only.
Double linked list - a variation of Linked list in which
navigation is possible in both ways either forward and backward
easily as compared to Single Linked List.

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

Linked list Basic operation


Insertion add an element at the beginning of the list.
Deletion delete an element at the beginning of the
list. Display
displaying complete list.
Search search an element using given key.
Traversing displaying complete list. Start with the
head and access each node until you reach null. Do not
change the head reference.

Linked List Operations


Insertion -add an element at the beginning of the list.

public void addFirst(AnyType item)


{
head = new Node<AnyType>(item, head);
}

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":

public void insertAfter(AnyType key, AnyType


toInsert)
{
Node<AnyType> tmp = head;
while(tmp != null && !tmp.data.equals(key)) tmp
= tmp.next;

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.

public void insertBefore(AnyType key, AnyType toInsert)


{
if(head == null) return null;
if(head.data.equals(key))
{
addFirst(toInsert);
return;
}

Node<AnyType> prev = null;


Node<AnyType> cur = head;

while(cur != null && !cur.data.equals(key))


{
prev = cur;
cur = cur.next;
}
//insert between cur and prev
if(cur != null) prev.next = new Node<AnyType>(toInsert,
cur);
}

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);
}
}

DeletionFind a node containing "key" and delete it. In the


picture below we delete a node containing "A
The algorithm is similar to insert "before" algorithm. It
is convinient to use two referencesprevandcur. When
we move along the list we shift these two references,
keepingprevone step beforecur. We continue
untilcurreaches the node which we need to delete.
There are three exceptional cases, we need to take care
of:
1. list is empty
2. delete the head node
3. node is not in the list

public void remove(AnyType key)


{
if(head == null) throw new RuntimeException("cannot delete");

if( head.data.equals(key) )
{
head = head.next;
return;
}

Node<AnyType> cur = head;


Node<AnyType> prev = null;

while(cur != null && !cur.data.equals(key) )


{
prev = cur;
cur = cur.next;
}

if(cur == null) throw new RuntimeException("cannot delete");

//delete cur node


prev.next = cur.next;
}

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

public class TestArray {


public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}

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

The process of putting a new data element onto


stack is known as PUSH Operation. Push
operation involves series of steps
Step 1 Check if stack is full.
Step 2 If stack is full, produce error and exit. Top
Step 3 If stack is not full, increment top to point
next empty space. Step 4 Add data element to
the stack location, where top is pointing.
Step 5 return success.

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

A POP operation may involve the


following steps
Step 1 Check if stack is empty.
Step 2 If stack is empty, produce
error and exit.

Top

d
c

Step 3 If stack is not empty, access


the data element at which top is
pointing.

Step 4 Decrease the value of top by


1.

stac
k

stac
k

Step 5 return success.

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.

Queue operations may involve initializing or defining the


queue, utilizing it and then completing erasing it from memory.
enqueue add store an item to the queue.
dequeue remove access an item from the queue.
Few more functions are required to make above mentioned
queue operation efficient. These are peek get the element at front of the queue without
removing it.
isfull checks if queue is full.
isempty checks if queue is empty.

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.

Das könnte Ihnen auch gefallen