Implementation of Stack Using Array
Implementation of Stack Using Array
1
Push(stack, data)
2
{
3
if (stack is full)
4
return null
5
top top + 1
6
stack[top] data
7
}
8
Pop()
9
{
10
if(!isempty())
11
{
12
data = stack[top];
13
top = top - 1;
14
return data;
15
}
16
Else
17
Print empty
18
Implementation of stack using linked list
1.
struct node
2.
{
int data;
3.
node *link;
4.
};
5.
function push(node newtop, int
item)
6.
{
if(top==NULL)
7.
{
newtop.data :=
element
8.
newtop.next := NULL
9.
top := newtop
10.
}
else
{
11.
newtop.data := item
12.
newtop.next := top
13.
top := newtop
14.
}
}
15. function pop()
16.
{
if(top==NULL)
17.
Print empty
18.
else
19.
top := top.next
20.
}
23.
1
Simple insertion sort.
4
template <typename
Comparable>
5
void
insertionSort( vector<Comparable> &
a)
6 { for( int p = 1; p < a.size( ); ++p )
8{Comparable tmp = std::move( a[ p ] );
10 int j; for( j = p; j > 0 && tmp < a[ j 1 ]; --j )
12 a[ j ] = std::move( a[ j - 1 ] );
13 a[ j ] = std::move( tmp );
14
24.
front=rear=newnode
rear.next :=NULL
}
else
{ rear.next
:=newnode
25.
rear :=newnode
26.
rear.next :=NULL
}
}
27. function remove()
28.
{ if(front==NULL)
29.
Print Nothing
to remove
else
30.
front
:=front.next
Shellsort
4
template <typename
Comparable>
5
void
shellsort( vector<Comparable> & a )6
{ for( int gap = a.size( ) / 2;
gap > 0; gap /= 2 )
8
for( int i = gap; i <
a.size( ); ++i )
9
{ Comparable tmp =
std::move( a[ i ] );
11
int j = i;
12 for( ; j >= gap && tmp < a[ j - gap ];
j -= gap )
1 a[ j ] = std::move( a[ j - gap ] );
1 a[ j ] =std::move( tmp ); } }
59.
list=list->next;
60.
list->next=newnode;
61.
newnode->next=head;
Double hashing
1
insert key to hash table
h[tablesize]
2
void insert( const & key, int
tableSize ){
for(i=0 to
size)
3
{
fi=i*(R-(key
% R));
4
hash=(key+fi)
%size;
5
if(h[hash]==0)
6
h[hash]=key;
else I++;
}}
7
void remove(key)
8
{
for( i=0 to size )
9
{
fi=i*(R-(key %
R));
10
pos=(x+fi)%size;
11
if(h[pos]!=0)
h[pos]=0;
12 Else Print element not fount}
circular queue using linked list
1
struct node
2
{
int data
3
node *link
4
function
insert_at_begining(element,nod
e newnode)
5
{ newnode->data=element
if(start==NULL)
6
{
start=newnode
7
newnode>link=NULL
8
} else {
9
newnode>link=start
10
start=newnode
} }
11 function
insert_at_end(element,node
newnode)
12
{
newnode>data=element
13
if(start==NULL)
14
{
start=newnode
15
newnode>link=NULL
16
}
else
{
17
for(dis=start;dis>link!=NULL;dis=dis->link)
18
19
20
{
}
dis->link=newnode
newnode-
>link=NULL
21
} }
22 function
insert_at_position(element,node
newnode,position)
23
{
temp>data=element
for(count=1,del=start;count<p
osition;count++)
24
{
dis=del
25
del=del->link
26
}
dis>link=temp
27
temp->link=del
}
28 function delete_begining(node
start)
29
{
if(start!=NULL)
30
start=start->link
}
31
function delete_end(node
start)
32
{
if(start!=NULL)
33
{ if(start->link!
=NULL)
34
{
for(dis=start;dis->link!
=NULL;dis=dis->link)
35
{
temp=disg
}
36
temp->link=NULL
37
dis=dis->link
38
}
else
39
{
start=NULL
40
}
41
}
Implementation of doubly linked
listfunction insertBeginning(List list,
Node newNode)
1.
{
== null)
2.
3.
4.
5.
6.
if (list.firstNode
{
newNode
newNode
null
null
list.firstNode :=
list.lastNode :=
newNode.prev :=
newNode.next :=
7.
8.
9.
}
else
insertBefore(list,
list.firstNode, newNode)
10.
} function insertEnd(List
list, Node newNode)
11.
{
If( list.lastNode
== null)
12.
insertBeginning(list, newNode)
13.
else
14.
insertAfter(list,
list.lastNode, newNode)
15.
}
16.
function insertAfter(List list,
Node node, Node newNode)
17.
{
18.
newNode.prev :=
node
19.
newNode.next :=
node.next
20.
if node.next ==
null
21.
list.lastNode := newNode
22.
Else
23.
node.next.prev :=
newNode
24.
node.next :=
newNode
25.
}
26. function deletefront( List
list,Node node)
27.
{
if(node.nextnode==NULL)
28.
{ Delete(node)
Else
{
29.
node:=node.nextnode
30.
node.prev:=NULL
31.
}
}
32. function
delete_last(list
list,node node)
33.
{
If(node.next==NUL
L)
34.
{
node=NULL
35.
Delete(node)
36.
}
37.
else
38.
Node.next.prev:=node.prev
{
for(dis=start;dis->link!
=NULL;dis=dis->link)
32
{
temp=dis
}
temp>link=NULL
33
dis=dis->link
34
} else
{
35
start=NULL
36
}
37
}
38 of Binary search tree using
linked list
39
function insert( const
Comparable & x, BinaryNode *
&t)
40 8
{
41 9
if( t == nullptr )
42 10
t = new
BinaryNode{ x, nullptr, nullptr }
43 11
else if( x < t->element )
44 12
insert( x, t->left )
45 13
else if( t->element < x )
46 14
insert( x, t->right )
47 15
else
48 16
// Duplicate; do
nothing
49 17
}
50 18 function remove( const
Comparable & x, BinaryNode *
&t)
51 19
{ 20
if( t ==
nullptr )
52 21
return
53
if( x < t->element )
54 23
remove( x, t->left )
55 24
else if( t->element < x
)
56 25
remove( x, t->right )
57 26
else if( t->left !=
nullptr && t->right !=
nullptr ) // Two children
58 { t->element = findMin( t>right )->element
59 29
remove( t>element, t->right )
60 30
}else {
61 33
t = ( t->left !
= nullptr ) ? t->left : t->right }
}
62 36 function remove( const
Comparable & x, BinaryNode *
&t)
63 37
{
if( t ==
nullptr )
64 39
65
66
67
68
69
70
71
72
73
return;
if( x < t->element )
4
remove( x, t->left )
42 else if( t->element < x )
remove( x, t->right )
else if( t->left != nullptr && t>right != nullptr ) // Two
children {
t->element = findMin( t>right )->element
remove( t->element, t->right )
}
49
else50
{
51
BinaryNode *oldNode = t
52
t=
( t->left != nullptr ) ? t->left : t>right
53
delete oldNode;
54
}
Heap sort.
1
template <typename
Comparable>
2
void
heapsort( vector<Compar
able> & a )
3
{
4
for( int i = a.size( ) / 2 1; i >= 0; --i )
/*buildHeap */
5
percDown( a, i,
a.size( ) );
6
for( int j = a.size( ) - 1;
j > 0; --j )
{
7
std::swap( a[ 0 ], a[
j ] );
8
percDown( a, 0, j );
9
} }
10
11
Internal method for
heapsort.
i is the index of an item in the heap.
Returns the index of the left child.
12
inline int leftChild( int i )
13
{
return 2 * i + 1;
14
}
* Internal method
for heapsort that is used in
deleteMax and buildHeap.
* i is the position from which to
percolate down.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
mergeSort( a,
tmpArray, left, center );
mergeSort( a,
tmpArray, center + 1, right );
merge( a,
tmpArray, left, center + 1,
right );
}}
template <typename
Comparable>
void
merge( vector<Comparable>
& a, vector<Comparable> &
tmpArray,
int leftPos, int rightPos, int
rightEnd ) {
int leftEnd =
rightPos - 1; 14 int tmpPos =
leftPos;
int numElements =
rightEnd - leftPos + 1;
while( leftPos <=
leftEnd && rightPos <=
rightEnd )
if( a[ leftPos ] <=
a[ rightPos ] )
tmpArray[ tmpPos++ ] =
std::move( a[ leftPos++ ] );
Else
tmpArray[ tmpPos++ ] =
std::move( a[ rightPos++ ] );
while( leftPos <=
leftEnd )
// Copy rest
of first half
tmpArray[ tmpPos++ ] =
std::move( a[ leftPos++ ] );
while( rightPos <=
rightEnd )
// Copy rest of
right half
tmpArray[ tmpPos++ ] =
std::move( a[ rightPos++ ] );
// Copy tmpArray back
for( int i = 0; i <
numElements; ++i, --rightEnd
)
a[ rightEnd ] =
std::move( tmpArray[ rightEnd
] );
Quick sort
1
template <typename
Comparable>
2
3
4
5
6
7
8
9
10
void
quicksort( vector<Comparable
> & a, int left, int right )
{
if( left + 10 <= right )
{
const
Comparable & pivot =
median3( a, left, right );
int i = left, j = right
- 1;
for( ; ; )
{
while( a[ +
+i ] < pivot ) { }
while( pivot <
a[ --j ] ) { }
if( i < j )
std::swap( a[ i ], a[ j ] );
else
11
break;
}
12
std::swap( a[ i ],
a[ right - 1 ] );
quicksort( a, left, i - 1 );
quicksort( a, i + 1,right );
13
}
14
Else
//
Do an insertion sort on the
subarray
15
insertionSort( a, left,
right );
16 }
Dijkstras algorithm
1
void Graph::dijkstra( Vertex s )
2
{
for each Vertex v
3
{
v.dist = INFINITY;
4
v.known = false;
5
}
s.dist = 0;
6
while( there is an
unknown distance vertex )
7
{
Vertex v =
smallest unknown distance
vertex;
8
v.known = true;
9
for each Vertex
w adjacent to v
10
if( !
w.known )
11
{
DistType cvw = cost of edge from v
to w;
12
if( v.dist + cvw <
w.dist )
13
decrease( w.dist to v.dist +
cvw );
14
w.path = v;
6
v.known = false;
7
}s.dist = 0;
9while( there is an unknown
distance vertex )
10{Vertex v = smallest
unknown distance vertex;
12
v.known = true;
13for each Vertex w adjacent to
v
14
if( !w.known )
15
{
DistType cvw = cost of
edge from v to w;
17
if( v.dist + cvw <
w.dist )
18
{// Update w
20
vertex[itr->first].dist =
minimum(vertex[itr>first].dist,itr->second);
21
w.path = v;
24
}
25
}
Kruskal algorithm
vector<Edge> kruskal( vector<Edge>
edges, int numVertices )
{
DisjSets ds{ numVertices };
priority_queue pq{ edges };
vector<Edge> mst;