Advertisement
Doragonroudo

[Missed Class] EGCO112 - LL-NODE

Apr 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. /* LL-NODE.h */
  2.  
  3. #ifndef NODE_h
  4. #define NODE_h
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. class NODE
  9. {
  10.     int id;
  11.     string name;
  12.     NODE* next;
  13. public:
  14.     NODE(int,string);
  15.     void show_node();
  16.     void insert(NODE*&);
  17.     NODE* move_next();
  18.     ~NODE();
  19. };
  20.  
  21. #endif
  22.  
  23.  
  24.  
  25.  
  26. /* LL-LL */
  27.  
  28. class LL
  29. {
  30.     NODE*hol;
  31.     int size;
  32. public:
  33.     void add_node(NODE*&);
  34.     void show_all();
  35.     LL();
  36.     ~LL();
  37. };
  38.  
  39. /* LL-NODE.cpp */
  40.  
  41. #include"NODE.h"
  42.  
  43. NODE::NODE(int x,string n)
  44. {
  45.     id=x;
  46.     name=n;
  47.     next=NULL;
  48.     cout<<"adding "<<x<<endl;
  49. }
  50. NODE:: ~NODE()
  51. {
  52.     cout<<"Node "<<id<<" is being deleted"<<endl;
  53. }
  54. NODE* NODE::move_next()
  55. {
  56.     return next;
  57. }
  58. void NODE::show_node()
  59. {
  60.     cout<<"Node data:"<<id<< " Name " <<name<< endl;
  61. }
  62. void NODE::insert(NODE*& x)
  63. {
  64.     x->next=this;
  65. }
  66.  
  67. /* LL-LL.cpp */
  68.  
  69. #include"NODE.h"
  70. #include"LL.h"
  71. LL::LL()
  72. {
  73.     hol=NULL;
  74.     size=0;
  75. }
  76.  
  77. LL::~LL()
  78. {
  79.     NODE* tem;
  80.     while(hol!=NULL){
  81.         tem=hol->move_next();
  82.         delete hol;
  83.         hol=tem;
  84.     }
  85. }
  86.  
  87. void LL::show_all()
  88. {
  89.     NODE* t=hol;
  90.     int i;
  91.     for(i=0; i<size; i++)
  92.     {
  93.         t->show_node();
  94.         t=t->move_next();
  95.     }
  96. }
  97. void LL::add_node(NODE *&A)
  98. {
  99.     hol->insert(A);
  100.     hol=A;
  101.  
  102.     size++;
  103. }
  104.  
  105. /* LL-LLtest */
  106.  
  107. #include <iostream>
  108. #include <cstdlib>
  109. #include "NODE.h"
  110. #include "LL.h"
  111. using namespace std;
  112.  
  113. class student:public NODE
  114. {
  115.     double gpa;
  116. public:
  117.     student(int x,string n,double g):NODE(x,n)
  118.     {
  119.         gpa=g;
  120.     }
  121.     ~student();
  122. };
  123.  
  124. int main(int argc, char *argv[])
  125. {
  126.     LL A;
  127.     int i;
  128.     NODE *t;
  129.  
  130.     for(i=1; i<argc; i+=3)
  131.     {
  132.         t = new student(atoi(argv[i]),argv[i+1],atof(argv[i+2]));
  133.         A.add_node(t);
  134.     }
  135.     A.show_all();
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement