Skip to content

Commit aef3adc

Browse files
committed
Optimized code
1 parent ae1f7ac commit aef3adc

File tree

5 files changed

+101
-33
lines changed

5 files changed

+101
-33
lines changed

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

linked_list.cpp

100644100755
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@ class LinkedList
2020
size = 0;
2121
start = NULL;
2222
}
23-
struct node<T> *create_node(T value)
23+
struct node<T> *create_node(const T value)
2424
{
2525
struct node<T> *temp = new (struct node<T>);
2626
temp->next = NULL;
2727
temp->data = value;
2828
return temp;
2929
}
3030
// functions to insert values
31-
void
32-
insert_begin(T value)
31+
void insert_begin(const T value)
3332
{
3433
struct node<T> *temp = create_node(value);
3534
if (start != NULL)
@@ -39,7 +38,7 @@ class LinkedList
3938
start = temp;
4039
size++;
4140
}
42-
void insert(T value)
41+
void insert(const T value)
4342
{
4443
struct node<T> *temp = start;
4544
if (temp != NULL)
@@ -54,7 +53,7 @@ class LinkedList
5453
insert_begin(value);
5554
}
5655
}
57-
void insert_pos(T value, int pos)
56+
void insert_pos(const T value, int pos)
5857
{
5958
if (pos == 1)
6059
{
@@ -81,7 +80,7 @@ class LinkedList
8180
}
8281
}
8382
// search the list
84-
int search(T value)
83+
int search(const T value)
8584
{
8685
struct node<T> *temp = start;
8786
for (int pos = 1; temp != NULL; pos++)
@@ -95,7 +94,7 @@ class LinkedList
9594
return -1;
9695
}
9796
// delete from list
98-
void remove_pos(int pos)
97+
void remove_pos(const int pos)
9998
{
10099
if (pos > 1 && pos <= size)
101100
{
@@ -121,7 +120,7 @@ class LinkedList
121120
cout << "\nNo such entry in list\n";
122121
}
123122
}
124-
void remove(T value)
123+
void remove(const T value)
125124
{
126125
remove_pos(search(value));
127126
}

queue.cpp

100644100755
Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
#include <iostream>
2-
#include <cstdlib>
1+
#include <bits/stdc++.h>
32
using namespace std;
4-
const int MAX_SIZE = 100;
3+
class QueueFullException : public exception
4+
{
5+
virtual const char *what() const throw()
6+
{
7+
return "\nQueue is full\n";
8+
}
9+
};
10+
class QueueEmptyException : public exception
11+
{
12+
virtual const char *what() const throw()
13+
{
14+
return "\nQueue is empty\n";
15+
}
16+
};
517
//generic implementation of standard Queue data structure using templates
618
template <class T>
719
class Queue
820
{
921
private:
22+
static const int MAX_SIZE = 100;
1023
T data[MAX_SIZE];
1124
int front;
1225
int rear;
@@ -18,28 +31,46 @@ class Queue
1831
rear = 0;
1932
}
2033

21-
void Enqueue(T element)
34+
void Enqueue(const T element)
2235
{
2336
if (Size() == MAX_SIZE - 1)
24-
cout << "Queue full\n";
25-
data[rear] = element;
26-
rear = ++rear % MAX_SIZE;
37+
{
38+
QueueFullException e;
39+
throw e;
40+
}
41+
else
42+
{
43+
data[rear] = element;
44+
rear = ++rear % MAX_SIZE;
45+
}
2746
}
2847

29-
int Dequeue()
48+
T Dequeue()
3049
{
3150
if (isEmpty())
32-
cout << "Queue empty\n";
33-
T ret = data[front];
34-
front = ++front % MAX_SIZE;
35-
return ret;
51+
{
52+
QueueEmptyException e;
53+
throw e;
54+
}
55+
else
56+
{
57+
T ret = data[front];
58+
front = ++front % MAX_SIZE;
59+
return ret;
60+
}
3661
}
3762

38-
int Front()
63+
T Front()
3964
{
4065
if (isEmpty())
41-
cout << "Queue empty\n";
42-
return data[front];
66+
{
67+
QueueEmptyException e;
68+
throw e;
69+
}
70+
else
71+
{
72+
return data[front];
73+
}
4374
}
4475

4576
int Size()

stack.cpp

100644100755
Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
#include <iostream>
22
using namespace std;
3-
const int MAX_SIZE = 100;
3+
class StackoverflowException : public exception
4+
{
5+
virtual const char *what() const throw()
6+
{
7+
return "\nStack overflow\n";
8+
}
9+
};
10+
class StackunderflowException : public exception
11+
{
12+
virtual const char *what() const throw()
13+
{
14+
return "\nStack underflow\n";
15+
}
16+
};
417
//generic implementation of standard Stack data structure using templates
518
template <class T>
619
class Stack
720
{
821
private:
22+
static const int MAX_SIZE = 100;
923
T data[MAX_SIZE];
1024
int top;
1125

@@ -15,37 +29,61 @@ class Stack
1529
top = -1;
1630
}
1731

18-
void Push(T element)
32+
void Push(const T element)
1933
{
2034
if (top >= MAX_SIZE)
2135
{
22-
cout << "Stack overflow\n";
36+
StackoverflowException e;
37+
throw e;
38+
}
39+
else
40+
{
41+
data[++top] = element;
2342
}
24-
data[++top] = element;
2543
}
2644

2745
T Pop()
2846
{
29-
if (top == -1)
47+
if (top <= -1)
3048
{
31-
cout << "Stack underflow\n";
49+
StackunderflowException e;
50+
throw e;
51+
}
52+
else
53+
{
54+
return data[top--];
3255
}
33-
return data[top--];
3456
}
3557

3658
T Top()
3759
{
38-
return data[top];
60+
if (top <= -1)
61+
{
62+
StackunderflowException e;
63+
throw e;
64+
}
65+
else
66+
{
67+
return data[top];
68+
}
3969
}
4070

4171
int Size()
4272
{
43-
return top + 1;
73+
if (top <= -1)
74+
{
75+
StackunderflowException e;
76+
throw e;
77+
}
78+
else
79+
{
80+
return top + 1;
81+
}
4482
}
4583

4684
bool isEmpty()
4785
{
48-
return (top == -1) ? true : false;
86+
return (top <= -1) ? true : false;
4987
}
5088
};
5189

0 commit comments

Comments
 (0)