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.
}
implementation of linear queue using
array
1.
*queue as queue[size]
2. function insert(int item)
3.
{ if(queue.isfull)
4.
Print full
5.
else
{
6.
if(front==-1)
7.
front++
8.
else
queue[++top]=item
}
}
9.
function remove()
10.
{
if(queue.isempty())
11.
Print underflow
12.
else
13.
{
return
stack[top--]
14.
if(front>rear)
15.
front=rear=1 }
}
linear queue using linked list
16. struct node
17. {
int data;
18.
struct node *next;
19. };
20. function insert(node
newtop,int item)
21.
{
if(front==NULL)
22.
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 ); } }
plementation of circular queue
31.
* declare circular queue as
cqueue[size]
32. */
33. function insert(int item)
34.
{
If(cqueue.Isfull())
35.
print circular
Queue is full
36.
else
{
rear=(rear+1)%size
37.
cqueue[rear]=item
38.
}
}
39. function remove()
40.
{ if(cqueue.isfull())
41.
Print circular queue
is empty
else
{
42.
return
cqueue[front]
43.
front=(front+1)%size } }
44.
f singly circular linked list
45. struct link_list
46. { int no;
47.
struct link_list *next;
48. };insert_front(item)
49. { if(list!=NULL)
50.
{
Newnode->
51.
newnode>next=list=head;
52.
while(list->next!=head)
53.
list=list->next;
54.
list->next=newnode;
55.
head=newnode;
56.
} }Insert end()
57. {
list=head;
58.
while(list->next!=head)
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
of singly linked listALG ORITHM:
1
struct node
2
{
int data
3
node *link
4
}//function
insert_at_begining(element,nod
e newnode)
5
{ newnode>data=element //insert the
data element inside the new
nodes data field
6
if(start==NULL)
7
{ start=newnode
8
newnode>link=NULL
9
} else {
10
newnode>link=start
11
start=newnode} }
12 function
insert_at_end(element,node
newnode)
13
{
newnode>data=element
14
if(start==NULL)
15
{ start=newnode
16
newnode>link=NULL
17
} else {
18 for(dis=start;dis->link!
=NULL;dis=dis->link)
{
}
19
dis->link=newnode
20
newnode>link=NULL } }
21 function
insert_at_position(element,node
newnode,position)
22
{
temp>data=element
for(count=1,del=start;count<p
osition;count++) {
dis=del
23
del=del->link }
24
dis->link=temp
25
temp->link=del
}
26 function delete_begining(node
start)
27
{
if(start!=NULL)
28
start=start->link
}
29
function delete_end(node
start)
30
{
if(start!=NULL)
31
{
if(start>link!=NULL)
{
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.
* n is the logical size of the binary heap.
15
*/
16
template <typename
Comparable>
17
void
percDown( vector<Compa
rable> & a, int i, int n )
18
{
19
int child;
20
Comparable tmp;
21
for( tmp =
std::move( a[ i ] );leftChild(
i ) < n; i = child )
22
{
23
child =
leftChild( i );
24
if( child != n - 1
&& a[ child ] < a[ child
+1])
25
++child;
26
if( tmp <
a[ child ] )
27
a[ i ]
=std::move( a[ child ] );
28
else
29
break;
30
}
31
a[ i ]
=std::move( tmp );
32
}
Merge sort
1
* Mergesort algorithm
(driver).
2
template <typename
Comparable>
3
void
mergeSort( vector<Comparab
le> & a )
{ vector<Comparable> tmpArray(
a.size( ) );
mergeSort( a, tmpArray, 0,
a.size( ) - 1 );
4
}template <typename
Comparable>
5
void
mergeSort( vector<Comparab
le> & a,
6
vector<Comparable> &
tmpArray, int left, int right )
7
{
8
if( left < right )
9
{
10
int center = ( left
+ right ) / 2;
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;
Minimum spanning tree using Prims
algorithm
1
void
Graph::dijkstra( Vertex s ) 3
for each Vertex v
4
{v.dist = INFINITY;
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;
while( mst.size( ) != numVertices - 1 )
{
Edge e = pq.pop( ); // Edge e = (u, v)
SetType uset = ds.find( e.getu( ) );
SetType vset = ds.find( e.getv( ) );
if( uset != vset )
{// Accept the edge
mst.push_back( e );
ds.union( uset, vset );
}}
return mst;
}