Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <exception>
  5.  
  6. using namespace std;
  7.  
  8. //The node that we are going to use in both of our data structures
  9. template <typename T>
  10. struct Node
  11. {
  12.     T data;
  13.     Node* next;
  14. };
  15.  
  16. template <typename T>
  17. class Stack{
  18.  
  19. private:
  20.     Node<T>* head = NULL;
  21.     int stackSize;
  22.  
  23. public:
  24.     Stack(){
  25.         stackSize = 0;
  26.     }
  27.  
  28.     //We push and pop the elements from the head in order for the push/pop to be in a constant time O(1)
  29.    
  30.     void push(T n){
  31.         Node<T>* newNode = new Node<T>();
  32.         newNode->data = n;
  33.         newNode->next = head;
  34.         head = newNode;
  35.         stackSize++;
  36.     }
  37.  
  38.    
  39.     void pop(){
  40.         if (head == NULL) throw out_of_range("Stack Underflow");
  41.         Node<T>* temp = head;
  42.         head = head->next;
  43.         delete temp;
  44.         stackSize--;
  45.     }
  46.  
  47.     bool isEmpty(){
  48.         if (head == NULL) return true;
  49.         else return false;
  50.     }
  51.  
  52.     T top(){
  53.         return head->data;
  54.     }
  55.  
  56.     int size(){
  57.         return stackSize;
  58.     }
  59.  
  60.     void print(){
  61.         Node<T>* temp = head;
  62.         while (temp != NULL){
  63.             cout << temp->data << " ";
  64.             temp = temp->next;
  65.         }
  66.         cout << endl;
  67.     }
  68.  
  69. };
  70.  
  71.  
  72. template <typename T>
  73. class SortedList{
  74. private:
  75.     Node<T>* head;
  76.     int sizeOfList;
  77.  
  78. public:
  79.     SortedList(){
  80.         head = NULL;
  81.         sizeOfList = 0;
  82.     }
  83.  
  84.     void Insert(T num){
  85.         Node<T>* newNode = new Node<T>();
  86.         newNode->data = num;
  87.  
  88.         //If the list is empty
  89.         if (head == NULL){
  90.             newNode->next = NULL;
  91.             head = newNode;
  92.             sizeOfList++;
  93.             return;
  94.         }
  95.  
  96.         Node<T>* temp = head;
  97.  
  98.         while (temp->data < num && temp->next != NULL){
  99.             temp = temp->next;
  100.         }
  101.  
  102.         //If temp is still equal to head then it means that we haven't entered the while loop
  103.         //and we need to insert at the head
  104.  
  105.         if (head == temp){
  106.             if (newNode->data < temp->data){
  107.                 newNode->next = temp;
  108.                 head = newNode;
  109.             }
  110.             else{
  111.                 newNode->next = temp->next;
  112.                 temp->next = newNode;
  113.             }
  114.         }
  115.  
  116.         //Insert anywhere but the head
  117.         else{
  118.             newNode->next = temp->next;
  119.             temp->next = newNode;
  120.         }
  121.  
  122.         sizeOfList++;
  123.     }
  124.  
  125.     void Delete(T num){
  126.         Node<int>* temp = head;
  127.         Node<int>* prev = temp;
  128.  
  129.         //Looking for the number that we need to delete
  130.         while (num != temp->data){
  131.             prev = temp;
  132.             temp = temp->next;
  133.         }
  134.  
  135.         //If the prev == temp that means that we didn't enter the while loop A.K.A we need to insert at the head
  136.         if (prev == temp){
  137.             head = temp->next;
  138.             delete temp;
  139.         }
  140.  
  141.         //Insert at the tail or in a position other than the head
  142.         else{
  143.             prev->next = temp->next;
  144.             delete temp;
  145.         }
  146.  
  147.         sizeOfList--;
  148.     }
  149.  
  150.     int size(){
  151.         return sizeOfList;
  152.     }
  153.  
  154.     bool isEmpty(){
  155.         return head == NULL ? true : false;
  156.     }
  157.  
  158.     void print(){
  159.         Node<T>* temp = head;
  160.  
  161.         while (temp != NULL){
  162.             cout << temp->data << " ";
  163.             temp = temp->next;
  164.         }
  165.  
  166.         cout << endl;
  167.     }
  168.  
  169. };
  170.  
  171.  
  172. int main(){
  173.     //Testing
  174.  
  175.     Stack<int>* s = new Stack<int>();
  176.     cout << "The Stack: " << endl;
  177.    
  178.     s->push(13);
  179.     s->push(5);
  180.     s->print();
  181.     s->pop();
  182.     s->print();
  183.    
  184.     cout << endl;
  185.  
  186.     SortedList<int>* l = new SortedList<int>();
  187.  
  188.     cout << "The Sorted List: " << endl;
  189.  
  190.     l->Insert(13);
  191.     l->Insert(12);
  192.     l->Insert(14);
  193.     l->Insert(4);
  194.     l->Insert(44);
  195.     l->Insert(55);
  196.     l->Insert(1);
  197.     l->Insert(13);
  198.    
  199.  
  200.     l->print();
  201.  
  202.     l->Delete(1);
  203.     l->print();
  204.  
  205.     l->Delete(55);
  206.     l->print();
  207.  
  208.     l->Delete(13);
  209.     l->print();
  210.  
  211.     cout << "The list size is: " << l->size() << endl;
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement