Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <iostream>
  4.  
  5. template <typename T>
  6. class list{
  7. private:
  8.     struct element{
  9.         T data;
  10.         struct element * next;
  11.     };
  12.     element * head;
  13. public:
  14.     list(T elem){
  15.         element * p = (struct element *) malloc(sizeof(struct element));
  16.         p->data = elem;
  17.         p->next = head;
  18.         head = p;
  19.     }
  20. };
  21.  
  22. void main(){
  23.     struct element * head = NULL;
  24.    
  25.     for (int i = 0; i < 50; i++){
  26.         struct element * ml = (struct element *) malloc (sizeof(struct element));
  27.         ml->data = i;
  28.         ml->next = NULL;
  29.  
  30.         if (head == NULL) head = ml;
  31.         else {
  32.             struct element * cur = head;
  33.            
  34.             while (cur->next != NULL)
  35.                 cur = cur->next;
  36.             cur->next = ml;
  37.            
  38.         }
  39.     }
  40.  
  41.     list a(-1);
  42.     struct element * cur = head;
  43.  
  44.     while (cur != NULL){
  45.         std::cout << cur->data << " ";
  46.         cur = cur->next;
  47.     }
  48.  
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement