Guest User

Untitled

a guest
Jan 4th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. // Hindi talaga namin alam mam paano mag call at sa main banda :(
  2. // turuan nyo kame mam Thank you
  3.  
  4. # include <iostream>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. using namespace std;
  8. template <typename A>
  9.  
  10.  
  11.  
  12. class node{
  13.  
  14. public:
  15. A data;
  16. node *next;
  17.  
  18.  
  19.  
  20.  
  21. };
  22.  
  23. template <typename A>
  24. node<A>::node(A value){
  25.  
  26. data=value;
  27. next=NULL;
  28.  
  29. }
  30.  
  31.  
  32. template <typename A>
  33. class list{
  34.  
  35. node *head, *tail;
  36.  
  37. public:
  38.  
  39. void addNode1(A &start); // beginning
  40. void addNode2(A &start ); //last
  41. void addNode3(A &start ); //anywhere
  42. void deleteNode();
  43. void displayNode();
  44. char name[50];
  45. int age;
  46. // const (A val);
  47.  
  48. };
  49.  
  50. /*template<typename A>
  51. list<A>::const (A val)
  52. {
  53. nxtNode=0;
  54. }*/
  55.  
  56. /*class node{
  57.  
  58. public:
  59. node<A>(A value);;
  60.  
  61.  
  62. private:
  63. A data;
  64. node<A> *nxtNode;
  65. };*/
  66.  
  67. class start{
  68.  
  69. public:
  70. node *firstNode;
  71.  
  72. int listSize;
  73. }h;
  74.  
  75.  
  76.  
  77. template<typename A>
  78.  
  79. void list<A>::addNode1(A &start)
  80. {
  81. // node <int> *addnode = new node(value);
  82. node *addnode=new node;
  83. node *temp;
  84. temp=head;
  85. head= addnode;
  86.  
  87.  
  88. cout<<endl<<"List"<<endl<<endl;
  89.  
  90. cin.ignore();
  91. cout<<"Enter your name: ";
  92. cin.getline(addnode->name,50);
  93.  
  94. cout<<"Enter your age: ";
  95. cin>>addnode->age;
  96.  
  97.  
  98. cout<<"Enter your phone number: ";
  99. cin>>addnode->phoneNumber;
  100.  
  101. cin.ignore();
  102. cout<<"Enter your address: ";
  103. cin.getline(addnode->address,50);
  104.  
  105. addnode->nxt=NULL;
  106. addnode->nxt=temp;
  107.  
  108.  
  109. }
  110.  
  111.  
  112. template<typename A>
  113. void list<A>::addNode2(A &start)
  114. {
  115. node *temp;
  116.  
  117. temp=head;
  118.  
  119. node *addnode= new node;
  120.  
  121. cout<<endl<<"List"<<endl<<endl;
  122.  
  123. cin.ignore();
  124. cout<<"Enter your name: ";
  125. cin.getline(addnode->name,50);
  126.  
  127. cout<<"Enter your age: ";
  128. cin>>addnode->age;
  129.  
  130.  
  131. cout<<"Enter your phone number: ";
  132. cin>>addnode->phoneNumber;
  133.  
  134. cin.ignore();
  135. cout<<"Enter your address: ";
  136. cin.getline(addnode->address,50);
  137.  
  138.  
  139. addnode->nxt = NULL;
  140. cin.ignore();
  141.  
  142.  
  143.  
  144. while(temp->nxt !=NULL)
  145. {
  146. temp=temp->nxt;
  147. }
  148.  
  149. temp->nxt=addnode;
  150.  
  151.  
  152. }
  153.  
  154.  
  155.  
  156. template<typename A>
  157. void list<A>::addNode3(A &start)
  158. {
  159. node *hopper=h.firstNode;
  160. node *addnode = new node;
  161. int pos=0;
  162.  
  163.  
  164.  
  165. if(h.firstNode==0)
  166. {
  167.  
  168. cout << "Enter your friend's name: ";
  169. cin.ignore();
  170. cin.getline(addnode->name,50);
  171. cout << "Enter your friend's age: ";
  172. cin >> addnode->age;
  173. addnode->next = 0;
  174. cin.ignore();
  175.  
  176. h.firstNode=addnode;
  177. h.listSize=1;
  178. }
  179. else
  180. {
  181.  
  182. cout << "You currently have " << h.listSize << " in your list." << endl;
  183. cout << "In what position do you wish to insert your new node? :";
  184. do
  185. {
  186. cin >> pos;
  187. if(pos <= 0 || pos > h.listSize+1)
  188. cout << endl << "You cannot insert a node in that position. Enter another position: ";
  189.  
  190. }while(pos <= 0 || pos > h.listSize+1);
  191.  
  192. int counter=1;
  193.  
  194. cin.ignore();
  195. cout << "Enter your friend's name: ";
  196. cin.getline(addnode->name,50);
  197. cout << "Enter your friend's age: ";
  198. cin >> addnode->age;
  199. cin.ignore();
  200.  
  201.  
  202. if(pos==1)
  203. {
  204. addnode->next=h.firstNode;
  205. h.firstNode=addnode;
  206.  
  207. }
  208. else
  209. {
  210. if(pos > h.listSize)
  211. {
  212. while(counter < pos-1)
  213. {
  214. hopper=hopper->next;
  215. counter++;
  216. }
  217.  
  218. addnode->next=0;
  219. hopper->next=addnode;
  220.  
  221. }
  222. else
  223. {
  224. while(hopper!=0)
  225. {
  226. if(counter==pos-1)
  227. break;
  228. hopper=hopper->next;
  229. counter++;
  230.  
  231. }
  232.  
  233. addnode->next=hopper->next;
  234. hopper->next=addnode;
  235.  
  236. }
  237. }
  238. h.listSize++;
  239.  
  240. }
  241.  
  242. }
  243.  
  244. template<typename A>
  245. void list<A>::deleteNode()
  246. {
  247.  
  248. char name1 [50];
  249. node *temp=h.firstNode;
  250. node *preceeding=h.firstNode;
  251. node *target=0;
  252.  
  253. cin.ignore();
  254. cout << "Enter friend's name to delete:" ;
  255. cin.getline(name1, 50);
  256.  
  257.  
  258.  
  259. while(temp != 0)
  260. {
  261.  
  262. if(strcmp(temp->name,name1)==0)
  263. {
  264. ChangeCase(temp->name);
  265. target=temp;
  266. break;
  267. }
  268.  
  269. preceeding=temp;
  270. temp=temp->next;
  271.  
  272. }
  273.  
  274. if(target!=0)
  275. {
  276. if(target->next==0)
  277. {
  278.  
  279. preceeding->next=0;
  280. delete target;
  281.  
  282. }
  283.  
  284. else
  285. {
  286. if(target==h.firstNode)
  287. {
  288.  
  289. h.firstNode=target->next;
  290.  
  291. delete target;
  292. }
  293. else
  294. {
  295. preceeding->next=target->next;
  296. delete target;
  297.  
  298. }
  299. }
  300.  
  301. cout << "Successfully deleted " << name1 << "'s record!" << endl;
  302. }
  303.  
  304. else
  305.  
  306. cout << "Your friend's record is not found! " << endl;
  307.  
  308. system("pause");
  309.  
  310.  
  311. }
  312.  
  313. template<typename A>
  314. void list<A>::displayNode()
  315. {
  316. node *display = h.firstNode;
  317.  
  318. while(display != 0)
  319. {
  320. cout << display->name << " " << display->age << endl;
  321. display=display->next;
  322. }
  323.  
  324. system("pause");
  325. }
  326.  
  327. //template<typename A>
  328. void main (){
  329.  
  330.  
  331. int l;
  332. list <int> mylist;
  333.  
  334. int c=0;
  335.  
  336. h.listSize=0;
  337. h.firstNode=NULL;
  338.  
  339.  
  340. do{
  341. system("CLS");
  342.  
  343. cout<<"\t\tMenu"<<endl;
  344. cout<<"[1]Add Node in the beginning"<<endl;
  345. cout<<"[2]Add Node in the End"<<endl;
  346. cout<<"[3]Choose your position"<<endl;
  347. cout<<"[4]Delete"<<endl;
  348. cout<<"[5]Display"<<endl;
  349. cout<<"[6]Exit"<<endl<<endl;
  350. cout<<endl;
  351. cout<<"Enter your choice: ";
  352. cin>>c;
  353. cout<<endl;
  354.  
  355. switch(c){
  356. case 1:
  357. mylist.addNode1(l);
  358. break;
  359. case 2:
  360. // addNode2(h);
  361. break;
  362. case 3:
  363. // addNode3(h);
  364. break;
  365. case 4:
  366. // deleteNode();
  367. break;
  368.  
  369. case 5:
  370. // displayNode();
  371. break;
  372.  
  373. case 6:
  374. cout<<"buh bye!"<<endl;
  375. break;
  376. }
  377. }while(c!=6);
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384. }
  385.  
  386.  
  387. /*class list
  388. not class head
  389. class list
  390. tapos
  391. may dalawang node
  392. head and tail
  393.  
  394.  
  395.  
  396. template<typename A>
  397.  
  398. class node{
  399. public:
  400. A data;
  401. node *next;
  402. node(A value){
  403. data=value;
  404. next=NULL;
  405. }
  406. };
  407.  
  408. 5 minutes agoJake Laurence Santiago
  409. ganun. pero una ang node bago ang list
  410. tapos
  411. para sa input
  412.  
  413. node <int> *newnode = new node(int value here);
  414. pde din <char> <float> <double> or <bool>
  415.  
  416. pero sa void main(){
  417. list l; lang */
Add Comment
Please, Sign In to add comment