please help me to write a program toinsertion and deletion in array
Feb 12, 2008 at 4:57pm UTC
please give me a program for insertion for insertion and deletion in an array
Last edited on Feb 12, 2008 at 5:05pm UTC
Feb 12, 2008 at 4:58pm UTC
program to push and pop in an array stack
Feb 12, 2008 at 4:58pm UTC
program to insert and delete in an array queue
Feb 12, 2008 at 4:59pm UTC
program to convert given decimal number to binary
Feb 13, 2008 at 1:45am UTC
i think thers a decimal to binary converter in the sourcode section of this site
Feb 13, 2008 at 10:48am UTC
You need an Object Oriented solution or just a structured one (C or C++)?
Which data type you need to put into your stack and queue?
If you need a generic data type solution can you use C++ templates?
Feb 13, 2008 at 12:07pm UTC
I can give you an Object-Orientd implementation with templates that supports all data types stack and queue.
QUEUE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
///////////////////////////////////////////////////
// COPY FROM HERE
#include <iostream.h>
///////////////////////////////////////////////////
#define QUEUE_SIZE 5 // MODIFY THIS!!!
///////////////////////////////////////////////////
template <class T>
class queue {
public :
queue();
~queue();
bool enqueue(T elem);
T dequeue();
bool isEmpty();
bool isFull();
void empty();
private :
T data[QUEUE_SIZE + 1];
unsigned long head;
unsigned long tail;
unsigned long next(unsigned long val);
};
///////////////////////////////////////////////////
template <class T>
queue<T>::queue() {
head = tail = 0;
}
template <class T>
queue<T>::~queue() {
}
template <class T>
bool queue<T>::enqueue(T elem) {
if (! isFull()) {
data[tail] = elem;
tail = next(tail);
return true ;
}
else {
return false ;
}
}
template <class T>
T queue<T>::dequeue() {
T elem;
if (! isEmpty()) {
elem = data[head];
head = next(head);
}
return elem;
}
template <class T>
bool queue<T>::isEmpty() {
return head == tail;
}
template <class T>
bool queue<T>::isFull() {
return next(tail) == head;
}
template <class T>
void queue<T>::empty() {
head = tail = 0;
}
template <class T>
unsigned long queue<T>::next(unsigned long val) {
if (val == QUEUE_SIZE)
return 0;
else
return val + 1;
}
// COPY TILL HERE
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// TEST PROGRAM
void pause() {
char c;
cin >> c;
}
void main() {
queue<int > q;
if (q.isEmpty())
cout << "queue is empty" << endl;
else
cout << "queue is not empty" << endl;
cout << "add numbers from 1 to 4" << endl;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
cout << "extracted the first element: " << q.dequeue() << endl;
if (q.isFull())
cout << "queue is full" << endl;
else
cout << "queue is not full" << endl;
cout << "add numbers from 5 to 6" << endl;
q.enqueue(5);
q.enqueue(6);
if (q.isFull())
cout << "queue is full" << endl;
else
cout << "queue is not full" << endl;
// now q should be full and then next values shoud not be enqueued
cout << "add numbers from 7 to 8" << endl;
q.enqueue(7);
q.enqueue(8);
/////////////////////////////////
// print q extracting elements //
cout << "print queue extracting elements:" << endl;
cout << "queue = {" ;
if (q.isEmpty()) {
cout << " }" << endl;
}
else {
while (! q.isEmpty()) {
cout << q.dequeue() << ", " ;
}
cout << "\b\b}" << endl;
}
// print q extracting elements //
/////////////////////////////////
if (q.isEmpty())
cout << "queue is empty" << endl;
else
cout << "queue is not empty" << endl;
pause();
}
STACK:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
///////////////////////////////////////////////////
// COPY FROM HERE
#include <iostream.h>
///////////////////////////////////////////////////
#define STACK_SIZE 5 // MODIFY THIS !!!
///////////////////////////////////////////////////
template <class T>
class stack {
public :
stack();
~stack();
bool push(T elem);
T pop();
T top();
bool isEmpty();
bool isFull();
void empty();
private :
T data[STACK_SIZE];
long topelem;
};
///////////////////////////////////////////////////
template <class T>
stack<T>::stack() {
topelem = -1;
}
template <class T>
stack<T>::~stack() {
}
template <class T>
bool stack<T>::push(T elem) {
if (! isFull()){
data[topelem++] = elem;
return true ;
}
else {
return false ;
}
}
template <class T>
T stack<T>::pop() {
T elem;
if (! isEmpty()) {
elem = data[--topelem];
}
return elem;
}
template <class T>
T stack<T>::top() {
T elem;
if (! isEmpty()) {
elem = data[topelem];
}
return elem;
}
template <class T>
bool stack<T>::isEmpty() {
return topelem == -1;
}
template <class T>
bool stack<T>::isFull() {
return topelem == STACK_SIZE - 1;
}
template <class T>
void stack<T>::empty() {
topelem = -1;
}
// COPY TILL HERE
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// TEST PROGRAM
void pause() {
char c;
cin >> c;
}
void main() {
stack<int > s;
if (s.isEmpty())
cout << "stack is empty" << endl;
else
cout << "stack is not empty" << endl;
cout << "add numbers from 1 to 4" << endl;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
cout << "extracted top element from the stack: " << s.pop() << endl;
if (s.isFull())
cout << "stack is full" << endl;
else
cout << "stack is not full" << endl;
cout << "add numbers from 5 to 6" << endl;
s.push(5);
s.push(6);
if (s.isFull())
cout << "stack is full" << endl;
else
cout << "stack is not full" << endl;
// now s should be full and then next values shoud not be pushed
cout << "add numbers from 7 to 8" << endl;
s.push(7);
s.push(8);
/////////////////////////////////
// print s extracting elements //
cout << "print stack extracting elements:" << endl;
cout << "stack = {" ;
if (s.isEmpty()) {
cout << " }" << endl;
}
else {
while (! s.isEmpty()) {
cout << s.pop() << ", " ;
}
cout << "\b\b}" << endl;
}
// print s extracting elements //
/////////////////////////////////
if (s.isEmpty())
cout << "stack is empty" << endl;
else
cout << "stack is not empty" << endl;
pause();
}
I hope this is useful for you, anyway if you need something more different I can write other code for you.
Last edited on Feb 13, 2008 at 1:09pm UTC
Topic archived. No new replies allowed.