example1.cpp ------------------------------------------------------ #include #define SIZE 10 int list[SIZE] = {12, 23, 34, 45, 56, 67, 78}; int total = 7; void insertData(int data, int p); void deleteData(int p); void printList(const char *words); using namespace std; int main(){ printList("Printing list"); insertData(39, 3); printList("After inserting 39 at 3"); deleteData(2); printList("After deleting item 2"); return 0; } void printList(const char *words){ cout << words << endl; if (total > 0) { for (int i = 0; i < total; i++) cout << list[i] << " "; cout << endl << endl; } else cout << "List is empty" << endl; } void insertData(int data, int p){ if (total >= SIZE){ cout << "List is full. Cannot insert data." << endl; return; } if (p < 0 || p > total){ cout << "Invalid position. Cannot insert data." << endl; return; } for (int i = total; i > p; i--) list[i] = list[i - 1]; list[p] = data; total++; } void deleteData(int p){ if (total <= 0){ cout << "List is empty. Cannot delete data." << endl; return; } if (p < 0 || p >= total){ cout << "Invalid position. Cannot delete data." << endl; return; } for (int i = p; i < total - 1; i++) list[i] = list[i + 1]; total--; } ============================================================= example2.cpp #include #define SIZE 4 using namespace std; int STACK[SIZE]; int TOP = -1; void push(int data){ if (TOP < SIZE - 1){ TOP++; STACK[TOP] = data; } else cout << "Stack is full" << endl; } int pop(){ int data; if (TOP > -1){ data = STACK[TOP]; TOP--; return data; } return -999; } void printstack(){ int i; for (i = 0; i <= TOP; i++) cout << STACK[i] << ", "; cout << endl; } int main(){ push(52); push(85); printstack(); cout << "POP out: " << pop() << endl; cout << "POP out: " << pop() << endl; return 0; } ========================================================== example3.cpp ----------------------------------------- #include using namespace std; #define TOTALSIZE 6 int head = 0, tail = -1, q[TOTALSIZE]; void enqueue(int data){ if (tail <= TOTALSIZE - 2) { tail++; q[tail] = data; } else cout << "queue is full" << endl; } int dequeue(){ int data; if (head <= tail){ data = q[head]; head++; return data; } return -999; } void display(){ int i; for (i = head; i <= tail; i++) cout << q[i] << ", "; cout << endl; } int main(void){ int element, option; while (true){ do{ cout << "Queue Operations:" << endl; cout << "1. Enqueue" << endl; cout << "2. Dequeue" << endl; cout << "3. Display" << endl; cout << "> "; cin >> option; if (option == 1){ cout << "Element = "; cin >> element; } } while (option != 1 && option != 2 && option != 3); if (option == 1) enqueue(element); else if (option == 2){ element = dequeue(); if (element != -999) cout << "Dequeue element=" << element << endl; } else{ display(); } } return 0; } ====================================================== example4.cpp ------------------------------------------ #include #define size 6 using namespace std; int head, tail, q[size]; void initialize(){ head = 0; tail = -1; } bool empty(){ if (head == 0 && tail == -1) return true; else return false; } bool full(){ if ((tail + 1) % size == head && tail != -1) return true; else return false; } bool enqueue(int element){ if (full()){ cout << "Queue overflow" << endl; return false; } else{ tail = (tail + 1) % size; q[tail] = element; return true; } } bool dequeue(int *element){ if (empty()){ cout << "Queue underflow" << endl; return false; } else{ *element = q[head]; if (head == tail) initialize(); else{ head = (head + 1) % size; } return true; } } void display(){ int i = head; cout << "in display" << endl; if (!empty()){ while (i != tail){ cout << q[i] << ", "; i = (i + 1) % size; } cout << q[tail] << ", "; } cout << endl << endl; } int main(void){ bool success; int element, option; initialize(); while (true){ do{ cout << "Queue Operations:" << endl; cout << "1. Enqueue" << endl; cout << "2. Dequeue" << endl; cout << "3. Display" << endl; cout << "> "; cin >> option; if (option == 1){ cout << "Element = "; cin >> element; } } while (option != 1 && option != 2 && option != 3); if (option == 1) success = enqueue(element); else if (option == 2){ success = dequeue(&element); if (success) cout << "Dequeue element = " <