I'm So Confused
Mar 17, 2013 at 5:16am UTC
This Whole Example is Confusing me to much Especially at Line 59
current_node = new_node; if current Node is = to New Node then the current Must Have the Value of the New for example if new node's Value is 6 the current node should have 6 that's not an error i just want an explanation Please Explain it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
class Node{
public :
int getn()
{
return object;
}
void setn(int obj)
{
this ->object = obj;
}
Node* getnex()
{
return Next_node;
}
void setnext(Node *Next_node)
{
this ->Next_node = Next_node;
}
private :
int object;
Node* Next_node;
};
class list{
public :
void add(int );
int get();
bool next();
friend void traverse(list List);
friend list addnodes();
list()
{
head_node = new Node();
head_node->setnext(NULL);
current_node = NULL;
size = 0;
}
private :
int size;
Node* current_node;
Node* head_node;
Node* last_current_node;
};
void list::add(int obj)
{
Node* new_node = new Node();
new_node->setn(obj);
if (current_node != NULL)
{
new_node->setnext(current_node->getnex());
current_node->setnext(new_node);
last_current_node = current_node;
current_node = new_node;
}
else
{
new_node->setnext(NULL);
head_node->setnext(new_node);
last_current_node = head_node;
current_node = new_node;
}
size++;
}
int list::get()
{
if (current_node!=NULL)
{
return current_node->getn();
}
}
bool list::next()
{
if (current_node == NULL)
{
return false ;
}
last_current_node = current_node;
current_node = current_node->getnex();
if (current_node == NULL || size == 0)
{
return false ;
}
else
{
return true ;
}
}
void traverse(list List)
{
Node* saved_C_node = List.current_node;
List.current_node = List.head_node;
for (int i = 1;List.next();i++)
{
cout<<"\n Elements: " <<i<<" " <<List.current_node;
}
}
list addnodes()
{
list LIst;
LIst.add('s' );
LIst.add('x' );
LIst.add(3);
LIst.add(4);
LIst.add(5);
LIst.add(6);
LIst.add(7);
LIst.add(8);
cout<<"\n List Size Is = " <<LIst.size<<endl;
return LIst;
}
int main()
{
list LIst = addnodes();
traverse(LIst);
_getch();
}
Mar 17, 2013 at 5:57am UTC
current_node is a pointer, like a note with an address. You changing it to point to new_node, like writing down new address of your friend, who have moved
Mar 17, 2013 at 9:37am UTC
linked list is so confusing. You really need a board and draw your nodes list and the node itself. Keep tracking each node.
Topic archived. No new replies allowed.