//Nick
#include <cstddef>
#ifndef DOUBLY_LINKED_LIST
#define DOUBLY_LINKED_LIST
template <class T>
class IntDLLNode {
friend class IntDLList;
public:
IntDLLNode(){next = prev = NULL;}
IntDLLNode(const T& el, IntDLLNode *n = NULL, IntDLLNode *p = NULL){
info = el;
next = n;
prev = p;
}
// protected:
T info;
IntDLLNode<T> *next, *prev;
};
template<class T> class IntDLList {
public:
IntDLList(){head = tail = NULL;}
void addToDLLTail(const T& el);
void addToDLLHead(const T& el);
void deleteNode(const T& el);
bool isInList(const T& el) const;
T deleteFromDLLTail();
T deleteFromDLLHead();
void addSorted(int);
void printList();
private:
IntDLLNode<T> *head, *tail;
};
#endif // DOUBLY_LINKED_LIST
#include <cstddef>
#include <iostream>
using namespace std;
template<class T>
void IntDLList<T>::addToDLLHead(const T& el){
if(head!=NULL){
head = new IntDLLNode<T>(el,head,NULL);
head->prev=head;
}
else head = tail = new IntDLLNode<T>(el);
}
template<class T>
void IntDLList<T>::addToDLLTail(const T& el){
if(tail!=NULL){
tail = new IntDLLNode<T>(el,NULL,tail);
tail->prev->next = tail;
}
else head = tail = new IntDLLNode<T>(el);
}
template<class T>
T IntDLList<T>::deleteFromDLLTail(){
if(head!=NULL){
T el = tail->info;
if(head == tail) {
delete tail;
head = tail = NULL;
}
else {
tail = tail-> prev;
delete tail->next;
tail->next = NULL;
}
return el;
}
else throw(20);
}
template<class T>
T IntDLList<T>::deleteFromDLLHead(){
if(head!=NULL){
int el = head->info;
IntDLLNode<T> *tmp = head;
if(head == tail){
head = tail = NULL;
}
else{head = head->next;}
delete tmp;
return(el);
}
else throw(20);
}
template<class T>
void IntDLList<T>::deleteNode(const T& el){
if(head!=NULL){
if(head == tail && el == head->info){
delete head;
head = tail = NULL;
}
else if(el == head->info){
IntDLLNode<T> *tmp = head;
head = head->next;
delete tmp;
}
else{
IntDLLNode<T> *els, *tmp;
for(els = head, tmp = head->next;
tmp!=NULL && tmp-> info!= el;
els = els->next, tmp = tmp->next);
if(tmp!=NULL){
els->next = tmp->next;
if(tmp == tail)
tail = els;
delete tmp;
}
}
}
}
template<class T>
bool IntDLList<T>::isInList(const T& el) const{
IntDLLNode<T> *tmp;
for(tmp=head;tmp!=NULL && tmp->info!= el;
tmp = tmp->next);
return(tmp!=NULL);
}
template<class T>
void IntDLList<T>::addSorted(int i) {
IntDLLNode<T> *tmp;
if(head==NULL)
head = tail = new IntDLLNode<T>(i);
else if(head!=NULL){
for(tmp=head;tmp!=NULL && tmp->info <= i;){
if(tmp->next->info >= i){
new IntDLLNode<T>(i,tmp,tmp->next);
break;
}
else{ tmp = tmp->next;}
}
}
}
template<class T>
void IntDLList<T>::printList(){
IntDLLNode<T> *tmp;
if(head!=NULL){
for(tmp=head; tmp!=NULL; tmp = tmp->next){
cout << tmp->info;
}
}
}
void f(){
try{
//int val = list.deleteFromHead();
}
catch(int error_code)
{
cerr << "Error: " <<error_code << endl;
switch (error_code){
default: cout << "error!!" << endl;
}
}
}
#include <iostream>
#include <stdio.h>
#include <list>
using namespace std;
int main()
{
int choice, choice2, numero;
cout << "Select an option\n" << endl;
cout << "1. Create Simple List" << endl;
cout << "2. Create Sorted List" << endl;
cout << "3. Create FIFO Queue" <<endl;
cout << "4. Create LIFO Queue" <<endl;
cout << "5. Exit Program" << endl;
cin >> choice;
IntDLList<int> dll;
switch(choice){
case 1:
switch(choice2){
//add to head
case 1:
cin >> numero;
dll.addToDLLHead(numero);
//add to tail
case 2:
cin >> numero;
dll.addToDLLTail(numero);
//delete head
case 3:
//display integer at head of list
dll.deleteFromDLLHead();
//delete tail
case 4:
//display integer at tail of list
dll.deleteFromDLLTail();
//delete integer from list
case 5:
cin >> numero;
dll.deleteNode(numero);
case 6:
dll.printList();
case 7: break;
}
case 2:
switch(choice2){
//add integer in sorted order
case 1:
cin >> numero;
dll.addSorted(numero);
//delete integer node
case 2:
cin >> numero;
dll.deleteNode(numero);
//print list
case 3: dll.printList();
//end program
case 4: break;
}
case 3:
switch(choice2){
//enqueue
case 1:
cin >> numero;
dll.addToDLLTail(numero);
//dequeue
case 2:
cin >> numero;
dll.deleteFromDLLHead();
//print FIFO
case 3: dll.printList();
//end
case 4: break;
}
case 4:
switch(choice2){
//add to stack
case 1:
cin >> numero;
dll.addToDLLHead(numero);
//delete from stack
case 2:
dll.deleteFromDLLHead();
//print stack
case 3: dll.printList();
//end program
case 4: break;
}
case 5: break;
}
}