Google PlusFacebookTwitter

(Circular) Queues : Implementation Using Array

By on Dec 15, 2007 in On A Whim | 4 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Array implementation of a Circular Queue */ #include <iostream.h> #include <conio.h> #include <process.h> void main() { clrscr(); const int max = 5; int ch, n, front = -1, rear = -1, q[max], i; char choice;do { clrscr();cout<<endl<<“Program Menu” <<endl<<“1. Insert new element” <<endl<<“2. Delete an element” <<endl<<“3. Show list” <<endl<<“4. Exit” <<endl<<“Enter your choice: “; cin>>ch; switch(ch) { case 1 : clrscr();if (front == (rear + 1) % max)cout<<endl<<“Queue overflow! Insertion not possible!”;else if (front == -1 && rear == -1) { front = 0; rear = 0; cout<<endl<<“Enter number to be inserted: “;cin>>n; q[front] = n; } else { rear = (rear + 1) % max; cout<<endl<<“Enter number to be inserted: “;cin>>n; q[rear] = n; } break;case 2 : clrscr();if (front == -1 && rear == -1)cout<<endl<<“Queue does not exist!”;else if (rear == 0 && front == 0){ n = q[front]; rear = -1; front = -1; cout<<endl<<“Deleted data item: “<<n<<endl; } else { n = q[front]; front = (front + 1) % max; cout<<endl<<“Deleted data item: “<<n<<endl; } break; case 3 : clrscr();if (front == -1 && rear == -1)cout<<endl<<“Queue does not exist!”;else if(front == 0 && rear == max – 1){ cout<<“\nDisplaying items:\n”;for (i = front; i <= rear; i++) cout<<q[i]<<” “; cout<<endl; } else { cout<<“\nDisplaying items:\n”;for(i=front; i!=rear; i=(i+1)%max)cout<<q[i]<<” “; cout<<q[rear]<<endl; } break; case 4 : cout<<endl<<“Exiting program”<<endl;exit(0); default : cout<<endl<<“You entered an invalid choice!”;}cout<<endl<<“Do you wish to continue? (y/n): “;cin>>choice;} while (choice == ‘Y’ || choice == ‘y’);} /* Output */ Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 1 Enter number to be inserted: 41 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 1 Enter number to be inserted: 42 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 1 Enter number to be inserted: 43 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 3 Displaying items: 41 42 43 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 2 Deleted data item: 41 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 3 Displaying items: 42 43 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 4 Exiting...

Queues : Implementation Using Array

By on Dec 15, 2007 in On A Whim | 0 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Array implementation of a Queue */ #include <iostream.h> #include <conio.h> #include <process.h> void main() { const int max = 10; int ch, n, front = -1, rear = -1, q[max];char choice;do { clrscr();cout<<endl<<“Program Menu” <<endl<<“1. Insert new element” <<endl<<“2. Delete an element” <<endl<<“3. Show list” <<endl<<“4. Exit” <<endl<<“Enter your choice: “; cin>>ch; switch(ch) { case 1 : clrscr();cout<<endl<<“Enter number to be inserted: “;cin>>n; if (rear == max-1) cout<<endl<<“Queue overflow! Insertion not possible!”;else { if (front == -1) { front++; q[front] = n; rear = front; } else { rear++; q[rear] = n; } } break; case 2 : clrscr(); if (front == -1) cout<<endl<<“Queue does not exist!”;else if (rear == front) { n = q[front]; rear = -1; front = -1; cout<<endl<<“Deleted data item: “<<n<<endl; } else { n = q[front]; front++; cout<<endl<<“Deleted data item: “<<n<<endl; } break; case 3 : clrscr(); if (front == -1) cout<<endl<<“Queue does not exist!”;else { cout<<“\nDisplaying items:\n”;for(int i = front; i <= rear; i++) cout<<q[i]<<” “; cout<<endl; } break; case 4 : cout<<endl<<“Exiting program”<<endl;exit(0); default : cout<<endl<<“You entered an invalid choice!”;}cout<<endl<<“Do you wish to continue? (y/n): “;cin>>choice; } while (choice == ‘Y’ || choice == ‘y’);} /* Output */ Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 1 Enter number to be inserted: 42 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 1 Enter number to be inserted: 41 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 1 Enter number to be inserted: 43 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 3 Displaying items:42 41 43 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 2 Deleted data item: 42 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 3 Displaying items:41 43 Do you wish to continue? (y/n): y Program Menu 1. Insert new element 2. Delete an element 3. Show list 4. Exit Enter your choice: 4 Exiting...

Stacks : Implementation Using Arrays

By on Dec 15, 2007 in On A Whim | 0 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Array implementation of a Stack */ #include <iostream.h> #include <conio.h> #include <process.h> class array { int num[10], top, max; public: array() { top = -1; max = 9; } void push(int n) {if (top == max) cout<<endl<<“Stack overflow! Cannot be inserted!”;else { top++; num[top] = n; } } void pop(){if (top == -1) cout<<endl<<“Stack overflow! Stack does not exist!”;else { int n = num[top]; top–; cout<<endl<<“Deleted number is: “<<n<<endl;} } void traverse();}; void array::traverse() { if (top == -1) cout<<endl<<“Stack underflow! Stack does not exist!”;else{ for (int i = top; i >= 0; i–)cout<<num[i]<<” “; cout<<endl; } } void main() { array ft; int ch; int n; char choice; do { clrscr();cout<<endl<<“Program Menu” <<endl<<“1. Push (insert)” <<endl<<“2. Pop (delete)” <<endl<<“3. Traverse (show whole list)”<<endl<<“4. Exit” <<endl<<“Enter your choice: “; cin>>ch; switch(ch) { case 1 : cout<<endl<<“Enter number to be inserted: “;cin>>n; ft.push(n); break; case 2 : ft.pop();break; case 3 : ft.traverse();break; case 4 : cout<<endl<<“Exiting program”<<endl;exit(0); default : cout<<endl<<“You entered an invalid choice!”;}cout<<endl<<“Do you wish to continue? (y/n): “; cin>>choice;} while (choice == ‘Y’ || choice == ‘y’);} /* Output */ Program Menu 1. Push (insert) 2. Pop (delete) 3. Traverse (show whole list) 4. Exit Enter your choice: 1 Enter number to be inserted: 42 Do you wish to continue? (y/n): y Program Menu 1. Push (insert) 2. Pop (delete) 3. Traverse (show whole list) 4. Exit Enter your choice: 1 Enter number to be inserted: 41 Do you wish to continue? (y/n): y Program Menu 1. Push (insert) 2. Pop (delete) 3. Traverse (show whole list) 4. Exit Enter your choice: 1 Enter number to be inserted: 43 Do you wish to continue? (y/n): y Program Menu 1. Push (insert) 2. Pop (delete) 3. Traverse (show whole list) 4. Exit Enter your choice: 3 43 41 42 Do you wish to continue? (y/n): y Program Menu 1. Push (insert) 2. Pop (delete) 3. Traverse (show whole list) 4. Exit Enter your choice: 2 Deleted number is: 43 Do you wish to continue? (y/n): y Program Menu 1. Push (insert) 2. Pop (delete) 3. Traverse (show whole list) 4. Exit Enter your choice: 3 41 42 Do you wish to continue? (y/n): y Program Menu 1. Push (insert) 2. Pop (delete) 3. Traverse (show whole list) 4. Exit Enter your choice: 4 Exiting...

Queues : Implementation Using Linked Lists

By on Dec 15, 2007 in On A Whim | 2 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Linked List implementation of a Queue */ #include <iostream.h>#include <stdio.h>#include <conio.h>#include <process.h> class student{int rno;char nm[42];float perc;public:student *next;void getdata(){cout<<endl<<“Enter roll number: “;cin>>rno;cout<<“Enter name: “;gets(nm);cout<<“Enter percentage: “;cin>>perc;next = NULL;}void showdata(){cout<<endl<<“Roll number: “<<rno<<endl<<“Name: “<<nm<<endl<<“Percentage: “<<perc<<endl;}}; void main(){int ch;char choice;student *newptr, *front = NULL, *rear = NULL, *ptr;do{clrscr();cout<<endl<<“Program Menu”<<endl<<“1. Push (insert)”<<endl<<“2. Pop (delete)”<<endl<<“3. Traverse (show whole list)”<<endl<<“4. Exit program”<<endl<<“Enter your choice: “;cin>>ch;switch(ch){case 1 : clrscr();newptr = new student;if (!newptr){cout<<endl<<“Memory allocation error! Exiting program”;exit(0);}newptr->getdata();if (front == NULL){front = newptr;rear = newptr;}else{rear->next = newptr;rear = newptr;}break;case 2 : if (front == NULL)cout<<endl<<“Error! Queue does not exist!”;else{if (front == rear){ptr = front;front = NULL;rear = NULL;}else{ptr = front;front = front->next;}cout<<endl<<“Showing details of deleted element: “;ptr->showdata();delete ptr;}break;case 3 : ptr = front;if (front == NULL)cout<<endl<<“Error! Queue does not exist!”;else{while (ptr != NULL){ptr->showdata();ptr = ptr->next;}}break;case 4 : cout<<endl<<“Exiting program”<<endl;exit(0);default : cout<<endl<<“You entered an invalid choice!”;}cout<<endl<<“Do you wish to continue? (y/n): “;cin>>choice;}while (choice == ‘Y’ || choice == ‘y’);} /* Output */ Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 1 Enter roll number: 42Enter name: Ankur BanerjeeEnter percentage: 42 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 1 Enter roll number: 41Enter name: Arthur DentEnter percentage: 75 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 1 Enter roll number: 45Enter name: Ford PrefectEnter percentage: 65 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 3 Roll number: 42Name:Ankur BanerjeePercentage: 42 Roll number: 41Name:Arthur DentPercentage: 75 Roll number: 45Name:Ford PrefectPercentage: 65 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 2 Showing details of deleted element:Roll number: 42Name:Ankur BanerjeePercentage: 42 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 3 Roll number: 41Name:Arthur DentPercentage: 75 Roll number: 45Name:Ford PrefectPercentage: 65 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 4 Exiting...

Stacks : Implementation Using Linked Lists

By on Dec 15, 2007 in On A Whim | 0 comments

/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULARPURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Linked List implementation of a Stack */ #include <iostream.h>#include <stdio.h>#include <conio.h>#include <process.h> class student{ int rno; char nm[42]; float perc; public: student *next; void getdata() {cout<<endl<<“Enter roll number:”; cin>>rno; cout<<“Enter name: “; gets(nm); cout<<“Enter percentage: “; cin>>perc; next = NULL;} void showdata() {cout<<endl<<“Roll number: “<<rno<<endl<<“Name: “<<nm<<endl<<“Percentage: “<<perc<<endl; }}; void main(){ int ch; char choice; student *newptr, *top = NULL, *ptr; do { clrscr();cout<<endl<<“Program Menu”<<endl<<“1. Push (insert)”<<endl<<“2. Pop (delete)”<<endl<<“3. Traverse (show whole list)”<<endl<<“4. Exit program”<<endl<<“Enter your choice: “; cin>>ch;switch(ch) { case 1 :clrscr(); newptr = new student; if (!newptr) {cout<<endl<<“Memory allocation error! Exiting program”;exit(0); } newptr->getdata(); if (top == NULL) {top = newptr; } else {newptr->next = top;top = newptr; } break; case 2 :if (top == NULL)cout<<endl<<“Error! Stack does not exist!”; else {ptr = top;top = top->next; }cout<<endl<<“Showing details of deleted element:”; ptr->showdata(); delete ptr; break; case 3 :ptr = top; if (top == NULL)cout<<endl<<“Error! Stack does not exist!”; else {while (ptr != NULL){ ptr->showdata(); ptr = ptr->next;} }break; case 4 : cout<<endl<<“Exiting program”; exit(0); default :cout<<endl<<“You entered an invalid choice!”; }cout<<endl<<“Do you wish to continue? (y/n): “;cin>>choice; } while (choice == ‘Y’ || choice == ‘y’);} /* Output */ Program Menu1. Push (insert)2. Pop (delete) 3. Traverse (show whole list)4. Exit programEnter your choice: 1 Enter roll number: 42Enter name: Ankur BanerjeeEnter percentage: 42 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 1 Enter roll number: 10Enter name: Arthur DentEnter percentage: 96 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 3 Roll number: 10Name: Arthur DentPercentage: 96 Roll number: 42Name: Ankur BanerjeePercentage: 42 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 2 Showing details of deleted element:Roll number: 10Name: Arthur DentPercentage: 96 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 3 Roll number: 42Name: Ankur BanerjeePercentage: 42 Do you wish to continue? (y/n): y Program Menu1. Push (insert)2. Pop (delete)3. Traverse (show whole list)4. Exit programEnter your choice: 4 Exiting...