Data Structure
Data Structure
Introduction
Stacks
Queues
Linked Lists
Sorting
Searching
Introduction
Linked lists
Allow insertions and removals anywhere
Stacks
Allow insertions and removals only at top of stack
Queues
Allow insertions at the back and removals from the front
Stacks
Stack
New nodes can be added and removed only at the top
Similar to a pile of dishes
Last-in, first-out (LIFO)
Bottom of stack indicated by a link member to NULL
Constrained version of a linked list
push
Adds a new node to the top of the stack
pop
Removes a node from the top
Stores the popped value
Returns true if pop was successful
B
top A
C
top B
A
D
top C
B
A
top
E
D
C
B
A
top
D
C
B
A
Stack Operation:
CreateS, isEmpty, isFull
Stack createS(max_stack_size) ::=
#define MAX_STACK_SIZE 100 /* maximum stack size */
typedef struct {
int key;
/* other fields */
} element;
element stack[MAX_STACK_SIZE];
int top = -1;
Boolean isEmpty(Stack) ::= top< 0;
Push
void push(int *top, element item)
{
/* add an item to the global stack */
if (*top >= MAX_STACK_SIZE-1) {
stack_full( );
return;
}
stack[++*top] = item;
}
Pop
element pop(int *top)
{
/* return the top element from the stack */
if (*top == -1)
return stack_empty( ); /* returns and error key */
return stack[(*top)--];
}
Queues
Queue
Similar to a supermarket checkout line
rear
front
B
A
C
rear B
front A
D
rear C
B
front A
rear
D
C
front B
rear
front
Comments
queue is empty
Job 1 is added
Job 2 is added
Job 3 is added
Job 1 is deleted
Job 2 is deleted
Opearation:
createQ, isEmptyQ, isFullQ
Queue createQ(max_queue_size) ::=
# define MAX_QUEUE_SIZE 100/* Maximum queue size */
typedef struct {
int key;
/* other fields */
} element;
element queue[MAX_QUEUE_SIZE];
int rear = -1;
int front = -1;
Boolean isEmpty(queue) ::= front == rear
Boolean isFullQ(queue) ::= rear == MAX_QUEUE_SIZE-1
Enqueue (Input) :
void enqueue(int *rear, element item)
{
/* add an item to the queue */
if (*rear == MAX_QUEUE_SIZE_1) {
queue_full( );
return;
}
queue [++*rear] = item;
}
Dequeue (Output):
element dequeue(int *front, int rear)
{
/* remove element at the front of the queue */
if ( *front == rear)
return queue_empty( ); /* return an error key
*/
return queue [++ *front];
}
Linked Lists
Linked list
Linear collection of self-referential class objects,
called nodes
Connected by pointer links
Accessed via a pointer to the first node of the list
Subsequent nodes are accessed via the link-pointer
member of the current node
Link pointer in the last node is set to null to mark the
lists end
Use a linked list instead of an array when
You have an unpredictable number of data elements
Your list needs to be sorted quickly
Linked Lists
Types of linked lists:
Singly linked list
Forward pointer of the last node points to the first node and backward
pointer of the first node points to the last node
Linked lists
Inserting a node
Before insertion into a linked list, we first apply the
searching algorithm. If the flag returned from the searching
algorithm is false, we will allow insertion, otherwise we
abort the insertion algorithm, because we do not allow data
with duplicate values. Four cases can arise:
Inserting into an empty list.
Insertion at the beginning of the list.
Insertion at the end of the list.
Insertion in the middle of the list.
Deleting a node
Before deleting a node in a linked list, we apply the search
algorithm. If the flag returned from the search algorithm is
true (the node is found), we can delete the node from the
linked list. However, deletion is simpler than insertion: we
have only two casesdeleting the first node and deleting
any other node. In other words, the deletion of the last and
the middle nodes can be done by the same process.
SEARCHING TECHNIQUES
1-LINEAR (SEQUENTIAL) SEARCH
2-BINARY SEARCH
SEARCHING TECHNIQUES
To finding out whether a particular element is
present in the list.
2 methods: linear search, binary search
The method we use depends on how the elements
2.
BINARY SEARCH
List must
be a sorted one
We compare the element with the element placed
approximately in the middle of the list
If a match is found, the search terminates
successfully.
Otherwise, we continue the search for the key in a
similar manner either in the upper half or the
lower half.
Baba?
Eat?
Sorting: Definition
Sorting: an operation that segregates items into
groups according to specified criterion.
A={3162134590}
A={0112334569}
Bubble Sort
Selection Sort
Merge Sort
The end of the list has been reached so this is the end of the first pass. The
twelve at the end of the list must be largest number in the list and so is now in
The 12
is greater
3 so
theypass
are exchanged.
the correct
position.
Wethan
now the
start
a new
from left to right.
The 12 is greater than the 7 so they are exchanged.
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
6, 6,
2,
2, 9, 9,
11, 3,
11,
9, 7,
11,
3, 11,
7, 12
Notice that this time we do not have to compare the last two
numbers as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2,
6,
9,
9,
3,
7,
11,
12
Third Pass
2, 6, 9, 3,
9, 9,
7, 9,
3,
7, 11, 12
This time the 11 and 12 are in position. This pass therefore only
requires 5 comparisons.
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2,
6,
9,
9,
3,
7,
11,
12
Third Pass
2,
6,
9,
3,
7,
9,
11,
12
Fourth Pass
2, 6, 3,
9, 9,
7, 9,
3,
7, 9, 11, 12
Each pass requires fewer comparisons. This time only 4 are needed.
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2,
Third Pass
2,
Fourth Pass
2,
Fifth Pass
2,
6,
6,
6,
6,
3,
9,
9,
3,
3,
6,
9,
3,
7,
7,
3,
7,
9,
9,
7,
9,
9,
9,
11,
11,
11,
11,
12
12
12
12
The list is now sorted but the algorithm does not know this until it
completes a pass with no exchanges.
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2,
Third Pass
2,
Fourth Pass
2,
Fifth Pass
2,
Sixth Pass
2,
6,
6,
6,
3,
3,
9,
9,
3,
6,
6,
9,
3,
7,
7,
7,
3,
7,
9,
9,
9,
7,
9,
9,
9,
9,
11,
11,
11,
11,
11,
12
12
12
12
12
This pass no exchanges are made so the algorithm knows the list is
sorted. It can therefore save time by not doing the final pass. With
other lists this check could save much more work.
40
43
65
40
43
-1 58
40
-1 43
-1 40
-1 58
42
42
65
42
58 65
42
43 58 65
-1
40
-1
40 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
42 43 58 65
2.
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
Largest
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
Largest
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
Largest
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
Largest
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
1
Comparison
Data Movement
Sorted
Selection Sort
Largest
Comparison
Data Movement
Sorted
Selection Sort
2
Comparison
Data Movement
Sorted
Selection Sort
DONE!
Comparison
Data Movement
Sorted
Selection
Sort:
Example
40 2 1 43 3 65 0 -1 58 3
42
40
43
-1 58
42 65
40
43
-1 42
58 65
40
-1 42 43 58 65
40
-1 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
-1
40 42 43 58 65
Selection
Sort: Code
For (i=0;i<N-1;i++){
pos = i;
for(j=i+1;j<N;j++){
if ( arr[pos] > arr[j] )
pos = j;}
if(pos != i )
{ swap(&arr[i],&arr[p]); }
}
Mergesort
Mergesort (divide-and-conquer)
Divide array into two halves.
A
A
L
L
R
R
T
I
H
T
M
H
S
M
divide
Mergesort
Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
A
divide
sort
Mergesort
Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
Merge two halves to make sorted whole.
A
divide
sort
merge
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted
smallest
T
auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted
smallest
auxiliary array